943,633 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jun 11th, 2009
0

how do i pass arrays in javascript to php using POST method

Expand Post »
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??
Similar Threads
Reputation Points: 14
Solved Threads: 4
Junior Poster in Training
jcanaway is offline Offline
51 posts
since Apr 2009
Jun 11th, 2009
0

Re: how do i pass arrays in javascript to php using POST method

I'm not sure if this will work:

startpage
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>test page</title>
  4. <script type="text/javascript">
  5.  
  6.  
  7. var submitJArray = function() {
  8. var jArray = [ "One", "Two", "Three"];
  9. document.getElementById("hiddenF").value = jArray;
  10. };</script>
  11. </head>
  12. <body>
  13. <form id="testform" action="process.php" method="post" onsubmit="return submitJArray();">
  14. <div>
  15. <input type="hidden" id="hiddenF" name="hiddenF" value="">
  16. Dummy Field: <input type="text" id="txt" name="txt" value="" size="30"><br><br>
  17. <input type="submit" value="submit">
  18. </div>
  19. </form>
  20. </body>
  21. </html>

process.php

php Syntax (Toggle Plain Text)
  1. <html>
  2. <body>
  3. <p>
  4. <?php
  5. $jArray = $_POST['hiddenF'];
  6.  
  7. $pArray = new Array( $jArray );
  8. foreach ( $pArray as $getArray ) {
  9. echo $getArray."<br>";
  10. }
  11. ?>
  12. </p>
  13. </body>
  14. </html>
Last edited by essential; Jun 11th, 2009 at 8:41 pm.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Jun 11th, 2009
0

Re: how do i pass arrays in javascript to php using POST method

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):
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. delimiter = '^';
  2. 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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $delimiter = '^';
  2. $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
Last edited by Airshow; Jun 11th, 2009 at 9:18 pm.
Sponsor
Reputation Points: 300
Solved Threads: 357
WiFi Lounge Lizard
Airshow is offline Offline
2,524 posts
since Apr 2009
Jun 12th, 2009
0

Re: how do i pass arrays in javascript to php using POST method

ok I saw this on another page. so I am not sure how it will work. but as a data when you send
arrayElement=1&arrayElement=2&arrayElement=3
you can send all the elements to server. and you can retreived it from server too. (unfortunately I dont know how you can do it with php)
Reputation Points: 31
Solved Threads: 5
Light Poster
yilmazhuseyin is offline Offline
48 posts
since Oct 2006
Jun 12th, 2009
0

Re: how do i pass arrays in javascript to php using POST method

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
Last edited by Airshow; Jun 12th, 2009 at 8:33 am.
Sponsor
Reputation Points: 300
Solved Threads: 357
WiFi Lounge Lizard
Airshow is offline Offline
2,524 posts
since Apr 2009
Jun 12th, 2009
0

Re: how do i pass arrays in javascript to php using POST method

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
Reputation Points: 14
Solved Threads: 4
Junior Poster in Training
jcanaway is offline Offline
51 posts
since Apr 2009
Jun 12th, 2009
0

Re: how do i pass arrays in javascript to php using POST method

Hmm... You're such a demanding person, w/o even thinking that you are getting all of this solutions for free. You don't deserve any help from here...


Tsk...Tsk...Tsk...
Last edited by essential; Jun 12th, 2009 at 4:00 pm.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Jun 12th, 2009
0

Re: how do i pass arrays in javascript to php using POST method

no i don't expect any script from any just to point me some where where i can learn about it properly
Reputation Points: 14
Solved Threads: 4
Junior Poster in Training
jcanaway is offline Offline
51 posts
since Apr 2009
Jun 12th, 2009
0

Re: how do i pass arrays in javascript to php using POST method

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
Reputation Points: 14
Solved Threads: 4
Junior Poster in Training
jcanaway is offline Offline
51 posts
since Apr 2009
Jun 13th, 2009
0

Re: how do i pass arrays in javascript to php using POST method

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
Reputation Points: 14
Solved Threads: 4
Junior Poster in Training
jcanaway is offline Offline
51 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Pass id of anchor to use in function: JQUERY
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript getelemtnByID woes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC