1

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
}
15
  • It should work. What does var_dump($_POST); show? Commented Feb 27, 2020 at 2:05
  • I think your ` url: "zip_download.php",` is not correct, verify file path Commented Feb 27, 2020 at 3:39
  • @Barmar I am triggering this with a multiselect in a leaflet map. So any sort of dump, print, or echo doesn't show up. And I don't have the knowledge on how to get them to show up otherwise. (If I navigate to the php directly, i can see the messages pop up in my window, but that doesn't do any good for the troubleshooting of the handoff.) Commented Feb 27, 2020 at 14:19
  • @AkhilAravind It's a generic sanitized link for the purpose of sharing. The actual link is correct, and I can navigate to it through my editor when i have the jquery file open. Commented Feb 27, 2020 at 14:20
  • @Barmar - I've updated the question with some debugging. I did var_dump followed by die in order to see what came back in the network console. Commented Feb 27, 2020 at 14:29

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried several variations of that as well with no luck. I've tried using Json.strigify on the array as well as a joined string. I've tried to read the JSON in PHP as you have above both with and without stripslashes, as well as with and without the key in the foreach loop.
I updated the question with the debugged output of your suggestion as well.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.