WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Array values from javascript to php
Categories : PHP, Java Script, Arrays Click here to Update Your Picture
Fernando Dulanto
Date : Sep 22nd 2006
Grade : 2 of 5 (graded 2 times)
Viewed : 8960
File : 4500.TXT
Images : No Images for this code example.
Search : More code by Fernando Dulanto
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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