联系表格 - AJAX被忽略

时间:2014-08-01 09:31:20

标签: javascript php jquery html ajax

大家好我不能让一个jsfiddle任何人都可以看到这个AJAX表单的问题,我之前使用过这个代码并且它已经工作了就像asif网站没有读取ajax代码而且我花了几个小时研究这个

AJAX:

<script type="text/javascript">
$(document).ready(function() {  
    $("#submit_button").click(function(event) {
        event.preventDefault();//cancel postback
        var name = $("#name").val();
        var company = $("#company").val();
        var comments = $("#comments").val();
        if(name != '' && company != '' && comments != '')
        {
            $.ajax({
              url: "review.php",
              type: "POST",
              data: {
                   name: name,
                   company: company,
                   comments: comments
               }
                success: function(data) 
              {
                // You might want to display a message telling 
                //the user that the form was successfully filled out.
                $('#contactform').css({ 'opacity' : 0.4 });
                $("#successmessage").show();
                $("#dangermessage").hide();
              }
            });
        }
        else
        {
        $("#dangermessage").show();
        }
    });//click()
});//ready()
</script>

HTML:

<div id="successmessage" style="display:none;" class="alert-success" role="alert"><strong>Thank you, Your review has been received, We will upload shortly!</strong></div>
        <div id="dangermessage" style="display:none;" class="alert-danger" role="alert"><strong>Something went wrong there is all fields filled out correctly?</strong></div>
                    <h4>Wish to Add a review?</h4>
                        <form id="review" name="review" method="post" action="review.php" class="reviewForm">
                            <input class="inputLeft" type="text" id="name" name="name" placeholder="*First and Second Name">
                            <input class="inputRight" type="text" id="company" name="company" placeholder="*Company"><br />
                            <textarea name="comments" id="comments" placeholder="Comments:"></textarea><br />
                            <button id="submit_button" class="sendBtn">Send</button>
                        </form>

PHP:

<?php
if(isset($_POST['name'])) {

$name = $_POST['name']; // required
$company = $_POST['company']; // not required
$comments = $_POST['comments']; // required

$email_to = "test@live.co.uk";
$email_subject = $name . " Review Submission";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($name)."\n";
$email_message .= "Company: ".clean_string($company)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$name.$company."\r\n".
'Reply-To: '.$name.$company."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
//@mail($email_also, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<!--Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>

我已经尝试了PHP表单正在工作的所有内容并发送邮件,但是AJAX无法正常工作,经过一些调整后我设法得到了显示的危险信息,但现在它甚至没有这样做。

目前它所做的一切都是去.php文件发送应该被event.preventDefault();

停止的邮件

此外,我在其他网站上使用了这段代码,它运行正常。

谢谢大家

2 个答案:

答案 0 :(得分:4)

您忘记了数据后的逗号:

         data: {
               name: name,
               company: company,
               comments: comments
           },
            success: function(data) 
          {

答案 1 :(得分:0)

删除表单标记,它将完美运作!

enter image description here

在您的评论页面中:

   $email_message = "First Name: ".clean_string($name)."\n";
   $email_message = "Company: ".clean_string($company)."\n";
   $email_message = "Comments: ".clean_string($comments)."\n";

而不是

$email_message .= "First Name: ".clean_string($name)."\n";
$email_message .= "Company: ".clean_string($company)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

因为$ email_message。=&#34;名字:&#34; .clean_string($ name)。&#34; \ n&#34 ;;倾向于   $ email_message = $ email_message。 &#34;名字:&#34; .clean_string($ name)。&#34; \ n&#34 ;;但 $ email_message实际上没有定义 定义它或删除点

和数据后的逗号

      data: {
           name: name,
           company: company,
           comments: comments
       },