|
|
|
How to carry an array value from javascript to php?
Explanation
The javascript array values can be passed to a php file using the following method :
Step 1: Let us consider we have a js array as
| <script language=javascript>
scriptAr = new Array();
scriptAr[0] = "uno";
scriptAr[1] = "dos";
scriptAr[2] = "tres";
</script> | |
Step 2: Now we will create a hidden form field as follows
| <form action="phpArrayTest.php" method=post name=test onSubmit=setValue()>
<input name=arv type=hidden>
<input type=submit>
</form> | |
Here what we have done is, when the submit is called we first do some work ("onSubmit=setValue()") by using the onSubmit method. The onSubmit method will invoke setValue() function defined by us.
After that the action will take place and stringTokens.php will be called.
Step 3: Here we define the setValue method. The method will convert the array periviously defined in to a string and then set it to the hidden field.
| <script language=javascript>
function setValue()
{
var arv = scriptAr.toString();
// This line converts js array to String document.test.arv.value=arv;
// This sets the string to the hidden form field. }
</script> | |
Step 4:In the php file the string will be split back into array.
Let us consider we get a value from a post action one,two,three
| | i.e: $ss = $_POST['arv']; | |
Now using the php method explode() we will convert the string in to a array object :
| explode(separator,string);
Example : $tok = explode(',',$ss); | |
so we have got a string array. Now we print the array using print_r() method. This method prints the array in string format.
| print_r($tok);
The result
Array ( [0] => one [1] => two [2] => three ) | | |
|
| Array values from php to javascript Categories : PHP, Java Script, Arrays | | | enhanced date picker with jcript checking for a dynamic date input Categories : PHP, Java Script, Date Time, Calendar, Arrays | | | PHP Array to Javascript Object Categories : PHP, Arrays, Java Script | | | Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript. Categories : PHP, Java Script, DOM XML, XML, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | Zephyr: AJAX Based Framework for PHP5 Developers Categories : PHP, AJAX, Frameworks, Java Script, Web Applications | | | clearing variables in php3 Categories : Variables, Arrays, PHP | | | Create HTML forms dynamicly using Javascript & PHP Categories : PHP, PHP Classes, Java Script | | | Linked comboboxes with php-mysql & javascript Categories : PHP, Java Script, Databases, MySQL | | | Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other? Categories : PHP, Math., Arrays | | | The toll booth Categories : PHP, Java Script, Filesystem | | | MD5 secured login Categories : PHP, Java Script, Authentication, Security | | | Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | | | Dump the contents of a PHP variable in html format with a recursive list of subfolders and files from a given root directory.
Categories : PHP, Directories, Variables, Arrays | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | |
|
|
|