将数组从AJAX传递到HTML到PHP - 这可能吗?

I have an AJAX call returning an array to a PHP-HTML page.


Here are some blocks of codes:

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:

  1. Is it possible to return an array value and insert it into the HTML page?
  2. Is a JavaScript array the same as a PHP array?
  3. In the end, will my $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.