Skip to content
Advertisement

Sending Javascript Object to PHP via Ajax

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:

JavaScript

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.

Advertisement

Answer

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

JavaScript

In php you can decode the string with json_decode:

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

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement