【问题标题】:How to clear form data, after submit, but after data passed提交后如何清除表单数据,但在数据传递后
【发布时间】:2016-03-20 12:50:03
【问题描述】:

我一直在寻找超过 2 天的解决方案来解决我的这个问题。浏览了堆栈上的所有相关帖子,但没有运气。我有一个接受数据的表单,然后传递给一个 php 程序来处理它,该程序又调用一个 html 感谢页面。我对 Javascript 或 JQuery 一点都不熟悉,但花一些时间后可以让它工作。我在这里发布代码以及许多我试图无济于事的建议。感谢任何结束我痛苦的帮助。谢谢。

HTML

<form method='post' action='contact-form-proc.php' name='contactform' id='contactform' onsubmit="setTimeout('clear_form()',200);return true">
    <p>
    <label for='fullname'>Your Name:</label><br/>
    <input type='text' name='fullname' />
    </p>

    <p>
    <label for='email' >Email Address:</label><br/>
    <input type='text' name='email' />
    </p>

    <p>
        <label for='mobile' >Mobile:</label><br/>
        <input type='text' name='mobile' />
    </p>
    <p>
        <label for='checkIndate' >Check in Date:</label><br/>
        <input type='text' name='checkIndate'/>
    </p>
    <p>
        <label for='comment' >Comment:</label><br/>
        <textarea name='comment' cols='25' rows='3'></textarea>
    </p>

    <input type="image" src="submit.gif" name="submit"/>
    <!--<input type='submit' name='submit' value='' id="submit" />-->
    <input type="hidden" name="submit" value="Submit"/>

</form>

PHP

<?php

if(empty($_POST['submit']))
{
    echo "Form is not submitted!";
    exit;
}
if(empty($_POST["fullname"]) ||
    empty($_POST["email"]))
    {
        echo "Please fill the form";
        exit;
    }

$name = $_POST["fullname"];
$email = $_POST["email"];
$mobile = $_POST["mobile"];
$checkindate = $_POST["checkIndate"];
$comment = $_POST["comment"];

mail( 'xxx@xxxxxx.com' , 'New form submission' , "New form submission: Name: $name, Email:$email, Mobile: $mobile, CheckIndate:$checkIndate, Comment:$comment"  );

header('Location: thank-you.html');


?>

这些是我尝试过的:

<script type="text/javascript">
       /*$('#contactform').trigger("reset");*/
       function clear_form() {
      /*    document.contactform.reset();

                document.getElementById("contactform").reset();*/
    /*$(window).load(function() {
        $('#contactform input[type="text"]').val('');*/


        $(window).load(function() {
                $('#contactform').children('input').val('');
                }

        });
                }
  </script>

【问题讨论】:

    标签: html forms submit reset


    【解决方案1】:

    非常感谢您抽出时间帮助我和其他所有人解决这个奇怪的问题。

    我找到了解决方案。我保持一切原样,另外,

    一个。编写了一个函数来重置表单,并且, 湾。将该函数添加到 body 元素的 onload 和 onunload 处理程序。

    如下:

    <script>
        function clearForms()
        {
          var i;
          for (i = 0; (i < document.forms.length); i++) {
            document.forms[i].reset();
          }
        }
    </script>
    
    <body onload="clearForms()" onunload="clearForms()">
    

    希望它能帮助并阻止像我这样的许多人浪费宝贵的时间和时间。问候。

    【讨论】:

      【解决方案2】:

      试试这个。

      <script type="text/javascript">
        $(document).ready(function(){
              $('input').val('');
          });
      </script>
      

      最后你不必调用 onsubmit 操作,document.ready 就足够了。

      【讨论】:

      • 它可以清除表单数据,我什至添加了 textarea 字段,这也很有效。但是表单没有被提交..!!!!不过还是谢谢。
      • 您的问题解决了吗?您是否从 html 中删除了此操作? onsubmit="setTimeout('clear_form()',200);return true" 我将所有代码复制到我的语言环境存储中,一切正常。
      • 另外我的更改不会影响表单提交,所以请给我更多信息以帮助您解决问题。
      • 我忽略了这一点。但是现在,即使删除了 onsubmit 操作,表单也没有被提交,但是数据被清除了。我之前提供的所有信息就是全部。只需为标签添加一些 CSS 样式,并且我正在使用图像作为提交按钮。还有一些验证,以便字段不保持为空。就是这样。
      猜你喜欢
      • 2014-02-18
      • 2021-09-24
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 2012-12-02
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多