gpt4 book ai didi

php - 通过 jQuery Ajax 传递 PHP 数组

转载 作者:行者123 更新时间:2023-11-29 01:34:21 26 4
gpt4 key购买 nike

我有一个 php 数组:

$toField = explode(",", $ids);  //Which looks something like '24,25,26,29'

我想通过 jQuery AJAX 传递这个数组,用这个:

<form id="myForm">
<input type="hidden" value="'.$toField.'">
<input type="submit" id="sendMessage" class="faceboxSubmit" name="ok" value="Send Reply"/>
</form>

这是我的 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,以执行如下操作:

    <?php 

foreach ($_POST['receiverID'] as $receiverID)
{
mysql_query //etc...
}

非常感谢任何帮助!

最佳答案

<form id='myform'>
<input type='hidden' name='to[]' value='1' />
<input type='hidden' name='to[]' value='2' />

.. etc ..
.. rest of you're form ..
</form>

jQuery 将数据:.. 部分更改为:

data: $('#myform').serialize()

那么你就是 PHP:

foreach ( $_POST [ 'to' ] as $num )
{
// do something with $num;
}

像这样的选项?

关于php - 通过 jQuery Ajax 传递 PHP 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1219473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com