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

Join Date: Apr 2009
Posts: 30
Reputation: jcanaway is an unknown quantity at this point 
Solved Threads: 0
jcanaway's Avatar
jcanaway jcanaway is offline Offline
Light Poster

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

 
0
  #1
Jun 11th, 2009
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??
Canaway Productions
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

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

 
0
  #2
Jun 11th, 2009
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

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 846
Reputation: Airshow is on a distinguished road 
Solved Threads: 121
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

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

 
0
  #3
Jun 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 48
Reputation: yilmazhuseyin is an unknown quantity at this point 
Solved Threads: 5
yilmazhuseyin's Avatar
yilmazhuseyin yilmazhuseyin is offline Offline
Light Poster

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

 
0
  #4
Jun 12th, 2009
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)
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 846
Reputation: Airshow is on a distinguished road 
Solved Threads: 121
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

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

 
0
  #5
Jun 12th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: jcanaway is an unknown quantity at this point 
Solved Threads: 0
jcanaway's Avatar
jcanaway jcanaway is offline Offline
Light Poster

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

 
0
  #6
Jun 12th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

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

 
0
  #7
Jun 12th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: jcanaway is an unknown quantity at this point 
Solved Threads: 0
jcanaway's Avatar
jcanaway jcanaway is offline Offline
Light Poster

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

 
0
  #8
Jun 12th, 2009
no i don't expect any script from any just to point me some where where i can learn about it properly
Canaway Productions
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: jcanaway is an unknown quantity at this point 
Solved Threads: 0
jcanaway's Avatar
jcanaway jcanaway is offline Offline
Light Poster

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

 
0
  #9
Jun 12th, 2009
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
Canaway Productions
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: jcanaway is an unknown quantity at this point 
Solved Threads: 0
jcanaway's Avatar
jcanaway jcanaway is offline Offline
Light Poster

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

 
0
  #10
Jun 13th, 2009
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
Canaway Productions
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC