Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何避免在一个字段出错时要求用户重新填充所有表单字段_Php_Sql_Forms - Fatal编程技术网

Php 如何避免在一个字段出错时要求用户重新填充所有表单字段

Php 如何避免在一个字段出错时要求用户重新填充所有表单字段,php,sql,forms,Php,Sql,Forms,php新手,非常感谢您在以下方面的帮助: 我制作了一个注册表单脚本,通过多个错误处理程序验证用户的输入。检查有效电子邮件、pwd和pwd返回匹配等 如果用户填写了所有字段,但其中一个字段出现错误,无法通过错误处理程序,我希望将其他字段发送回同一表单,这样用户就不必重新填写,因为我又重新填写了。因此,如果密码不匹配,我想重新加载同一页,但另一个字段已填充 我可以在页面的url中看到信息,但在表单字段中看不到。 下面是我的表单HTML <div class="register-content"

php新手,非常感谢您在以下方面的帮助:

我制作了一个注册表单脚本,通过多个错误处理程序验证用户的输入。检查有效电子邮件、pwd和pwd返回匹配等

如果用户填写了所有字段,但其中一个字段出现错误,无法通过错误处理程序,我希望将其他字段发送回同一表单,这样用户就不必重新填写,因为我又重新填写了。因此,如果密码不匹配,我想重新加载同一页,但另一个字段已填充

我可以在页面的url中看到信息,但在表单字段中看不到。 下面是我的表单HTML

<div class="register-content">
                <form action="includes/signup.inc.php" method="POST" class="margin-bottom-0">
                    <label class="control-label">Name <span class="text-danger">*</span></label>
                    <div class="row row-space-10">
                        <div class="col-md-6 m-b-15">
                            <input type="text" name="uFirst" class="form-control" placeholder="First name" required />
                        </div>
                        <div class="col-md-6 m-b-15">
                            <input type="text" name="uLast" class="form-control" placeholder="Last name" required />
                        </div>
                    </div>

                    <label class="control-label">Username <span class="text-danger">*</span></label>
                    <div class="row m-b-15">
                        <div class="col-md-12">
                            <input type="text" name="uName" class="form-control" placeholder="Username" required />
                        </div>
                    </div>

                    <label class="control-label">Email <span class="text-danger">*</span></label>
                    <div class="row m-b-15">
                        <div class="col-md-12">
                            <input type="text" name="mail" class="form-control" placeholder="Email address" required />
                        </div>
                    </div>


                    <label class="control-label">Password <span class="text-danger">*</span></label>
                    <div class="row m-b-15">
                        <div class="col-md-12">
                            <input type="password" name="pwd" class="form-control" placeholder="Password" required />
                        </div>
                    </div>

                    <label class="control-label">Re-enter Password <span class="text-danger">*</span></label>
                    <div class="row m-b-15">
                        <div class="col-md-12">
                            <input type="password" name="pwd-repeat" class="form-control" placeholder="Re-enter Password" required />
                        </div>
                    </div>
下面是我的php代码

<?php if(isset($_POST['signup-submit'])) {

  require 'dbh.inc.php';

  $firstName=$_POST['uFirst'];
  $lastName=$_POST['uLast'];
  $userName=$_POST['uName'];
  $email=$_POST['mail'];
  $password=$_POST['pwd'];
  $passwordRepeat=$_POST['pwd-repeat'];

  if (!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $username)) {
    header("location: ../signup.php?error=invalidemail&uid&uFirst=".$firstName."&uLast=".$lastName);
    exit();

  } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    header("location: ../signup.php?error=invalidemail&uFirst=".$firstName."&uLast=".$lastName."&uName=".$userName);
    exit();

  } else if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
    header("location: ../signup.php?error=invaliduid&uFirst=".$firstName."&uLast=".$lastName."&mail=".$email);
    exit();

  } else if ($password !== $passwordRepeat) {
    header("location: ../signup.php?error=passwordnotmatching&uFirst=".$firstName."&uLast=".$lastName."&uName=".$userName."&mail=".$email);
    exit();

有几种方法可以解决这个问题。最终,有两个高级选项:

将有效值传递回新表单 切勿删除有效值i.e。JavaScript+AJAX 使用当前设置,1将更简单。要使用当前设计,您需要将值存储在某个位置,以传递回要渲染的窗体。最简单的方法是将它们添加到URL参数字符串中,但其他选项包括cookie或会话存储

最简单的选择是将表单和验证合并到一个端点中,而不是将它们分开。这样,在呈现带有错误消息的表单时,您已经拥有post数据

一旦有了这些值,就可以简单地将它们插入带有值属性的表单HTML中。确保HTML对任何用户输入值进行编码,以避免XSS漏洞

例如:


根据您的代码,您只需调用输入框的GET value。