我正在尝试使用PHP将简单表单发送到电子邮件,但无法使用实际的ajax函数。
这是表格:
<form id="quick-booking-form" method="post" action="#">
<input type="text" name="mail" id="mail">
<textarea name="message" id="message">
</textarea>
<input type="submit" value="Send the Message" id="SendTheForm">
</form>
然后将其发送到单独的Javascript函数,该函数包含ajax:
$(function() {
//alert("page loaded");
$('#quick-booking-form').submit(function(event) {
event.preventDefault();
alert("Start of function");
var mail = $("#mail").val();
var message = $("#message").val();
alert("Mail: " + mail + " Message: " + message);
$.ajax({
type: "POST",
url:"process.php",
data:{
"mail": mail,
"message": message,
},
dataType: "text"
}).success(function(result){
alert(result);
//next line: end of function
}) ;
//next line: end of ajax
});
//next line: end of script
});
应将数据发送到此PHP文件(process.php):
$msg = $_POST['message'];
$UserName = $_POST['mail'];
$contact = "Booking Manager";
$reply = "web-bookings@thesite.net";
//Subject is always...
$subject = "A message from ".$UserName;
$from = $reply;
//test data
//$contactDetails = "test@test.net";
// send message
$message = "Message reads: ".$msg;
//$to = "notify@test.com";
$to = $_POST['mail'];
//$headers = "From: " . $contact."\r\n"."Reply-To:".$reply;
$headers = "From: " . $contact;
mail($to, $subject, $message, $headers);
echo "Mail Sent.";
应该发生的是提交表单。然后在validate.js中验证它(代码省略)。如果通过,表单数据将发送到process.php。这又将通知消息发送到notify@test.com。
目前发生的是alert("Start of Function")
点火,显示正确值的警报也会点火。
这就是它。邮件功能似乎没有触发,最后一个警报也没有显示。
答案 0 :(得分:1)
你说得对,但有些改变,试试这个
$(function() {
//alert("page loaded");
$('#quick-booking-form').submit(function(event) {
event.preventDefault();
alert("Start of function");
var mail = $("#mail").val();
var message = $("#message").val();
alert("Mail: " + mail + "Message: " + message);
$.ajax({
type: "POST",
url:"process.php",
data:{
"mail": mail,
"message": message,
},
dataType: "text",
success: function(html) {
alert(html);
}
});
return false;
//next line: end of ajax
});
//next line: end of script
});
使用preventDefault函数并成功执行ajax而不是
答案 1 :(得分:0)
试试这个 包括JQUERY库
HTML:
<form id="quick-booking-form" method="post" action="javascript:return false">
email:
<input type="text" name="mail" id="mail">
<br/>
message: <textarea name="message" id="message">
</textarea><br/>
<input type="submit" value="Send the Message" id="SendTheForm">
</from>
脚本:
$(function() {
//alert("page loaded");
$('#quick-booking-form').submit(function(event) {
var mail = $("#mail").val();
var message = $("#message").val();
if(mail==""){
alert("enter Mail"); return false;
}
if(message==""){
alert("enter message"); return false;
}
$.ajax({
type: "POST",
url:"process.php",
data:{
"mail": mail,
"message": message,
},success:function(result){
alert(result);
}
});
//next line: end of ajax
});
//next line: end of script
});
process.php:
$to =$_POST['mail'];
$subject = "Test mail";
$message =$_POST['mesage'];
$from = "bookings@test.net";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";