php 成功提交后清除表单域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24338678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:17:09  来源:igfitidea点击:

Clear form fields after a successful submit

phphtmlforms

提问by user3756377

well im working on a small html form.

嗯,我正在处理一个小的 html 表单。

            <form class="contact" action="" method="POST">
                <label>Name : </label><input type="text" name="name" value="<? echo $name; ?>"/>
                <p class="middle"><label>Comment : </label><textarea name="message"></textarea><? echo $message; ?></p>
                <label class="captcha"><img src="captcha.php" style="line-height: 30px;"></label><input type="text" name="code"/>
                <input type="submit" class="csubmit" value="Now !" name="get"/>
            </form>

and this is the php code:

这是php代码:

<?php
    if (isset($_POST['get'])) {
    $error = "";
    if (!empty($_POST['name'])) {
    $name = $_POST['name'];

    } else {
    $error .= "no name. <br />";
    }


    if (!empty($_POST['message'])) {
    $message = $_POST['message'];
    } else {
    $error .= "no message <br />";
    }

    if(($_POST['code']) == $_SESSION['code']) { 
  $code = $_POST['code'];
  } else { 
  $error .= "wrong captcha <br />";    
}
    if (!empty($error)) {
      echo '<p class="error">Error :<br/>' . $error . '</p>';
      } elseif (!empty($success)) {
      echo $success;
      }

    if (empty($error)) {
        $message = mysql_real_escape_string($message);
        $name = mysql_real_escape_string($name);
        $id = mysql_real_escape_string($_GET['id']);
        $date = date("Y-m-d H:i:s");
   mysql_query("INSERT INTO comments(id, name, comment, time,approved)VALUES('$id', '$name', '$message', '$date', '0')");
   echo "thank you";
    }
    }
    ?>

As you can see i user $message and $name to keep informations after a submit with wrong captcha code, but the problem is that i want to clear those fields after a submit with correct informations. Can you please tell me how can i clear form fields after a succesfull submit ?

如您所见,我使用 $message 和 $name 在提交错误验证码后保留信息,但问题是我想在提交正确信息后清除这些字段。你能告诉我成功提交后如何清除表单字段吗?

回答by imbondbaby

You can use .reset() on your form.

您可以在表单上使用 .reset() 。

$("#form")[0].reset();

You could follow that with Javascript too

你也可以使用 Javascript

document.getElementById('form').reset();

Or, if successful, redirect the user back to your contact page:

或者,如果成功,将用户重定向回您的联系页面:

header("Location: contact.php"); // redirect back to your contact form
exit;

EDIT

编辑

<input type="submit" class="csubmit" value="Now !" name="get" onClick="clearform();" />

function clearform()
{
    document.getElementById("name").value=""; //don't forget to set the textbox ID
    document.getElementById("message").value=""; //don't forget to set the textbox ID
    document.getElementById("code").value=""; //don't forget to set the textbox ID
}

Also use:

还可以使用:

required="required"

so people will be required to fill out the input fields :)

所以人们将需要填写输入字段:)

Which by the way is the prefered method. If you keep the user in a page that was reached through a POST method, if he refreshes the page the form will be submitted again.

顺便说一下,这是首选方法。如果您将用户保留在通过 POST 方法访问的页面中,如果他刷新页面,表单将再次提交。