[英]Cannot send variables with ajax to php
如果现在已经尝试了一段时间,并且无法在没有帮助的情况下使用它。
我想将变量从联系表单发送到php,以便将其邮寄给我。
js看起来像这样:
$("#submitQuote").live('click',function(){
var shirt_style = "shirt_style="+$(".dropdown[title=shirt_style] .dropdownValue").text();
var shirt_type = "&shirt_type="+$(".dropdown[title=shirt_type] .dropdownValue").text();
var cuffs = "&cuffs="+$(".dropdown[title=cuffs] .dropdownValue").text();
var chestpoket = "&chestpoket="+$(".dropdown[title=chestpoket] .dropdownValue").text();
var collar = "&collar="+$(".dropdown[title=collar] .dropdownValue").text();
var collar_buttons = "&collar_buttons="+$(".dropdown[title=collar_buttons] .dropdownValue").text();
var fastening = "&fastening="+$(".dropdown[title=fastening] .dropdownValue").text();
var cut = "&cut="+escape($(".dropdown[title=cut] .dropdownValue").text());
var Name = "&Name="+escape($("input[name=Name]").val());
var Email = "&Email="+escape($("input[name=Email]").val());
var Phonenumber = "&Phonenumber="+escape($("input[name=Phonenumber]").val());
var Address = "&Address="+escape($("input[name=Address]").val());
var Zipcode = "&Zipcode="+escape($("input[name=Zipcode]").val());
var City_country = "&City_country="+escape($("input[name=City_country]").val());
var Copy = "&Copy="+$(".checkbox[title=Copy]").hasClass("checkboxChecked");
var form_values1 = shirt_style+shirt_type+cuffs+chestpoket+collar+collar_buttons+fastening+cut;
var form_values2 = form_values1+Name+Email+Phonenumber+Address+Zipcode+City_country+Copy;
$.ajax({
type: "POST",
url: "http://www.....com/ajax/mail.php",
data: form_values2,
success: function() {
$('html,body').animate({scrollTop:290},1000);
$("#quoteStepContainer").html('');
$("#quoteStepContainer").html('<img src="http://www...com/img/sent.jpg" width="625" height="160" alt="Thank you" id="thankyouimage" />');
$("#thanksImage").fadeIn(1000);
$("#quoteStepContainer").delay(1000).animate({"height": "190px"},1500);
}
});
return false;
});
我希望将form_values2发送给我,但我无法让它工作。
我认为我的主要问题是我不确定php文件如何发送form_values2 var。
非常简单的php测试表单无法正常工作。
<?php
$mail = $_POST['Email'];
$name = $_POST['Name'];
$to = "email@mydomain.com";
$message =" You received a mail from ".$mail;
$message .=" His name is : ".$name;
if(mail($to,$mail,$message)){
echo "mail successful send";
}
else{
echo "there's some errors to send the mail, verify your server options";
}
?>
非常感谢你的帮助。
亚伦
一种更简单的方法(避免编码错误)是在<form>
上使用.serialize()
,使其像正常的非AJAX提交一样获取数据,如下所示:
$("#submitQuote").live('click',function(){
$.ajax({
type: "POST",
url: "http://www.....com/ajax/mail.php",
data: $("#formid").serialize(),
success: function() {
$('html,body').animate({scrollTop:290},1000);
$("#quoteStepContainer").html('<img src="http://www...com/img/sent.jpg" width="625" height="160" alt="Thank you" />').find('img').hide().fadeIn(1000)
.end().delay(1000).animate({"height": "190px"},1500);
}
});
return false;
});
您的第二组输入已与此配合使用,尽管第一组输入是按title
查找的,但请确保它们具有要发送给服务器的name
属性。 另外,仅出于完整性考虑,还有一个甚至更短的版本使用$.post()
,如下所示:
$("#submitQuote").live('click',function(){
$.post("http://www.....com/ajax/mail.php", $("#formid").serialize(), function() {
$('html,body').animate({scrollTop:290},1000);
$("#quoteStepContainer").html('<img src="http://www...com/img/sent.jpg" width="625" height="160" alt="Thank you" />').find('img').hide().fadeIn(1000)
.end().delay(1000).animate({"height": "190px"},1500);
});
return false;
});
您需要使用Json类型的数据。 例如:
var ajaxData = {
shirt_style: $(".dropdown[title=shirt_style] .dropdownValue").text(),
shirt_type : $(".dropdown[title=shirt_type] .dropdownValue").text()
};
$.ajax({
type: "POST",
url: "http://www.....com/ajax/mail.php",
data: ajaxData,
success: function (data) {
...
},
error: function () {
...
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.