清除php表单以发送另一封电子邮件

时间:2013-01-28 10:45:19

标签: php forms clear

我不确定实现这一目标的最佳方法是什么。如果用户想要发送另一个文本,我试图在我的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('');
});

2 个答案:

答案 0 :(得分:1)

您可以使用:输入选择器来实现此目的。

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

此单个语句将清除表单字段中的所有值。

答案 1 :(得分:0)

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