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'm learning Ajax by failure and have hit a wall:

I have an array (if it matters, the array is storing number id's based on what checkboxes the user checks) that is written in Javascript.

I have a function that is called when the user clicks the 'save' button. The function is as follows:

function createAmenities() {
    if (window.XMLHttpRequest) {
        //code for IE7+, Firefox, Chrome and Opera
        xmlhttp = new XMLHttpRequest();
    }
    else {
        //code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById('message').innerHTML = xmlhttp.responseText;
        }
    }

    var url = "create_amenities.php";

    xmlhttp.open("GET", url, true);

    xmlhttp.send();

}

My question is: What can I put in this function to pull the array into the php script I'm trying to call ('create_amenities.php')?

furthermore, should I try using JSON? And if so, how could I send a JSON object via ajax?

Thanks in advance.

share|improve this question

4 Answers 4

up vote 18 down vote accepted

If your array has more then 1 dimension or is an associative array you should use JSON.

Json turns a complete array structure into a string. This string can easily send to your php application and turned back into a php array.

More information on json: http://www.json.org/js.html

var my_array = { ... };
var json = JSON.stringify( my_array );

In php you can decode the string with json_decode:

http://www.php.net/manual/en/function.json-decode.php

var_dump(json_decode($json));
share|improve this answer
1  
This is a clean solution (+1). Just for completeness: the array can also be my_array=[...] and after json=JSON.stringify(my_array) it is sent as url="create_amenities.php?json"; –  Jiri Aug 4 '11 at 7:20
    
+1: Works very well with concise, easy to read code. –  FreeAsInBeer Jul 17 '12 at 20:50

Loop over the array and append encodeURIComponent('keyname[]') + '=' + encodeURIComponent(theArray[i]) + '&' to the query string each time.

furthermore, should I try using JSON?

You could, but it would mean decoding JSON at the other end instead of letting normal form handling take care of it.

And if so, how could I send a JSON object via ajax?

There is no such thing as a JSON object. JSON takes the form of a string, and you can include strings in query strings (just remember to encodeURIComponent).

share|improve this answer

First off, yes, do not write ajax by hand. You are unlikely to produce something that truly works on all browsers.

The best approach to your actual problem would be to submit your array as cgi parameters.

If the checkboxes are in a form, you need to do very little - simply submit the form,

 <form><input type=checkbox ...><input type=checkbox ...>
 $.post("test.php", $("#testform").serialize());

See http://api.jquery.com/jQuery.post/ for more details on how to do that. Your list will turn up as an array in PHP.

Or to augment your own example with something very simple, do this:

  url = url + '?checkboxes=' + checkboxes.join(',');

Now just split it inside PHP and you've got it back!

share|improve this answer

Please consider to use http://api.jquery.com/jQuery.get. It's easy to use.

share|improve this answer

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.