skip to Main Content

I know this HAS to be a duplicate. But I can’t for the life of me figure out where it’s going wrong. I’ve tried passsing the array directly, using json encode/decode, joining the array in jquery and passing it as a string and exploding it….but I just can’t get my php to run with the pass through data. Help?!

There is something going on with quotes if I had to guess, but I’m striking out. If I comment out the line that retrieves the array, and uncomment the two lines where i set the list and explode it, the script can be called by itself and works as expected. But in it’s current form, it does nothing. I’m sure I’m missing something simple because I don’t know enough about either language.

EDIT: UPDATE:
I was able to use:

var_dump($_POST);
die();

to get the following response in the network console. Does this shed light as to why the script is failing to run when passed the array?

array(1) {
  ["download_listArray"]=>
  array(3) {
    [0]=>
    string(6) "7391-1"
    [1]=>
    string(6) "7392-2"
    [2]=>
    string(6) "7393-3"
  }
}

For completeness sake, here is the same response when using the JSON.stringify method:

array(1) {
  ["download_listArray"]=>
  string(26) "["7391-1","7392-2","7393-3"]"
}

Jquery:

console.log(download_list);   \ gives: ["7391-1", "7392-2", "7393-3"]

 $.ajax({
            type: "POST",
            url: "zip_download.php",
            data: {download_listArray:download_list},

            success: function(){
            alert("OK");
        }
        });

PHP:

<?php
$sample_name_list = $_POST['download_listArray'];

//$sample_name_list = "7391-1,7392-2,7393-3";    #If I use these two lines, the script runs as expected
//$sample_name_list = explode(",", $sample_name_list);

foreach ($sample_name_list as $i => $sample_name){

   //do stuff
}

2

Answers


  1. Convert the array into Json and try same as the following codeblocks. Hope that works for you.

    Jquery:

    var download_list= JSON.stringify(download_list);
    $.ajax({
        type: "POST",
        url: "zip_download.php",
        data: {download_listArray:download_list},
        cache: false,
        success: function(){
            alert("OK");
        }
    });
    

    PHP:

    <?php
    $sample_name_list = json_decode(stripslashes($_POST['download_listArray']));
    
    foreach ($sample_name_list as $i => $sample_name){
       //do stuff
    }
    
    Login or Signup to reply.
  2. In response to your update:

    For completeness sake, here is the same response when using the JSON.stringify method:

    array(1) {
      ["download_listArray"]=>
      string(26) "["7391-1","7392-2","7393-3"]"
    }
    

    Please find my updated code to solve your issue according to your given data:

    PHP:

    <?php
    
    $_POST['download_listArray'] = '["7391-1","7392-2","7393-3"]';
    
    //var_dump($_POST['download_listArray']); die();
    
    $sample_name_list = json_decode(stripslashes($_POST['download_listArray']));
    
    foreach ($sample_name_list as $i => $sample_name){
    
       echo $sample_name.'<br>';
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search