【发布时间】:2014-12-23 00:26:22
【问题描述】:
我找不到任何人问类似的问题 - 我建立了一个简单的联系表单,当我单击提交时,它会将我带到编写用于发送消息的 php 的页面。我想知道如何才能避免这种情况发生,而只是在提交按钮下方显示“谢谢”消息,或类似的东西。
我的代码基于本教程。 http://www.freecontactform.com/html_form.php。表单发送电子邮件很好,但我只是不希望它在提交后重定向到另一个页面。
<form name="helpful-form" method="post" action="../helpful-form.php">
<input type="radio" name="unhelpful" value="The Page Did Not Answer My Questions">Did Not Answer My Questions</input><br>
<input type="radio" name="unhelpful" value="The Page Content Was Not What I Was Expecting"><br>
<input type="radio" name="unhelpful" value="There Was Too Much Information On The Page"><br>
<input type="radio" name="unhelpful" value="There Was Too Little Information On The Page"<br>
<input type="radio" name="unhelpful" value="The Page Content Was Too Complicated"><br>
<textarea rows="3" cols="30" name="recommendations"></textarea>
<input type="text" name="email">Email: (optional) </input>
<input type="submit" value="submit" name="submit">
</form>
helpful-form.php
<?php
$email_to = "*******************";
$email_subject = "Website Recommendation/Question - Form Submission";
function died($error) {
echo ($error);
die();
}
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
if (!isset($_POST['unhelpful']) && !isset($_POST['recommendations'])) {
died('Please Select A Reason Why This Page Is Unhelpful.');
}
$unhelpful = $_POST['unhelpful'];
$recommendations = $_POST['recommendations'];
$email = $_POST['email'];
$email_message = "A new website recommendation and/or question has been submitted from BestAttorney.com:\n";
$email_message .= "This page was not helpful because: ".clean_string($unhelpful)."\n";
$email_message .= "Other Comments from the User: ".clean_string($recommendations)."\n";
$email_message .= "This Form Was Submitted by: ".clean_string($email)."\n";
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you giving us your recommendations! We will respond to any questions you had in a timely manner.
<?php die(); ?>
我也知道我还需要做更多的工作来确保表单更安全,但现在我只想解决这个问题。有什么想法吗?谢谢大家!
【问题讨论】:
-
搜索 AJAX 教程。
-
也许用关键字 AJAX 搜索?它会带你到这样的事情:blog.teamtreehouse.com/create-ajax-contact-form
-
将所有内容放在同一个文件中并设置
action=""或使用Ajax,如前所述。 -
<input type="radio" name="unhelpful" value="There Was Too Little Information On The Page"<br>中还有一个语法错误,应该读作<input type="radio" name="unhelpful" value="There Was Too Little Information On The Page"><br>缺少> -
感谢该教程链接!我不知道我需要 AJAX,但该教程看起来拥有我需要的一切。
标签: php forms redirect contact