![](/img/trans.png)
[英]How to submit attr value and form values with POST using JQuery and PHP?
[英]Post form values using jquery and PHP
我正在尝试使用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")
,并且alert("Start of Function")
也会显示正确的值。
这是它所能做到的。 邮件功能似乎没有启动,并且最后一个警报也未显示。
您说得对,但有些更改,请尝试
$(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
});
对Ajax使用preventDefault函数和成功而不是完成
试试这个Include JQUERY Library
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.";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.