2010-12-15 62 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