vendredi 29 novembre 2019

How can i improve this form all in one php form?

So i did this all in one form for a project and was wondering how i could go about making it better, or more efficient. The form adds a player to a text box and then displays confirmation.Here is my code. The form validates using another file but im fine with what that portion is doing. I just want to make sure im doing things properly.

<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>
            New Player
        </title>
        <link href="https://fonts.googleapis.com/css?family=Fjalla+One|Odibee+Sans&display=swap" rel="stylesheet"> 
        <link rel="stylesheet" href=".\styles\styles.css">
    </head>

    <body>
        <div id="title">
      <h1>Add Player</h1>
</div>

<?php

include "playerFunctions.php";

$displayForm=true;
$firstName ="";
$lastName = "";
$email ="";
$city="";
$country="";
$proornot="";
$passWord="";
$passWordCon="";
$firstNameError ="";
$lastNameError ="";
$emailError ="";
$cityError="";
$pwError="";
$pwconError="";


if (isset($_POST["myB"])){

    $displayForm=false;

    $firstName = $_POST["fname"];
    $lastName = $_POST["lname"];
    $email = $_POST["email"];
    $city = $_POST["city"];
    $country=$_POST["country"];
    $proornot= $_POST["pro"];
    $passWord=$_POST["pwd"];
    $passWordCon=$_POST["confirmpwd"];


    if(!validName($firstName)){

            $firstNameError="Enter valid first name";
            $displayForm = true;

     }
     if(!validName($lastName)){
        $lastNameError="Enter valid last name";
        $displayForm = true;
 }

 if(!validEmail($email)){
    $emailError="Please enter a valid email";
    $displayForm = true;
}

if(!validCity($city)){
    $cityError="Please enter a valid city";
    $displayForm = true;
}
if(!$passWord){
    $pwError="Please enter a password";
    $displayForm = true;
}
if(!validPassword($passWord,$passWordCon)){
    $pwconError="Please confirm your password";
    $displayForm = true;
}

else if(!$displayForm)
  {
   $proornot=(!($proprnot)?"yes":"no");
   $myfile = fopen("player.txt", "a") or die("Unable to open file!");
   $txt = $firstName."~".$lastName."~".$email."~".$city."~".$country."~".$proornot."\r\n";
   fwrite($myfile, $txt);
   fclose($myfile);
  }
}
if($displayForm){


?>






<form id="myForm" name="myFormName" action="NewPlayer.php" method="post">

<label for="fname"> First Name:</label>
<input type="text" name="fname" id="fname" <?php if($firstNameError){echo 'style="border:1px solid red;"';}?> value="<?php  echo $firstName;?>"/>
<br/><span id="errorf"><?php echo $firstNameError;?></span><br/>

<label for="lname"> Last Name:</label>
<input type="text" name="lname" id="lname"<?php if($lastNameError){echo 'style="border:1px solid red;"';}?> value="<?php echo $lastName;?>"/>
<br/><span id="errorl"><?php echo $lastNameError;?></span><br/>

<label for="email"> Email Address:</label>
<input type="text" name="email" id="email" <?php if($emailError){echo 'style="border:1px solid red;"';}?>   value="<?php echo $email;?>"/>
<br/><span id="errort"><?php echo $emailError;?></span><br/>

<label for="city"> City:</label>
<input type="text" name="city" id="city" <?php if($cityError){echo 'style="border:1px solid red;"';}?> value="<?php echo $city;?>"/>
<br/><span id="errort"><?php echo $cityError;?></span><br/>


<label for="country"> Country:</label>

<select name="country" >
  <option value="Canada">Canada</option>
  <option value="United States">United States</option>
  <option value="Columbia">Argentina</option>
  <option value="Argentina">Brazil</option>
  <option value="Peru">Columbia</option>
  <option value="Brazil">Ecuador</option>
  <option value="Ecuador">Peru</option>
  <option value="Poland">Poland</option>
</select> <br/>

<label for="pro" id="checkLabel"> Professional:</label>
<input type="checkbox" id="check" name="pro" value="<?php echo $proornot;?>"/><br>

<label for="pwd" id="pw"> Password:</label>
<input type="password" name="pwd" <?php if($pwError){echo 'style="border:1px solid red;"';}?>  value="<?php echo $passWord;?>"/> <br/>
<span id="errorpw"><?php echo $pwError;?></span><br/>

<label for="confirmpwd" id="conpw">Confirm Password:</label>
<input type="password" id="conpw" name="confirmpwd"  <?php if($pwconError){echo 'style="border:1px solid red;"';}?> value="<?php echo $passWordCon;?>"/> <br/>
<span id="errorlpwcon"><?php echo $pwconError;?></span><br/>

<button type="submit" name="myB" id="myB" class="myButton" value="Add Player">Add Player</button><br/>
</form>
<?php
}

else{
    echo "<div id=title><h1>".$firstName." ".$lastName." has been added to your roster. Good Luck with your pool </h1></div><br/>";
    echo "<button type=button onClick=location.href='NewPlayer.php' name=myB id=myB class=myButton value=BackToForm>Back to Form</button><br/>";
}



?>
    </body>
</html>```



Aucun commentaire:

Enregistrer un commentaire