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

do you know where i can find what i need??

Recommended Answers

All 12 Replies

I'm not sure if this will work:


<title>test page</title>
<script type="text/javascript">

var submitJArray = function() {
var jArray = [ "One", "Two", "Three"];
   document.getElementById("hiddenF").value = jArray;
<form id="testform" action="process.php" method="post" onsubmit="return submitJArray();">
<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">


$jArray = $_POST['hiddenF'];

$pArray = new Array( $jArray );
   foreach ( $pArray as $getArray ) {
   echo $getArray."<br>";


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.


ok I saw this on another page. so I am not sure how it will work. but as a data when you send
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)

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.


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 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

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...


no i don't expect any script from any just to point me some where where i can learn about it properly

ok i think i found what i need with 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

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

You need to learn a lot about PHP, HTML, and JavaScript, too.

The main thing is that HTML can sent data to server (eg. to a PHP script) only from one form by collecting all INPUTs (and SELECTs) values/names pairs, compose them in a POST or GET format and sent the result to the server.

If you have collected some data by JavaScript into an Array variable, before you can sent the data to server (PHP script), you definitely must have a <FORM > prepared, and you can append some <INPUT > elements to it. The suggested hidden type was recommended because it does not appear on the page.

I offer some other code to give you more idea not solving:

<title>test page</title>

<script type="text/javascript">

var submitJArray = function(frm) 
{ var jArray = ["Zero", "One", "Two", "Three"];
  for (var i=0;i<jArray.length;i++)
  { var newHidInp = document.createElement('input');
        newHidInp.type  = 'hidden';  = 'outArray[]';
        newHidInp.value = jArray[i];
<form action="#" method="post" onSubmit="return submitJArray(this);">
  <input type="submit" value="send">

<?php print_r($_POST);?>



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

You are welcome :-)
I cannot help you with jQuery, because I have not meet it yet, not even been looking for. I originaly spent lot of time with developping the PHP interface to MySQL databases by generating pure HTML code server side. But it was anoying to sending lot of data between client and server on each user click hence I did accept a piece of client side code, and so stated with JavaScript. The similarity of C based programing languages is very confusing while the same code could be uderstood very differently by various languages. More over the use of JavaScript is closely binded with DOM which is, unfortunately, subject of interperetation of various browser developper.

The gu-ru of JavaSript arviced me, to keep the standard, and I try to make my codes as simple as is possible, without the necessity to detect the browser signature and respect its peculiar behavior.
For the database management it is sufficient, because nobody cares of the graphic layout and formating. I suppose the incorporating the too many graphics into the html pages as misunderstanness of this medium, because more than 15 years the PDF is developed parallely and it brings the full layout and format control, fonts included.

But sorry for the gossiping, I forgot, I do not write my testament :-)


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.