0

单击提交按钮后,我想记住特定输入的值吗?目的是如果在用户注册帐户期间遇到诸如电子邮件存在或密码不匹配之类的错误,我想保留姓名、年龄等值。到目前为止,这是我的代码,我该怎么办?有什么帮助或建议吗?谢谢

<?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();
            }
        }
        ?>
4

3 回答 3

1

使用 ajax/javascript 检查密码是否不匹配或用户名是否已存在。这样您就可以在不实际更改页面的情况下查询数据库。

因此,您将填写的字段保留在那里。这是w3schools的一个小例子

于 2012-08-25T06:16:07.003 回答
0
 <form method="post">
     <input id="txtfield" type="text" name="lname" autofocus="autofocus" required="required" Value=<?php $lname ?>/>

已经发回同一页面。你现在可以做的是:

 $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
 } 

换句话说,使用空白变量为输入字段设置所有值。如果发生错误,请将所有这些变量设置为其原始值。

于 2012-08-25T06:23:27.123 回答
0

根据我的理解,您希望提交的变量在提交错误或丢失的情况下再次出现在表单中。

用这个替换 HTML 代码,它应该这样做:

    <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>
于 2012-08-25T06:27:12.957 回答