hey guys, so i have already completed the form and the validation. what i wanna know is how to retain the values that user has already types into the text boxes.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;" charset="iso-8859-1;">
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen">
</head>
<body>
<center><img class="banner" src="image/banner.jpg"></center>
<div class="button">
<div class="bcontent">
    <ul class="menu">
    <li class="nvg"><a class="nav" href="index.php">Home</a></li>
    <li class="nvg"><a class="nav" href="">About</a></li>
    </ul>
</div>
</div>
<center>
<div class="slabel"><center><font face="Malgun Gothic" size="5" color="white"><b><label>Sign Up</label></b></font></center></div>
<div class="fsignup"><form name="register" action="register.php" method="POST">
    <table style="margin-left:10px; padding-top:10px;">
    <tr><td><font color="White" face="Malgun Gothic"><b>Full Name :</b></td>
    <td><input  type="text" maxlength="50" size="30" name="name" placeholder="Noreen Nong Musa" value="<?=@$name?>" /><br>
    </tr>
    <tr>
    <td  style="padding-top:10px;"><font color="White" face="Malgun Gothic"><b>Email :</font></b></td>
    <td  style="padding-top:10px;"><input  type="text" maxlength="30" size="20" name="email" placeholder="example@example.com" value="<?=@$email ?>" /></td>
    </tr>
    <tr>
    <td  style="padding-top:10px;"><font color="White" face="Malgun Gothic"><b>Password :</font></b></td>
    <td  style="padding-top:10px;"><input  type="password" maxlength="10" size="20" name="password" placeholder="Password" required /><br></td>
    </tr>
    <tr>
    <td  style="padding-top:10px;"><font color="White" face="Malgun Gothic"><b>Confirm Password :</font></b></td>
    <td  style="padding-top:10px;"><input  type="password" maxlength="10" size="20" name="cpassword" placeholder="Confirm Password" required /><br></td>
    </tr>
    </table>
<div style="text-align:center; margin-top:50px;"> 
<input type="submit" class="signup" value="Sign Up" name="submit">
</div>
</form></div>
</center>
</body>
</html>

<?php

if (isset($_POST['submit']))
{
    $name = mysql_real_escape_string($_POST['name']);
    $email = mysql_real_escape_string($_POST['email']);
    $password = mysql_real_escape_string($_POST['password']);
    $cpassword = mysql_real_escape_string($_POST['cpassword']);

    if(strlen($name)>10)
    {
        require "connect.php";
        $checkname=@mysql_query("SELECT `name` FROM `register` WHERE `name` = '$name' LIMIT 1") or die(mysql_error());

        if(mysql_num_rows($checkname)>0)
        {
            echo ('<script>alert("Name already exists")</script>');
        }
        else
        {
            if(strlen($email)<55)
            {
                require "connect.php";
                $checkemail=@mysql_query("SELECT `email` FROM `register` WHERE `email` = '$email' LIMIT 1") or die(mysql_error());

                if(mysql_num_rows($checkemail)>0)
                {
                    echo ('<script>alert("Email already exists")</script>');
                }
                else
                {

                    if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email))
                    {
                        if(preg_match('/^(?=.*\d).{4,10}$/', $password))
                        {
                            if($password == $cpassword)
                            {
                                require "connect.php";

                                $query= mysql_query("INSERT INTO register(name,email,password) VALUES ('$name','$email','$password')");
                                if($query)
                                {
                                    echo ('<script type="text/javascript">alert("Registration is Successful");
                                    window.location = "index.php"</script>');
                                }
                                else
                                {
                                    echo ('<script type="text/javascript">alert("Registration is Unsuccessful");
                                    window.location = "register.php"</script>');
                                }
                            }
                            else
                            {
                                echo "Passwords do not match!";
                            }
                        }
                        else
                        {
                            echo "Password must be between 4 and 8 digits long and include at least one numeric digit.";
                        }
                    }
                    else
                    {
                        echo "Email address is invalid";
                    }
                }
            }
            else
            {
                echo "Email address is too long";
            }
        }
    }
    else
    {
        echo "Name is too short";
    }
}


exit();
?>

for example in the email field, the email given by user is invalid so the error message will appear but the name field would still be retained with the name given by user. how do i accomplish that? as u can see ive tried using <?=@$name?> and also <?php echo $name?> but those didnt work. And i dont wanna use javascript because well what if the user has javascrit turned off then no validation will happen and also im keepping to languages i know coz my deadline is tmr. This validation part where the values are retained is just a last minute addition. thanks in advance for any help!

Recommended Answers

All 7 Replies

I think you should maybe use <?= $_POST['name']; ?> instead of just <?= $name; ?>, as the $name variable hasn't been set yet when PHP arrives at the <form> part.

ive changed my code to the one u have suggested and got:

<br /><b>Notice</b>: Undefined variable: name in <b>C:\xampp\htdocs\CashFlow\register.php</b> on line <b>22</b><br /><br />

That's because your error reporting is set to also show notices. $_POST['name'] has not yet been set when you first load the page, thus you get the notice. You can get rid of it by putting this line at the top of your script:

error_reporting(E_ALL ^ E_NOTICE);

Does it display the name correctly though, after you submit the form?

id added error_reporting(E_ALL ^ E_NOTICE); but the notice is still there. i added '@' in front of $_POST and the notice is gone.

I think i didn't explain myself properly. See if say the errors for incorrect password appears i would like to have the name field and email field to still retain wtv input the user has filled in. or if the error for invalid email address appears then only the name field value is retained.
because what i have right now is that the fields would be automatically emptied if an error appears. Its not that i want the values to be retained AFTER submit, just during validation process. is that possible using php? i know this can be achieved using javascript but i dont want to use javascript.

Could you try and replace your HTML part with the following?

<?php
error_reporting(E_ALL ^ E_NOTICE);
?>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;" charset="iso-8859-1;">
    <link rel="stylesheet" type="text/css" href="css/style.css" media="screen">
</head>
<body>
    <center>
        <img class="banner" src="image/banner.jpg"></center>
        <div class="button">
        <div class="bcontent">
        <ul class="menu">
        <li class="nvg"><a class="nav" href="index.php">Home</a></li>
        <li class="nvg"><a class="nav" href="">About</a></li>
        </ul>
        </div>
        </div>
        <center>
        <div class="slabel"><center><font face="Malgun Gothic" size="5" color="white"><b><label>Sign Up</label></b></font></center></div>
        <div class="fsignup">
        <form name="register" action="register.php" method="POST">
            <table style="margin-left:10px; padding-top:10px;">
            <tr><td><font color="White" face="Malgun Gothic"><b>Full Name :</b></td>
            <td><input type="text" maxlength="50" size="30" name="name" placeholder="Noreen Nong Musa" value="<?php echo $_POST['name']; ?>" /><br>
            </tr>
            <tr>
            <td style="padding-top:10px;"><font color="White" face="Malgun Gothic"><b>Email :</font></b></td>
            <td style="padding-top:10px;"><input type="text" maxlength="30" size="20" name="email" placeholder="example@example.com" value="<?php echo $_POST['email']; ?>" /></td>
            </tr>
            <tr>
            <td style="padding-top:10px;"><font color="White" face="Malgun Gothic"><b>Password :</font></b></td>
            <td style="padding-top:10px;"><input type="password" maxlength="10" size="20" name="password" placeholder="Password" required /><br></td>
            </tr>
            <tr>
            <td style="padding-top:10px;"><font color="White" face="Malgun Gothic"><b>Confirm Password :</font></b></td>
            <td style="padding-top:10px;"><input type="password" maxlength="10" size="20" name="cpassword" placeholder="Confirm Password" required /><br></td>
            </tr>
            </table>
            <div style="text-align:center; margin-top:50px;">
            <input type="submit" class="signup" value="Sign Up" name="submit">
            </div>
        </form>
        </div>
    </center>
</body>
</html>

okay, i have replaced. the notice error is no longer there. though the values still automatically disappear if the input is wrong.

That is weird, because if the form gets submitted, it is reloaded and $_POST vars should have been filled with the post data.

Could you try to place a print_r($_POST); before the HTML, and see if it gets filled correctly after a form submission?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.