2014-11-16 33 views
1
$.ajax({ 
url: 'test.php', 
type: "POST", 
data: registerForm.serialize(), region: $("#regio option:selected").text(), 
success: function(data) { 
console.log(data) 
} 
}); 

現在我用這個我得到registerForm.serialize()的所有數據,我可以叫他們test.php的與$_POST['the_name'];傳遞值從Ajax調用PHP

現在我需要傳遞給test.php的$("#regio option:selected").text()每當我在test.php中做echo $_POST['region']它沒有看到區域屬性,我在裏面發送ajax到test.php。

如何將$("#regio option:selected").text()傳遞給test.php並在test.php中調用它。

回答

0

你可以只添加一個查詢字符串成:

data: registerForm.serialize() + '&region=' + $("#regio option:selected").text(), 

然後,只是把它當作一個PHP正常崗位價值

$region = $_POST['region']; 
0

讓我們重新縮進代碼:

$.ajax({ 
    url: 'test.php', 
    type: "POST", 
    data: registerForm.serialize(), 
    region: $("#regio option:selected").text(), 
    success: function(data) { 
     console.log(data) 
    } 
}); 

上面的代碼定義了一個region屬性,該屬性已被傳遞給$.ajax()region屬性不屬於data屬性,實際上你的data屬性甚至不是對象。另一個答案建議您可以使用字符串連接添加另一個查詢參數,或使用serializeArray方法並將對象推入返回的數組中:

// returns an array of objects with `name` and `value` properties 
var postData = registerForm.serializeArray(); 
postData.push({ 
    name: 'region', 
    value: $("#regio option:selected").text() 
}); 

$.ajax({ 
    url: 'test.php', 
    type: "POST", 
    data: postData, 
    success: function(responseData) { 
     console.log(responseData); 
    } 
});