[英]Cannot send variables with ajax to php
If been trying for some time now and cannot get this to work without help. 如果现在已经尝试了一段时间,并且无法在没有帮助的情况下使用它。
I want to send variables from a contact form to a php in order to get it mailed to me. 我想将变量从联系表单发送到php,以便将其邮寄给我。
the js looks something like this: 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;
});
I want the form_values2 to be send to me, but I cannot get it to work. 我希望将form_values2发送给我,但我无法让它工作。
I think the main problem for me is that I'm unsure how the php file has to look like in order to send the form_values2 var. 我认为我的主要问题是我不确定php文件如何发送form_values2 var。
the very simple php test-form does not work. 非常简单的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";
}
?>
Thank you so much for your help. 非常感谢你的帮助。
Aaron 亚伦
A much easier approach (and avoids encoding errors) would be to use .serialize()
on the <form>
so it gets the data like a normal non-AJAX submit does, like this: 一种更简单的方法(避免编码错误)是在
<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;
});
Your second set of inputs already works with this, the first set though you're finding by title
, make sure they have the name
attribute you want to send to the server instead. 您的第二组输入已与此配合使用,尽管第一组输入是按
title
查找的,但请确保它们具有要发送给服务器的name
属性。 Also, just for the sake of completeness, there's an even shorter version using $.post()
, like this: 另外,仅出于完整性考虑,还有一个甚至更短的版本使用
$.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;
});
You need to use Json-typed data. 您需要使用Json类型的数据。 For example:
例如:
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.