how do i pass arrays in javascript to php using POST method
alright im making a sortable displays in my website but i am trying to figure out how to pass my javascript array to and external php script through POST method so i can update the column, position, hidden, or closed in the database i am using jQuery Sortable Portlets
located here
http://jqueryui.com/demos/sortable/#portlets
do you know where i can find what i need??
jcanaway
Junior Poster in Training
51 posts since Apr 2009
Reputation Points: 14
Solved Threads: 4
Jcan,
I'm not a json peron but I would guess that json would offer a very compact way to do this.
The good news for non-json folks is that standard javascript is also very compact for an indexed array comprising numbers or strings or a mixture of the two. It gets more complex if it contains objects and/or is associative, but not insurmountable.
For the simple case (numbers and strings):
delimiter = '^';
var myPostString = myArray.join(delimiter);
Choose a delimiter that will never appear in your arrays. It can be multiple characters if you want, eg, delimiter = '£%^&'; .
Then use the most appropriate technique for building your post (ie HTML form vs ajax http request object). I'm guessing you know how to do this.
Server-side, in php, simply use explode() to convert the posted string back to an array:
$delimiter = '^';
$myVar = explode(delimiter, $_POST['myVar']);
At this point it will be an array of strings - it can't be otherwise. Because php automatically type converts, much like js, you may not need to explicitly convert numbers back, but it may depend on what you are going to do with the values. Just be aware.
Hope this helps.Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
HTTP supports three methods of sending character data as part of a request:GET (data frms part to the URL)
POST (data is included within the request)
COOKIE (data previously stored at the client is included within the request)
On submission of a request, these become available in PHP as the associative arrays:$_GET
$_POST
$_COOKIE
A composite associative array $_REQUEST is also available, comprising everything in $_GET, $_POST and $_COOKIE.
Always sanitise values with eg. hrmlspecialshars(), or intval() before using especially when building sql, otherwise your PHP scripts are open to the dreaded "code injection", which in extrmis means goodbye database!
That's the basics.
Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
a lot of you gave some good thing but i am making on drop auto submit Post method to php script that will but the built array from the javascript in to the database in the proper order one of you show how to submit it with a form i don't want a form i want it to auto submit on sort element drop and ajax will auto submit to php with out effecting the current page besides changing the order look at myyearbook.com for intents but if i find what i need before you guy figure out what i mean ill pass the information on to you all
jcanaway
Junior Poster in Training
51 posts since Apr 2009
Reputation Points: 14
Solved Threads: 4
no i don't expect any script from any just to point me some where where i can learn about it properly
jcanaway
Junior Poster in Training
51 posts since Apr 2009
Reputation Points: 14
Solved Threads: 4
ok i think i found what i need with HttpObject.open im not sure dose any know if i can use this i am still look at info on it so i can produce what i need but it is just a question
jcanaway
Junior Poster in Training
51 posts since Apr 2009
Reputation Points: 14
Solved Threads: 4
alright jQuery has a bunch of the thing i need to do this now i have to from my Array and send it to the php in post Method ya im figuring it out now
jcanaway
Junior Poster in Training
51 posts since Apr 2009
Reputation Points: 14
Solved Threads: 4
i know a lot about php I've developed a lot of advance php scripts I'm i just was trying to understand understand jquery they i understand how to use it now but the script examples were chopped down badly on something i know html and css and going through javascript very quickly because it is close to php script kind of but thanks and I'm using auto submit i have already finished it
jcanaway
Junior Poster in Training
51 posts since Apr 2009
Reputation Points: 14
Solved Threads: 4