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

Recommended Answers

All 12 Replies

I'm not sure if this will work:

startpage

<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

<html>
<body>
<p>
<?php 
$jArray = $_POST['hiddenF'];

$pArray = new Array( $jArray );
   foreach ( $pArray as $getArray ) {
   echo $getArray."<br>";
   }
?>
</p>
</body>
</html>

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

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)

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

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

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

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

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:

<html>
<head>
<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';
        newHidInp.name  = 'outArray[]';
        newHidInp.value = jArray[i];
    frm.appendChild(newHidInp);
  }
}
</script>
</head>
<body>
<form action="#" method="post" onSubmit="return submitJArray(this);">
  <input type="submit" value="send">
</form>

<?php print_r($_POST);?>

</body>
</html>

Jan

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

Jan

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.