2010-12-15 61 views
0

如果一直在尝试一段时间,并且无法在没有帮助的情况下使其工作。不能发送带有ajax的变量到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被发送给我,但我不能得到它的工作。

我认为我的主要问题是,我不确定如何发送的form_values2变种的PHP文件看起来像。

非常简单的php测试表单不起作用。

<?php 
$mail = $_POST['Email']; 
$name = $_POST['Name']; 




$to = "[email protected]"; 
$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"; 
} 
?> 

非常感谢您的帮助。

亚伦

回答

2

一个更容易的方法(和避免编码错误)将在<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; 
}); 
+0

非常感谢你的回复尼克。但我认为主要的问题是PHP文件本身。我不明白。你可以帮我吗? – Aaron 2010-12-15 11:17:23

+0

@Aaron - 我在你的问题中没有看到任何PHP代码,你可以添加它吗? – 2010-12-15 12:11:11

+0

尼克,这是我的主要问题:(我无法编码一个PHP文件,这将给我发送变量“form_values2”。上面的代码是正确的,我只是点得到它如何编码mail.php为了电子邮件我的变量 – Aaron 2010-12-15 12:13:43

0

您需要使用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() { 
     ... 
    } 
}); 
+0

不错的想法!但我更希望如果有人能够通过向我展示一个php文件来帮助我解决现有的代码,那么就会通过电子邮件发送表单。 – Aaron 2010-12-15 11:49:24