0

I have my json being passed to the php file as follows.

JAVASCRIPT

 $.ajax({
      url: "/gallerytest/getimages.php",
      type: "POST",
      dataType: "json",
      data: {
           imagesArray: imageArray.itemList,
           action: "load"
      },
      success: function(imagesArray) {
             for (var i = 0; i < imagesArray.length; i++) {
                  console.log('image ' + imagesArray[i]["src"]);
             }
      }
  });

With the imagesArray being captured in the php file, I want to put it into an array. In my php I have this so far:

PHP

if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
    case "test" : test();break;
    case "load" : load();break;
    // ...etc...
}
}


function load(){

$imagesArray = $_POST['imagesArray'];


$images = json_decode($imagesArray, true);
$imagesArr = array();
for ($i = 0; $i < count($images); $i++){
    $image = array(
        "src" => $images[$i]
    );
    $imagesArr[] = $image;  
}


echo json_encode($imagesArr);
}

I am passing the array back for testing purposes where the javascript outputs the src of each element.

The ultimate goal which I would like help on too, should it not go beyond the call of duty, is in an SQL statement capture more images where the src does not match. In essence:

 SELECT * FROM images WHERE id_image NOT IN (imagesArray) ORDER BY rand() LIMIT 12
Tanil
  • 328
  • 3
  • 17
  • Have a look at http://stackoverflow.com/questions/920353/can-i-bind-an-array-to-an-in-condition (an example for PDO) I'm sure similar posts exists for mysqli_* – VMai May 20 '14 at 11:36
  • 2
    What exactly is the problem? you got some error? is it returning wrong data? btw is it echo `json_encode($imagesArray);`? Should it not be `echo json_encode($imagesArr);`? – Milan Halada May 20 '14 at 11:36
  • It doesn't echo anything. I have a test method that just spits out a list of numbers so I think the problem is converting the json object into a php array. – Tanil May 20 '14 at 11:38
  • 1
    You are encoding the wrong variable.. – RaggaMuffin-420 May 20 '14 at 11:39
  • 2
    `for($i = 0; $i < count($images); $i++) {` change the for loop, most likely a 500 error and always check the response code from the console browser. and `echo json_encode($imagesArr);` – user1978142 May 20 '14 at 11:39
  • no console isn't throwing any errors. It logs the "load images" in the JS section okay. I have a test method which creates an empty array, loops around filling it with numbers 1..10 and then passes it back to JS. That console.logs fine. So I assume the issue lies in the decode part or somewhere in the JSON php object part. @kevinabelita the for loop is the same as you've suggested :S. – Tanil May 20 '14 at 11:43
  • @user3012749 you could just echos after every step to debug it. Did you repair the for cycle bug kevinabelita found? Is the `success` event in js even fired? Try to add `error` event too – Milan Halada May 20 '14 at 11:47
  • That's what I assume. It should print at the very least 'image null' or along those lines. I thought the echo at that point will be a good time to debug. Not many steps before that. I have fixed the for loop but still an error – Tanil May 20 '14 at 11:55
  • @Uriel_SVK parsererror SyntaxError {stack: (...), message: "Unexpected token <"}(arguments),b?e=h.length:c&&(g=d,j(c))}return this} – Tanil May 20 '14 at 11:59
  • @user3012749 looks like some jQuery error, can you add breakpoints in `success`/`error` events in js to see what exactly is causing it? – Milan Halada May 20 '14 at 12:03
  • unfortunately not. I've decided to get the imagesArray `$imagesArray = $_POST['imagesArray'];` then echo back `echo $imagesArray;` – Tanil May 20 '14 at 12:09

1 Answers1

0
$.ajax({
      url: "/gallerytest/getimages.php",
      type: "POST",
      dataType: "json",
      data: {
           "imagesArray": imageArray.itemList,
           "action": "load"
      },
      success: function(imagesArray) {
             for (var i = 0; i < imagesArray.length; i++) {
                  console.log('image ' + imagesArray[i]["src"]);
             }
      }
  });

Please Pass the action and imagesArray tag in double quote

Kushal Suthar
  • 423
  • 6
  • 21