Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

PHP keep form data after failed submit

I know that there are many similar questions here that has been answered, but none of the answers has worked for me. I simply want the information that the user has already typed in to stay in the fields when the form submission fails (because of empty fields or such). I have tried using the code below but it doesn't work.

register.php:

<form class="form" id="form" action="includes/signup.inc.php" method="post">
<label>Full name</label>
<input type="text" placeholder="Enter full name..." name="name" value="<?php if (isset($_POST['name'])) { echo htmlentities($_POST['name']); } ?>">
<button type="submit" id="registerbtn" name="submit" value="submit" >Register</button>
</form>

signup.inc.php:

<?php

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

  $name = $_POST["name"];
  $email = $_POST["email"];
  $pwd = $_POST["pwd"];
  $pwdRepeat = $_POST["pwdrepeat"];
  $phone = $_POST["phone"];
  $address = $_POST["address"];
  $city = $_POST["city"];
  $value = $_POST['usertype'];

  require_once 'dbh.inc.php';
  require_once 'functions.inc.php';

  if (emptyInputSignup($name, $email, $pwd, $pwdRepeat, $phone, $address, $city) !== false) {
    header("location: ../register.php?error=emptyinput");
    exit();
  }
  if (invalidEmail($email) !== false) {
    header("location: ../register.php?error=invalidemail");
    exit();
  }
  if (emailExists($conn, $email) !== false) {
    header("location: ../register.php?error=emailtaken");
    exit();
  }
  if (pwdMatch($pwd, $pwdRepeat) !== false) {
    header("location: ../register.php?error=passwordsdontmatch");
    exit();
  }
  if (pwdLength($pwd) !== false) {
    header("location: ../register.php?error=passwordistooshort");
    exit();
  }
  if (cityUpperCase($city) !== false) {
    header("location: ../register.php?error=cityhasnouppercase");
    exit();
  }

  createUser($conn, $name, $email, $pwd, $phone, $address, $city, $value);

} else {
  header("location: ../register.php");
  exit();
}

I also have a functions.inc.php containing the called functions in signup.inc.php.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here i have made a VERY SIMPLE outline how to do this and some sample code (not tested and posibly full of typos.. its just the concept no working code

Show the form if nothing is submitted or an error happens.

IT IS NOT user friendly to only display one error at a time - show all arrors so the user can fix them all at a time (your former script only sjowed one error before redirect so collect all errors in one string array objecht - what you like and display them together.

ask if there have been any error and if not and if all checks wer ok then do the insert or show the error(s)

check the insert

if nothing has been submitted or an error occured show the form.

echo the sumbitted values into the form - probably better to sanitize the values and not put the $_POST values into it eg by

(NULL COALESCING OPERATOR ?? if the value before ?? is NULL then use '' othersise the value before the ?? )
https://www.tutorialspoint.com/php7/php7_coalescing_operator.htm

  <?php
    
    if (isset($_POST["submit"])) {
    
      $name = $_POST["name"];
      $email = $_POST["email"];
      $pwd = $_POST["pwd"];
      $pwdRepeat = $_POST["pwdrepeat"];
      $phone = $_POST["phone"];
      $address = $_POST["address"];
      $city = $_POST["city"];
      $value = $_POST['usertype'];
    
      require_once 'dbh.inc.php';
      require_once 'functions.inc.php';
    
    error="";
    
      if (emptyInputSignup($name, $email, $pwd, $pwdRepeat, $phone, $address, $city) !== false) {
        $error .= $emptyinput."<br>";
        ;
      }
      if (invalidEmail($email) !== false) {
         $error .= $invalidemail."<br>";
             }
      }
[all other sanitations  and checks ]

    
    if (empty($error))
{ $result=createUser($conn, $name, $email, $pwd, $phone, $address, $city, $value);
 }
else{
echo $error; 
}   
 
if ($result==TRUE)  
{
exit(); // to prevent the display of the form // or do if condition below. 
}

   } 



    ?>
    
    <!--  other HTML necessary code  -->
    
    <form class="form" id="form" action="" method="post">
    <label>Full name</label>
    <input type="text" placeholder="Enter full name..." name="name" value="<?= $_POST['name']??'';?>">
    
    [... all the other fields....   ]

    <button type="submit" id="registerbtn" name="submit" value="submit" >Register</button>
    </form>
    
 <!--  other HTML necessary code  -->
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...