2014-11-16 34 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); 
    } 
});