Pass PHP Array via jQuery Ajax

后端 未结 5 1069
粉色の甜心
粉色の甜心 2021-01-22 13:40

I\'ve got a php array:

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

I want to pass this array via jQuery AJAX

5条回答
  •  遇见更好的自我
    2021-01-22 14:07

    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);
    

    Which would give you

    string '24,25,26,29' (length=11)
    

    You then inject this in the form ; something like this would probably do :

    
    

    (Chech the HTML source of your form, to be sure ;-) )


    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);
    }
    

    Which will get you :

    string '24' (length=2)
    string '25' (length=2)
    string '26' (length=2)
    string '29' (length=2)
    

    And, now, you can use thoses ids...

提交回复
热议问题