I have an AJAX call returning an array to a PHP-HTML page.
Javascript:
"Note: get_data.php returns an HTML string, and I want to store it in an array."
var param_array = new Array();
$.post('get_data.php', { id: id }, function(data))
{
param_array.push(data);
$('#hidden_input').val(param_array);
}
HTML:
<form action='my_function' method='post'>
<input type='hidden' id='hidden_input' name='array_from_ajax'>
<input type='submit' name='btn_submit'>
</form>
PHP - CodeIgniter:
my_function()
{
$param_array = $this->input->post('array_from_ajax');
print_r($param_array); // Will this work? will $param_array contain the value?
}
Questions:
$param_array
in PHP contain the value passed from the AJAX call?JavaScript and PHP can only communicate over HTTP and only via strings (unless you could SOAP I guess, if that's even possible). That is to say there is no compatibility in data structures between them, so a JavaScript array <> a PHP array. Similarly, you can't set the input value to a JavaScript array.
The best way to handle this would probably be to JSON-encode the array before setting it to the value of the input. PHP can easily decode the JSON into data structures it can use.