I'm trying to create a post submission using XMLHttpRequest, but am having a hard time passing the values to PHP in an efficient way.
-
What's wrong with api.jquery.com/serialize ?Cyclone– Cyclone2011-05-01 18:28:44 +00:00Commented May 1, 2011 at 18:28
-
I suppose there is nothing wrong with it. I am just not using Jquery. I was looking for a way to simulate the way a form can build multi-dimensional data and submit it. I thought I could do this without using JSON.Jason– Jason2011-05-01 18:33:10 +00:00Commented May 1, 2011 at 18:33
-
While it could be useful for learning, in practice it's rare that a live site or web app makes raw XMLHttpRequests. You will have a much easier time using a library like jQuery, which as Cyclone points out gives you the added benefit of form serializing.Ben Regenspan– Ben Regenspan2011-05-01 18:34:19 +00:00Commented May 1, 2011 at 18:34
4 Answers
The fact that your name is probably kind of your answer is quite hilarious. Meaning: JSON serialization is a very good way of passing arrays between js and PHP. There are native json functions in PHP since version 5.2.
json_encode and json_decode respectively.
1 Comment
Use some existing library like jQuery, then you don't need to write your own client, waste time supporting different browsers, etc. Its much easier than writing yourself everything from scratch (unless you want to learn it)
Comments
Jquery does indeed make this easy. It actually opens up a few methods to get your data there.
If your data populates a HTML form you could use Jquery to programmatically submit data back to the server via the POST route.
Alternatively you can also set up AJAX calls that use either GET or POST requests, or as these other chaps suggest, you could serialize your data into JSON (JavaScript Object Notation) and submit it that way.