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 -->
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…