Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
And where is defined selectedPictures ?? –  Hackerman Jul 23 '14 at 15:08
    
Does the OK alert show? –  Spokey Jul 23 '14 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. –  Jens A. Koch Jul 23 '14 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. –  Pete Jul 23 '14 at 15:21

1 Answer 1

up vote 2 down vote accepted

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"

share|improve this answer
    
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. –  Pete Jul 23 '14 at 15:29
    
u get it, u can see it in network tab in chrome dev tools. or replace success to: function(data){alert(data);} –  Xrymz Jul 23 '14 at 15:31
    
Oh, you are right, I was confused because var_dump[POST] still showed array(0). Thanks –  Pete Jul 23 '14 at 15:37
    
Ok, then how come the isset is still false? –  Pete Jul 23 '14 at 15:42
    
var selectedPictures is not empty? i check it in my localhost it work fine if i replace "selectedPictures" with something like this {"ar":"ar"} –  Xrymz Jul 23 '14 at 16:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.