|
|
|
How to carry an array value from php to javascript?
Explanation
At some point of time we will be having a requirement to pass values from php array to a javascript array. For example let us consider a situation where we read some parameter from a file and use it in javascript. Here is how we can pass the values. Follow the steps to do it yourself
Step 1:
We read a file "im.txt" as array. The file will be read in such a way that each line will be a value in array. So if we have 10 lines in "im.txt" we will have the array length as 10.
| <script language="javascript">
scriptAr = new Array(); // initializing the javascript array
<?php
$file = "im.txt";
$lines = file($file); // read file values as array in php
$count = count($lines); //this gives the count of array
//In the below lines we get the values of the php array one by one and
update it in the script array.
foreach ($lines as $line_num >= $line)
{
print "scriptAr.push(\"$line\" );"; // This line updates the script
array with new entry
}
?>
</script> | |
Note: Please note that the php tags are inside the javascript tag.
|
|
| Array values from javascript to php Categories : PHP, Java Script, Arrays | | | PHP Array to Javascript Object Categories : PHP, Arrays, Java Script | | | enhanced date picker with jcript checking for a dynamic date input Categories : PHP, Java Script, Date Time, Calendar, Arrays | | | Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript. Categories : PHP, Java Script, DOM XML, XML, Arrays | | | PHP Script to find url links in a page Categories : PHP, URLs, Regexps, 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 | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | |
|
|
|