【问题标题】:Clear forms on successful submission成功提交后清除表格
【发布时间】:2016-06-07 16:56:22
【问题描述】:

我有一个注册表单,该表格将:

  1. 成功提交后清除表单
  2. 在提交错误时不清除表单(在我的情况下,密码不匹配)

这是我的代码:

<input name="uname" type="text" class="form-control" id="uname" placeholder="Username" value="<?php if (!empty($_POST["uname"])) { echo $_POST["uname"]; } else { echo ''; };  ?>" required>

此代码适用于在提交错误时不清除表单。虽然提交成功了,但是表单提交后表单仍然保留数据。因为,我正在使用这段代码,显然重置按钮也不起作用。那么,提交成功后如何清除呢?

无论如何,这是表单验证的代码:

if (isset($_POST['submit'])) {

              try {

                if ($_POST["pwd"] == $_POST["cpwd"]) {//matched passwords

                  $stmt = $conn->prepare("INSERT INTO tbl_customers(fld_customer_fname,
                    fld_customer_lname, fld_customer_gender, fld_customer_phone, fld_customer_email, fld_customer_username, fld_customer_password) VALUES(:fname, :lname,
                    :gender, :phone, :email, :uname, :pwd)");

                  $stmt->bindParam(':fname', $fname, PDO::PARAM_STR);
                  $stmt->bindParam(':lname', $lname, PDO::PARAM_STR);
                  $stmt->bindParam(':gender', $gender, PDO::PARAM_STR);
                  $stmt->bindParam(':phone', $phone, PDO::PARAM_STR);
                  $stmt->bindParam(':email', $email, PDO::PARAM_STR);
                  $stmt->bindParam(':uname', $uname, PDO::PARAM_STR);
                  $stmt->bindParam(':pwd', $pwd, PDO::PARAM_STR);

                  $fname = $_POST['fname'];
                  $lname = $_POST['lname'];
                  $gender =  $_POST['gender'];
                  $phone = $_POST['phone'];
                  $email = $_POST['email'];
                  $uname = $_POST['uname'];
                  $pwd = $_POST['pwd'];

                  $stmt->execute();
                  ?>
                  <br>
                  <div class="alert alert-success fade in">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                    <strong>Success!</strong> Account created.
                  </div>
                  <?php
                }
                else{//unmatched passwords
                  ?>
                  <br>
                  <div class="alert alert-danger fade in">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                    <strong>Error!</strong> Passwords do not match.
                  </div>
                  <?php
                }
              }

【问题讨论】:

  • 你会反对用javascript做吗?还是需要与 PHP 一起使用?
  • @NoReceipt4Panda 两者兼得会很棒吗?但我更喜欢它在 PHP 中。

标签: php html forms validation


【解决方案1】:

使用 Javascript 和 JQuery,可以使用如下命令:

$(".myform")[0].reset();

使用 PHP,您也可以在表单提交到同一页面(重新加载)后进行重定向:

header("location:".$_SERVER['PHP_SELF']);

在 PHP 中执行此操作的另一种方法是通过存储变量,如下所示:

if(isset($_POST['name']))
{
    $post_val=$_POST;
------------------------------------------------------------------------------------------

然后在提交后取消设置$post_val

unset($post_val);

通过$post_val 展示您的价值观

<input type="text" name="name"  value="<?php echo $_post_val["name"]?>"/>
<input type="text" name="email" value="<?php echo $_post_val["email"]?>"/>   
<input type="text" name="phone" value="<?php echo $_post_val["phone"]?>"/> 

希望这会有所帮助!

【讨论】:

  • 但是,如果我使用第二个选项来重定向页面,表单将不会显示 成功消息,因为它会刷新页面:/
  • 我明白了,很高兴我能提供帮助!
【解决方案2】:

我假设您在检查是否成功后呈现表单字段。如果我是对的,也将一些 $success 变量设置为 true

$success = true;

当你有成功或失败的地方。然后在渲染输入中做:

<input name="uname" type="text" class="form-control" id="uname" placeholder="Username" value="<?php if (!empty($_POST["uname"]) && !$success) { echo $_POST["uname"]; } ?>" required>

ps:你不需要else { echo ''; }; ;)

【讨论】:

  • if ($_POST["pwd"] == $_POST["cpwd"]) {...... $success = true;} else {..... $success = false;}
  • 啊,好吧,我有点像这样设置$success = "true";我的错。它有效。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多