附在表单提交上

时间:2013-07-21 03:33:27

标签: php jquery forms email

我正在尝试在提交联系表单后附加一些项目。不幸的是,我不知道如何告诉它要附加项目的文件。这是完整的代码。有人能帮忙吗?

<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];

$mail_to = 'contact@jeremyblaze.com';
$subject = 'Support Request';

$body_message = 'From: '.$field_name."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#submit").append("<p class='success'>Thanks! Your email has been sent.</p>");
        });
    </script>
<?php
}
else { ?>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#submit").append("<p class='error'>Sorry, something has gone wrong.</p>");
        });
    </script>
<?php
}
?>

2 个答案:

答案 0 :(得分:0)

工作演示http://jsfiddle.net/NYac5/

 $(document).ready(function () {
     $("#submit").after("<p class='error'>Sorry, something has gone wrong.</p>");
 });

答案 1 :(得分:0)

这部分代码混合了PHP,HTML和Javascript - 它不会像这样工作。

if ($mail_status) { ?>
  <script type="text/javascript">
  .
  .
  .
  </script>
<?php

你需要做两件事之一。发出带有邮件状态的新文档,或者将响应发送回Ajax调用(如果你已经这样做了)

E.g。

if ($mailStatus) {
?>
<!doctype html><html><head></head><body>
  <p class='success'>Thanks! Your email has been sent.</p>
</body></html>
<?php } else {  ?>
<!doctype html><html><head></head><body>
  <p class='failure'>Sorry! Your mail couldn't be sent</p>
</body></html>
<?php }

...或...

if ($mailStatus) {
  header("Content-type: application/json");
  echo '{"errcode":"0","errmsg":"Success - your message was sent"}'
} else {  
  header("Content-type: application/json");
  echo '{"errcode":"99","errmsg":"Sorry - your message couldn't be sent."}'
<?php }