| | |
how do i pass arrays in javascript to php using POST method
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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??
located here
http://jqueryui.com/demos/sortable/#portlets
do you know where i can find what i need??
Canaway Productions
I'm not sure if this will work:
startpage
process.php
startpage
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html> <head> <title>test page</title> <script type="text/javascript"> var submitJArray = function() { var jArray = [ "One", "Two", "Three"]; document.getElementById("hiddenF").value = jArray; };</script> </head> <body> <form id="testform" action="process.php" method="post" onsubmit="return submitJArray();"> <div> <input type="hidden" id="hiddenF" name="hiddenF" value=""> Dummy Field: <input type="text" id="txt" name="txt" value="" size="30"><br><br> <input type="submit" value="submit"> </div> </form> </body> </html>
process.php
php Syntax (Toggle Plain Text)
<html> <body> <p> <?php $jArray = $_POST['hiddenF']; $pArray = new Array( $jArray ); foreach ( $pArray as $getArray ) { echo $getArray."<br>"; } ?> </p> </body> </html>
Last edited by essential; Jun 11th, 2009 at 8:41 pm.
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):
Choose a delimiter that will never appear in your arrays. It can be multiple characters if you want, eg,
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:
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
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):
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
delimiter = '^'; var myPostString = myArray.join(delimiter);
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
$delimiter = '^'; $myVar = explode(delimiter, $_POST['myVar']);
Hope this helps.
Airshow
Last edited by Airshow; Jun 11th, 2009 at 9:18 pm.
HTTP supports three methods of sending character data as part of a request:
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
- 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)
- $_GET
- $_POST
- $_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
Last edited by Airshow; Jun 12th, 2009 at 8:33 am.
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
Canaway Productions
![]() |
Similar Threads
- Passing data from forms from one page to another (JavaScript / DHTML / AJAX)
- NEED help to pass javascript variable to php (PHP)
- JavaScript/PHP Sessions (JavaScript / DHTML / AJAX)
- fetching arrays from ajax, php/zf (PHP)
- passing arrays back and forth php(zend)/javascript (PHP)
- Pass a javascript variable to PHP in the same function (JavaScript / DHTML / AJAX)
- how to send data in POST method using javascript (JavaScript / DHTML / AJAX)
- HYPERLINK with PHP POST (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Pass id of anchor to use in function: JQUERY
- Next Thread: Javascript getelemtnByID woes
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array beta box browser captchaformproblem cart child class close codes column css date debugger decimal dependent design disablefirebug dom download editor element embed engine enter error events explorer ext file firefox focus form forms frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe index java javascript javascripthelp2020 jquery jsf jsp jump libcurl listbox maps masterpage math media menu microsoft mimic mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php position post problem programming prototype redirect safari scale scriptlets scroll search security select software toggle unicode variables w3c web webservice window windowofwords \n





