0

I'm trying to append some items after the contact form is submitted. Unfortunately, I don't know how to tell it what document to append the items to. Here is the full code. Someone able to help?

<?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
}
?>
4

2 回答 2

0

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

 $(document).ready(function () {
     $("#submit").after("<p class='error'>Sorry, something has gone wrong.</p>");
 });
于 2013-07-21T03:42:41.817 回答
0

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

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

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

例如

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 }
于 2013-07-21T03:43:43.400 回答