使用jquery和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.phpnotify@test.com。
当前发生的情况是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函数和success,而不是完成
试试这个包括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.";
相关文章:
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 循环遍历包含另一个表单的表单
- 单击表单中的按钮会导致页面刷新
- 禁用表中的行单击
- HTML可单击表行
- 单击表单的“提交”按钮时,获取表单中的所有INPUT和ACTION标记
- 使用jquery添加到表单内部表中的元素不会过帐
- 单击表单1中的按钮:更改表单2中隐藏输入的值(纯javascript)
- 单击表单中的功能
- 显示带有表单提交的单击事件的 toastR
- 单输入表单 - 特殊情况 |停止表单提交
- 使用 JQuery 在单击表中的 tr 时创建弹出窗口
- 流星 1.3 自动表单/快速表单错误
- 动态表行,将单击事件委派给新添加的元素
- 贝宝表单问题的单选按钮选择
- 克隆完整表并作为表单值提交
- Javascript按钮,它提交联系人表单7表单,然后重定向到URL
- 在表格中单击表单外的操作
- Javascript表单验证:表单在错误返回时提交:
- 当使用jquery单击表行时,选中复选框addclass