通过jQuery的Ajax通过PHP数组 [英] Pass PHP Array via jQuery Ajax
问题描述
我有一个PHP数组:
$toField = explode(",", $ids); //Which looks something like '24,25,26,29'
我想通过通过jQuery AJAX这个数组,这一点:
I want to pass this array via jQuery AJAX, with this:
<form id="myForm">
<input type="hidden" value="'.$toField.'">
<input type="submit" id="sendMessage" class="faceboxSubmit" name="ok" value="Send Reply"/>
</form>
这是我的jQuery的:
And here's my jQuery:
$("#sendMessage").click(function(event){
event.preventDefault();
var senderID = <?php echo $usrID; ?>;
var receiverID = $("#toField").val();
$.ajax(
{
type: "POST",
url: "replyMessage.php",
data: "senderID=" + senderID + "&subject=" + subject + "&message=" + message + "&receiverID=" + receiverID + "&threadID=" + thread_id,
beforeSend: function() {
$("#sendingMessage").show();
},
success: function() {
alert("Success");
}
});
});
我
如何传递数组,使之与replyMessage.php页面中,我可以把每个ID从阵列,做这样的事情:
How can I pass the array, so that with the "replyMessage.php" page, I could take each ID from the Array, to do something like this:
<?php
foreach ($_POST['receiverID'] as $receiverID)
{
mysql_query //etc...
}
任何帮助是极大AP preciated!
Any help is greatly appreciated!
推荐答案
首先,你可能想使用 爆
,而不是爆炸,来构建你的 $ toField
变量; - )
First, you probably want to use implode
, and not explode, to construct your $toField
variable ;-)
$ids = array(24, 25, 26, 29);
$toField = implode(',', $ids);
var_dump($toField);
这将使你
string '24,25,26,29' (length=11)
您在随后的形式注入这一点;这样的事情可能会做:
You then inject this in the form ; something like this would probably do :
<input type="hidden" value="<?php echo $toField; ?>">
的(再检查一下你的表单的HTML源代码,以确保;-))的
结果
然后,当它被提交从表单接收数据的PHP脚本,你会使用 爆炸
来提取数据作为一个数组,从字符串:
Then, on the PHP script that receives the data from the form when it's been submitted, you'd use explode
to extract the data as an array, from the string :
foreach (explode(',', $_POST['receiverID']) as $receiverID) {
var_dump($receiverID);
}
这将让你:
string '24' (length=2)
string '25' (length=2)
string '26' (length=2)
string '29' (length=2)
和,现在,你可以使用IDS放入系统...
And, now, you can use thoses ids...
这篇关于通过jQuery的Ajax通过PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!