一旦出现错误,数据将从输入字段中丢失

时间:2015-10-16 12:32:34

标签: javascript php html

<html>

    <head>
        <title> Register Page </title>
    </head>
    <body>
        <?php
            session_start();
            $fnameerr = "";
            $lnameerr = "";
            $passworderr = "";
            $emailerr = "";
            $usererr = "";
            $passmatcherr = "";

            if(isset($_POST['submit'])){
                if(empty($_POST['fname'])){
                    $fnameerr = "* Please Enter The First Name";
                }

                if(empty($_POST['lname'])){
                    $lnameerr = "* Please Enter The Last Name";
                }

                if(empty($_POST['username'])){
                    $usererr = "* Please Enter The UserName";
                }

                if(empty($_POST['password'])){
                    $passworderr = "* Please Enter The UserName";
                }

                if($_POST['password'] != $_POST['repassword']){
                    $passmatcherr = "* Password do not match";
                }

                if(empty($_POST['email'])){
                    $emailerr = "* Please Enter the Email Id ";
                }

            }
        ?>
            <h1>Please Fill In The Details</h1><hr>
            <form action="" method=POST >
            <lable> First Name : </lable> <input type=text name = fname placeholder=First Name /><?php echo $fnameerr ?><br><br>
            <lable> Last Name : </lable> <input type=text name = lname  placeholder = Last Name /><?php echo $lnameerr ?><br><br>
            <lable> Username : </lable> <input type=text name = username placeholder=Username /><?php echo $usererr ?><br><br>
            <lable> Password : </lable> <input type=password name = password placeholder=Password /><?php echo $passworderr ?><br><br>
            <lable> Re Password : </lable> <input type=password name = repassword placeholder='Re Password' /><?php echo $passmatcherr ?><br><br>
            <lable> E-Mail Id : </lable> <input type=text name = email placeholder=xyz@hotmail.com /><?php echo $emailerr ?><br><br>
            <lable> Area of Intrest : </lable> <select name=subject >
                                                <option value=maths > Maths </option>
                                                <option value=social > Social </option>
                                                <option value=english > English </option>
                                                <option value=science > Science </option>
                                                </select> <br><br>
            <lable> Want To Be A Instructor </lable> <input type=checkbox /> <br><br>
            <input type = submit name = submit value = Register />
            </form>
    </body>
</html>

我希望数据保留在文本字段中,但所有字段中的数据都丢失了,我该怎么办?实际上我做的是,我检查了所有输入字段是否都已设置,并检查重新输入的密码是否匹配。

2 个答案:

答案 0 :(得分:0)

您必须检查每个值:

$fname = isset($_POST["fname"]) ? $_POST["fname"] : '';

然后,在每个相应的输入中,添加“value”属性:

<input type="text" name="fname" placeholder="First Name" value="<?php echo $fname ?>" />

答案 1 :(得分:0)

  

在会话中存储值

试试这段代码: -

if(isset($_POST['submit'])) //use this code inside this if
    $_SESSION['data']=$_POST;
    print "<pre>";
    print_r($_SESSION['data']);
    print "</pre>";
  

并显示那样的价值

<input type=text value="<?php echo $_SESSION['data']['fname']; ?>" name = fname placeholder=First Name />