[英]Unable to send email from ajax contact form
我基本上是在編輯/加入一些腳本來制作滑出式聯系表格。 滑出正在工作。 表單元素已經定義,並且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錯誤,以便驗證問題。
任何PHP打印內容都將作為ajax響應返回。 因此,在調試時將echo語句放入PHP中,這將在成功回調的'result'參數中提供。
我對您的代碼進行了簡短的調查,我相信您的AJAX的URL錯誤。 您確定AJAX請求到達了PHP腳本嗎? 您正在使用url ./process_email.php
。 我相信您正在使用UNIX終端語法來指向文件。 但是,在Web上下文中,URL是相對於您的域的。
假設您的域名是www.mywebapp.com
。 如果沒有任何規則或更改路線,則可以從www.mywebapp.com/contact_me.php
獲得聯系表格。 您想要在AJAX中執行的操作是調用www.mywebapp.com/feedback/process_email.php
因此該URL必須為/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");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.