0

I want to remember the value of a specific input after the submit button is clicked? Purpose is if during the user registers an account and encounters errors like email exist or password do not match i want to retain the values like name, age and etc. as of the moment here's my code, what should I do? Any help or suggestion? thanks

<?php
        session_start();
        $errflag = false;
        $errmsg = array();
        if(isset($_SESSION['errmsg'])&&is_array($_SESSION['errmsg'])&&count($_SESSION['errmsg'])>0){
            foreach($_SESSION['errmsg'] as $msg){
                echo $msg;
            }
            unset($_SESSION['errmsg']);
        }
        ?>
        <form method="post">
            <table>
            <tr>
                <td>Last Name:</td><td><input id="txtfield" type="text" name="lname" autofocus="autofocus" required="required"></td>
            </tr>
            <tr>
                <td>First Name:</td><td><input id="txtfield" type="text" name="fname" required="required"></td>
            </tr>
            <tr>
                <td>E-mail:</td><td><input id="txtfield" type="email" name="email" required="required"></td>
            </tr>
            <tr>
                <td>Password:</td><td><input id="txtfield" type="password" name="pass" pattern=".{6,}" required="required"></td>
            </tr>
            <tr>
                <td>Re-type:</td><td><input id="txtfield" type="password" name="rpass" pattern=".{6,}" required="required"></td>
            </tr>
            <tr>
                <td></td><td><input id="btn" type="submit" name="register" value="Register"></td>
            </tr>
            </table>
        </form>
        <?php
        include 'functions/functions.php';
        if(isset($_POST['register'])){
            $result=ValidateEmail($_POST['email']);
            if($result){
                $errmsg[] = '<p id="error"><img src="img/error.png" alt="error">This email address is already in use.</p>';
                $errflag = true;
            }
            if($_POST['pass']!=$_POST['rpass']){
                $errmsg[] = '<p id="error"><img src="img/error.png" alt="error">Passwords does not match.</p>';
                $errflag = true;
            }
            if($errflag){
                session_regenerate_id();
                $_SESSION['errmsg'] = $errmsg;
                session_write_close();
                $errflag = false;
                header('location: register.php');
                exit();
            }
            else {
                $user = array('lname'=>$_POST['lname'],'fname'=>$_POST['fname'],'email'=>$_POST['email'],'pass'=>$_POST['pass']);
                RegisterUser($user);
                session_regenerate_id();
                $errmsg[] = '<p id="success"><img src="img/success.png" alt="success">Your account is now active. You may now login.</p>';
                $_SESSION['errmsg'] = $errmsg;
                session_write_close();
                header('location: index.php');
                exit();
            }
        }
        ?>
3
  • Yes, as the answer below says, its best to send an AJAX request which will return some kind of verification. No need to "remember" values as you will not be changing pages, but rather sending out a request to another page via AJAX and if the response is true (or whatever you want to verify it as) then you can continue. Aug 25, 2012 at 6:18
  • any samples or link, since I'm not familiar in ajax yet :(
    – Sui Go
    Aug 25, 2012 at 6:20
  • @SuiGo have added an example in answer below, basic guide for ajax. Aug 25, 2012 at 6:22

3 Answers 3

1

Use ajax/ javascript to check if password do not match or username already exists. That way you would be querying database without actually changing the page.

So you have the fields filled remain there. Here's small example at w3schools

5
  • is there another way without using any ajax/js? just pure php ^^ thanks!
    – Sui Go
    Aug 25, 2012 at 6:18
  • You can use Sessions for this but it is highly not recommended. Another way would be to have the submit redirect to the same page with all the post variables. If the verification fails, put all the post variables back into the form. Aug 25, 2012 at 6:19
  • yes post back to same page, is another cleaner solution and check for var's Aug 25, 2012 at 6:21
  • then where should i put the post values?
    – Sui Go
    Aug 25, 2012 at 6:22
  • Put the post values back into your form. Ill change my answer to accommodate this Aug 25, 2012 at 6:25
0
 <form method="post">
     <input id="txtfield" type="text" name="lname" autofocus="autofocus" required="required" Value=<?php $lname ?>/>

already posts back to the same page. What you can do now is something like:

 $lname = ""; //and all the other fields
 $fname = "";
 if ($_POST['lname'] == "")
 {
    $fname = $_POST['fname'];//and all the other fields
    //put all the variables back into the form and display an error text
 }
 else
 {
    //Lets do the whole form processing and submit here
 } 

In other words, set all your values for your input fields using variables that are blank. If an error happens, set all those variables to their original value.

0
0

according to my understanding, you want the submitted variables to appear back again in the form in case of wrong or missing submission.

Replace the HTML code with this, it should do it:

    <form method="post">
        <table>
        <tr>
            <td>Last Name:</td><td><input id="txtfield" type="text" name="lname" value="<?=$_POST['lname'];?>" autofocus="autofocus" required="required"></td>
        </tr>
        <tr>
            <td>First Name:</td><td><input id="txtfield" type="text" name="fname" value="<?=$_POST['fname'];?>" required="required"></td>
        </tr>
        <tr>
            <td>E-mail:</td><td><input id="txtfield" type="email" name="email" value="<?=$_POST['email'];?>" required="required"></td>
        </tr>
        <tr>
            <td>Password:</td><td><input id="txtfield" type="password" name="pass" pattern=".{6,}" required="required"></td>
        </tr>
        <tr>
            <td>Re-type:</td><td><input id="txtfield" type="password" name="rpass" pattern=".{6,}" required="required"></td>
        </tr>
        <tr>
            <td></td><td><input id="btn" type="submit" name="register" value="Register"></td>
        </tr>
        </table>
    </form>
2
  • why this not work? in the textbox is shows <?=$_POST['lname'];?>
    – Sui Go
    Aug 25, 2012 at 8:00
  • seems to me you are using localhost. replace the values with these: <?php echo $_POST['lname'];?> , <?php echo $_POST['fname'];?>, <?php echo $_POST['email'];?> Aug 25, 2012 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.