Ryokel Ryokel - 3 years ago 77
Ajax Question

Multiple values from ajax with undertermined number of value


I want to get multiple values from my database with ajax and php. I see previous post about this (like How to get multiple responses from PHP file via AJAX? ) but all the time it seems the number of values is already known.

I have to return unknown number of values because i dont know how many roxs will be selected.


Are they tricks to get unknown number of values with ajax or should i put data in different arrays and then return them?

Thanks for your answer.

Answer Source

If you just want to send back simple variables, you can concatenate them with a separator like this:

echo $var1.'***---***---***'.$var2.'***---***---***'.$var3;

Then split them with the same separator to build an array in JS.

var array = xhr.responseText.split('***---***---***');

This is not very sexy, you should use JSON.

//PHP side
echo json_encode([$var1, $var2, $var3]);

//JS side
var array = JSON.parse(xhr.responseText);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download