1

I want to pass an array from a HTML site to a PHP script using AJAX

JS

function selectPictures() {
    //selected Pictures is my JS array
    var jsonArray = JSON.stringify(selectedPictures);

    var request;
    request = $.ajax({
        url: "selectedPictures.php",
        type: "POST",
        data: {
            data: jsonArray
        },
        cache: false
        success: function () {
            alert('OK');
        }
    });
}

HTML

href="selectedPictures.php" onclick="selectPictures();"

PHP

if (isset($_POST['data'])) {
    $data = json_decode(stripslashes($_POST['data']));
    foreach($data as $d) {
        echo $d;
    }
}

Actually I want to send the data to another HTML page and then include the PHP script, but I don't understand why this example does not even work. The $_POST['data'] is not set.

UPDATE Ok, the Ajax post is actually working, as I see the HTTP request is successful BUT: I cannot access the variable instantly. I need to access the values of the passed array at once to execute another PHP script. When I want to do this, I get an undefined index error. Also at the time when the isset function is executed, it returns false (despite the successful HTTP request).

4
  • And where is defined selectedPictures ?? Commented Jul 23, 2014 at 15:08
  • Does the OK alert show? Commented Jul 23, 2014 at 15:08
  • please insert a var_dump($_POST) at the top of your PHP file, to see if you actually receive something. if not, you can concentrate on the JS to find the problem. Commented Jul 23, 2014 at 15:15
  • Yes, there is nothing received. That's why I'm wondering if something is wrong with the concept or the Ajax request. If I fill the array manually it does not work as well. Commented Jul 23, 2014 at 15:21

1 Answer 1

3

HTML

<a href="#" id="selectPictures">click</a>

JS

$(function(){
  $('#selectPictures').click(function(){
    var jsonArray = JSON.stringify(selectedPictures);   
    var request = $.ajax({
    url: "selectedPictures.php",
    type: "POST",
    data: {data: jsonArray},
    cache: false,
    success: function(data){alert(data);}
    });
  });
});

Use f12 in chrome to see errors, you forgot to add a comma after the "cache: false"

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, now the 'OK' alert shows up but I still don't get the data in my PHP script, i.e. the else part of the isset function is still executed.
u get it, u can see it in network tab in chrome dev tools. or replace success to: function(data){alert(data);}
Oh, you are right, I was confused because var_dump[POST] still showed array(0). Thanks
Ok, then how come the isset is still false?
var selectedPictures is not empty? i check it in my localhost it work fine if i replace "selectedPictures" with something like this {"ar":"ar"}
|

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.