如何在提交时重置/清除/刷新表单

时间:2016-06-10 02:09:52

标签: javascript php jquery mysql ajax

我无法在提交后重置来自。目前我依靠自动刷新页面。我从来没有使用过PHP,但我设法破解了一个用MySQL数据库设置的php表单。

此表单隐藏在div中,可在可见性中切换。因此,网页就像一个布告板,表格在同一页面上。

我使用了JQuery函数来重置表单。但目前div仍显示回声。

$(document).ready(function() {
  $('submit').click(function() {
    $('submission')[0].reset(); 
    });
});

我目前的设置如下:      

        if(! $conn ) {
           die('Could not connect: ' . mysql_error());
        }

        if(! get_magic_quotes_gpc() ) {
           $name = addslashes ($_POST['name']);
           $proposal = addslashes ($_POST['proposal']);
        }else {
           $name = $_POST['name'];
           $proposal = $_POST['proposal'];
        }

       $email = $_POST['email'];

        $sql = "INSERT INTO mvmv3". "(name, proposal, email, join_date ) 
            VALUES('$name','$proposal','$email', NOW())";

        mysql_select_db('mvmv_db');
        $retval = mysql_query( $sql, $conn );

        if(! $retval ) {
           die('Could not enter data: ' . mysql_error());
        }

        echo "Entered data successfully\n";

        mysql_close($conn);
     }else {
        ?>


<form name="submission" method = "post" action = "<?php $_PHP_SELF ?>" >

      <fieldset>
         <input name = "name" type = "text" 
                       id = "name" required autocomplete="off">
         <input name = "email" type = "text" 
                       id = "email" autocomplete="off">
         <textarea name = "proposal" type = "textarea" size="100"cols="40" rows="20"
                       id = "proposal" placeholder="Your proposal goes here..." required autocomplete="off"></textarea>


      </fieldset> 

      <fieldset> 
         <input name = "add" type = "submit" id = "add" value = "Submit"> 
      </fieldset>

 </form>
    <?php
        }
    ?>   

最好的方法是什么?我可能会让回声在4秒后消失吗?

2 个答案:

答案 0 :(得分:0)

if(! $conn ) {
       die('Could not connect: ' . mysql_error());
    }

    if(! get_magic_quotes_gpc() ) {
       $name      = addslashes ($_POST['name']);
       $proposal  = addslashes ($_POST['proposal']);
    }else {
       $name      = $_POST['name'];
       $proposal  = $_POST['proposal'];
    }

   $email         = $_POST['email'];

    $sql          = "INSERT INTO mvmv3". "(name, proposal, email, join_date ) 
        VALUES('$name','$proposal','$email', NOW())";

    mysql_select_db('mvmv_db');
    $retval = mysql_query( $sql, $conn );

    if(! $retval ) {
       die('Could not enter data: ' . mysql_error());
    }

    // WRAP THE "ECHOED" OUTPUT IN A DIV ELEMENT (WITH CLASS &/OR ID) 
    // SO YOU CAN EASILY REFERENCE IT IN JS
    echo "<div class='msg-box' id='msg-box'>Entered data successfully</div>\n";

    mysql_close($conn);
 }else {
 }

<强> JAVASCRIPT

    $(document).ready(function() {

        $('submit').click(function() {
          $('submission')[0].reset(); 

            // FADE-OUT THE DIV 3 SECONDS AFTER CLICKING THE BUTTON USING window.setTimeout... 
            // THIS ASSUMES THAT YOUR FORM IS NOT SUBMITTING NORMALLY (AJAX OR SO)
            /*
            setTimeout(
                function(){ 
                    $("#msg-box").fadeOut(500); 
                }, 
            3000);
            */
          });


        // FADE-OUT THE DIV 3 SECONDS AFTER PROCESSING THE FORM-DATA USING window.setTimeout... 
        // THIS ASSUMES THAT YOUR FORM HAS SUBMITTED NORMALLY (VIA POST OR GET)
        // AND THE MESSAGE IS DISPLAYED BY PHP AFTER PROCESSING...
        setTimeout(
            function(){ 
                $("#msg-box").fadeOut(500); 
            }, 
        3000);
    });

答案 1 :(得分:0)

使用类似

的内容
      $('input').val('');

清除所有输入字段