无法从ajax联系表单

时间:2015-05-18 22:26:50

标签: php jquery sendmail

我基本上编辑/加入一些脚本来制作一个滑动联系表单。幻灯片正在运行。已定义表单元素和sendmail php我认为也是正确的但由于某种原因我无法发送电子邮件。 jquery发送邮件检查功能显示错误 - 错误。

文件结构

contact_me.php
feedback/process_email.php
feedback/core.js

HTML

<html>
<head>
    <title>
        Feedback Form Demo
    </title>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>

    <link rel="stylesheet" href="feedback/core.css">
    <link rel="stylesheet" href="styles/bootstrap.min.css">

    <script src="feedback/core.js"></script>

</head>
<body>


    <div class="feedback">
    <a id="feedback_button">Enquiry</a>

    <div class="form">
    <h3>Please Send Us Your Enquiry</h3>
        <span class="status"></span>
        <ul class="feedb">
        <li class="col-lg-12"><label class="col-lg-3" for="fname">Name</label><input class="col-lg-9" type="text" name="fname" id="fname" value=""></li>
        <li class="col-lg-12"><label class="col-lg-3" for="femail">Email</label><input class="col-lg-9" type="text" name="femail" id="femail" value=""></li>
        <li class="col-lg-12"><label class="col-lg-3" for="fphone">Phone</label><input class="col-lg-9" type="text" name="fphone" id="fphone" value=""></li>
        <li><label for="finterest">Area of Interest</label></li>
                            <?php include 'connect.php'; 

/*
Displaying List of Categories from the Table - Category and that is limited to 10
*/
$qry=mysql_query("SELECT * FROM category LIMIT 0, 10 ", $con);

if(!$qry)
{
die("Query Failed: ". mysql_error());
}

while($row=mysql_fetch_array($qry))
{
?>
                    <li class="fcheck" ><INPUT TYPE=CHECKBOX NAME="finterest[]" value="<?php echo $row['category']; ?>"  > <?php echo $row['category']; ?><?php }?></li>


        <li><label for="fmsg">Message</label></li>
        <li><textarea rows="8" id="feedback_text"></textarea></li>
        <li><input type="button" value="Send" id="submit_form" /></li>
    </ul></div>
</div>

    </body>
</html>

PHP

<?php
  $to = "saudkazia@gmail.com";
  $name = $_POST['fname'];
  $subject = "Enquiry from ".$name;
  $phone = $_POST['fphone'];
  $email = $_POST['femail'];
  $message = $_POST['feedback'];
    $ip = $_SERVER['REMOTE_ADDR'];
    $httpref = $_SERVER['HTTP_REFERER'];
    $httpagent = $_SERVER['HTTP_USER_AGENT'];
    $today = date("F j, Y, g:i a");    

$interests  = 'None';
if(isset($_POST['finterest']) && is_array($_POST['finterest']) && count($_POST['finterest']) > 0){
    $interests = implode(', ', $_POST['finterest']);
}

    $mailbody = "
First Name: $name
Email: $email
Phone: $phone
Message: $message

Interests:
$interests

IP: $ip
Browser info: $httpagent
Referral: $httpref
Sent: $today
";



  if(mail($to,$subject,$mailbody))
  {
   echo "Message Sent";
  }
  else
  {
    echo "Message Not Sent";
  }
?>

JS

/*
 * JQuery functions for slideout feedback form
 * 
 * Sets up a sliding form on click of a feedback button
 * On submit button will send the data to a php script
 * 
 * By http://www.paulund.co.uk
 */
(function ($j) {

  feedback_button = {

    onReady: function () {      
      this.feedback_button_click();
      this.send_feedback();
    },

    feedback_button_click: function(){
        $("#feedback_button").click(function(){
            $('.form').slideToggle();           
        });
    },

    send_feedback: function(){
        $('#submit_form').click(function(){
            if($('#feedback_text').val() != ""){

                $('.status').text("");

                $.ajax({  
                    type: "POST",  
                    url: "./process_email.php",  
                    data: 'feedback=' + $('#feedback_text').val(),  
                    success: function(result,status) { 
                        //email sent successfully displays a success message
                        if(result == 'Message Sent'){
                            $('.status').text("Feedback Sent");
                        } else {
                            $('.status').text("Feedback Failed to Send");
                        }
                    },
                    error: function(result,status){
                        $('.status').text("Error");
                    }  
                });
            }
        });
    },


  };

  $j().ready(function () {
      feedback_button.onReady();
  });

})(jQuery); 

我认为问题出在PHP上,但我怎么能验证问题所在,因为Jquery ajax不允许显示php错误。有没有办法我可以暂时允许jquery显示php错误,以便我可以验证问题。

2 个答案:

答案 0 :(得分:1)

无论PHP打印是什么,都会以ajax响应的形式返回。因此,在调试时将echo语句放在PHP中,并且可以在成功回调的'result'参数中使用。

答案 1 :(得分:0)

我简要介绍了一下你的代码,我相信你的AJAX的URL是错误的。你确定AJAX请求到达PHP脚本吗? 您正在使用网址./process_email.php。我相信您正在使用UNIX终端语法来指向文件。但是,在Web上下文中,URL与您的域相关。

假设您的域名为www.mywebapp.com。如果没有任何规则或重新路由,您的联系表单应该来自www.mywebapp.com/contact_me.php。您要在AJAX中执行的操作是致电www.mywebapp.com/feedback/process_email.php,因此其中的网址必须为/feedback/process_email.php

总结:

$.ajax({
    type: "POST",
    //WRONG --> url: "./process_email.php", <-- WRONG!
    url: "/feedback/process_email.php",
    data: 'feedback=' + $('#feedback_text').val(),
    success: function (result, status) { 
        //email sent successfully displays a success message
        if (result == 'Message Sent') {
            $('.status').text("Feedback Sent");
        } else {
            $('.status').text("Feedback Failed to Send");
        }
    },
    error: function (result, status) {
        $('.status').text("Error");
    }
});