0

我不确定实现这一目标的最佳方法是什么。如果用户想要发送另一个文本,我正在尝试在我的 PHP 表单上创建输入按钮以清除表单。目前我添加了一个事件 onClick="history.go(-1);return (true);" 但表格仍然显示为已填满。无论如何通过向按钮添加事件来实现这一点。

    echo "<div id='success_page_apps'>"; 
    echo "<h1>Email Sent Successfully.</h1>";
    echo "<p>Thank you, your message has been submitted to us.</p>";
    echo "</div>";
    echo "<input type="button" value="Send Another" onClick="history.go(-1); return (true); ">";

否则我应该更改其余代码的某些内容:

    $error    = '';
    $email    = ''; 
    $comments = ''; 


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

    $email    = $_POST['email'];
    $comments = $_POST['comments'];
    $app =  $_SERVER["REQUEST_URI"];;

        if(trim($comments) == '') {
        $error = '<div class="error_message">Attention! Please enter your message.</div>';
    } else if(trim($email) == '') {
        $error = '<div class="error_message">Attention! Please enter a valid email address.</div>';
    } else if(!isEmail($email)) {
        $error = '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>';
    }

    if($error == '') {

        if(get_magic_quotes_gpc()) {
            $comments = stripslashes($comments);
        }

$address = "email@myemail.com";
$e_subject = 'You\'ve been contacted from an app web page ' . $name . '.';
$e_body = "You have been contacted using the app comments box on the above app web page, their additional message is as follows.\r\n\n";

$e_content = "\"$comments\"\r\n\n";

         $e_reply = "$name $email";

     $msg = $e_body . $e_content . $e_reply;

     mail($address, $e_subject, $msg, $app, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");

    echo "<div id='success_page_apps'>"; 
    echo "<h1>Email Sent Successfully.</h1>";
    echo "<p>Thank you, your message has been submitted to us.</p>";
    echo "</div>";
    echo '<input type="button" value="Send Another" onClick="history.go(-1); return (true); ">';
    }

}

  if(!isset($_POST['contactus']) || $error != '') // Do not edit.
     {
?>
        <?php echo $error; ?>

        <fieldset id="contact_apps">
        <br />
        <form  method="post" action="#ContactForm">
        <label for="email" accesskey="E"><span class="required"></span> Email</label>

        <input name="email" type="text" id="email" size="33" value="<?php echo$email;?>" />

        <textarea name="comments" cols="50" rows="15"  id="comments"><?php echo$comments;?></textarea>
        <input name="contactus" type="submit" class="send" id="contactus" value="Submit">

        </form>

        </fieldset>



<?php } 

更新

非常感谢 Edwin Alex 和 Bojangles 将我指向 jQuery 来解决问题。

我通过在标签后的 php 页面底部添加以下内容来解决问题。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>

<script> 
$(document).ready(function(){
 $("#email").val('');
  $("#comments").val('');
});
4

2 回答 2

1

您可以使用 :input 选择器来实现这一点。

$(":input").val('');

这条语句将清除表单字段中的所有值。

于 2013-01-28T10:50:07.937 回答
0

您应该始终使用 header("Location: ..."); 将用户重定向到新页面。否则,post 值将保留在内存中,页面刷新将重新发送它们。

于 2013-01-28T11:11:23.790 回答