简体   繁体   English

我想记住单击“提交”按钮后特定输入的值吗?

[英]I want to remember the value of a specific input after the submit button is clicked?

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

Use ajax/ javascript to check if password do not match or username already exists. 使用ajax / javascript检查密码是否不匹配或用户名已存在。 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 这是w3schools的一个小例子

 <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. 如果发生错误,请将所有这些变量设置为原始值。

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: 以此替换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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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