Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Let's say I have a javascript array with a bunch of elements (anywhere from 50-200).

I want to send that to PHP (prepared statement) using ajax. Currently, I .load a php file many times inside of a loop, but I want to convert that into an array and send the array once, loading the PHP file once instead of 50-200 times.

array[i] = variable;

share|improve this question
8  
JSON is your friend :-) – Ben Everard Feb 17 '11 at 22:33
The quality of advice we can give is limited by the vagueness of your answer. Can you post some sample code of the loop you're using? – meagar Feb 17 '11 at 22:36
How can I send the data through JSON/retrieve it on the other end? – switz Feb 17 '11 at 22:47

4 Answers

up vote 19 down vote accepted

You could use JSON.stringify(array) to encode your array in JavaScript, and then use $array=json_decode($_POST['jsondata']); in your PHP script to retrieve it.

share|improve this answer
2  
don't forget to sanitize your inputs, or you're toast! :D – Stephen Feb 17 '11 at 23:06
use node.js and mongodb so that you don't have to change from json at any point! – Stephen Feb 17 '11 at 23:23
3  
You should assume that any input to your PHP scripts could have been compromised by someone with malicious intentions. Run any variables that you are planning to put into a database query through mysql_real_escape_string(), and anything that will be put onto the screen through htmlentities() – Gareth Feb 18 '11 at 19:21

AJAX requests are no different from GET and POST requests initiated through a <form> element. Which means you can use $_GET and $_POST to retrieve the data.

When you're making an AJAX request (jQuery example):

// JavaScript file

elements = [1, 2, 9, 15].join(',')
$.post('/test.php', {elements: elements})

It's (almost) equivalent to posting this form:

<form action="/test.php" method="post">
  <input type="text" name="elements" value="1,2,9,15">
</form>

In both cases, on the server side you can read the data from the $_POST variable:

// test.php file

$elements = $_POST['elements'];
$elements = explode(',', $elements);

For the sake of simplicity I'm joining the elements with comma here. JSON serialization is a more universal solution, though.

share|improve this answer

So use the client-side loop to build a two-dimensional array of your arrays, and send the entire thing to PHP in one request.

Server-side, you'll need to have another loop which does its regular insert/update for each sub-array.

share|improve this answer
Why do I need to use a two dimensional array? There's only one parameter. – switz Feb 17 '11 at 22:45
@Switz I assumed you were using the data parameter of a $.get or $.post call. – meagar Feb 17 '11 at 23:02

Here's a function to convert js array or object into a php-compatible array to be sent as http get request parameter:

function any2url(prefix, obj) {
        var args=new Array();
        if(typeof(obj) == 'object'){
            for(var i in obj)
                args[args.length]=any2url(prefix+'['+encodeURIComponent(i)+']', obj[i]);
        }
        else
            args[args.length]=prefix+'='+encodeURIComponent(obj);
        return args.join('&');
    }

prefix is a parameter name.

EDIT:

var a = {
    one: two,
    three: four
};

alert('http://mysite.com/script.php?'+obj2url('a', a)); 

Will produce

http://mysite.com/script.php?a[one]=two&a[three]=four

which will allow you to use $_GET['a'] as an array in script.php. You will need to figure your way into your favorite ajax engine on supplying the url to call script.php from js.

share|improve this answer
I'm confused as to how this works. I have an array built in JS, how do I go from there? – switz Feb 17 '11 at 22:45
check the edit, hope it makes things clear for you. – Dennis Kreminsky Feb 17 '11 at 22:49

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.