简体   繁体   中英

Reloading registration form on failed validation

I have a registration form view stored in my database. User clicks on link to show registration form. form looks like so:

<form method="post" id="loginForm" action = ".">
                        <label>Name:<input type="text" name="name" value="" required><br></label>
                        <label>Email:<input type="email" name="email" value="" required><br></label>
                        <label>Password:<input type="password" name="pass" value="" required><br></label>


                        <input type="submit" value="Log In"/><br>
                        <input type="hidden" name="action" value="do_register"/>
                    </form> 

controller called index.php grabs the do_register value from the action and proceeds to process validation.

if($action == 'do_register'){
 $name = $_POST['name'];
 $email = $_POST['email'];
 $pass = $_POST['pass'];

 if(empty($email) || empty($pass) || empty($name)){
 $error = 'All fields required';
 $view = 'view.php';
 }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
 $error = "Please enter a valid email.";
 $view = "view.php";

 }else{
 $user = insertUser($name, $email, $pass);

 if(!$user){
 $error = "There was an error. Please try again";
 $view = "view.php";

 }else{
$error = "Thank you for registering!";
$view = 'view.php';
 }
 }
 }

When it reloads the view it just shows my error message and does not show the registration form to try again. Help is very much appreciated.

It is because you reloading the page. There is batter practice to use in session.

use $_SESSION["error"] instead of $error .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM