View Full Version : passing PHP variable to javascript
mpjbrennan
11-11-2002, 11:46 AM
Is it possible to pass a variable created by PHP to a javascript function so that subsequent processing of that variable can be done client-side?
For example, create an array based upon client form input and then write a page in which that array can be repeatedly queried by an embedded javascript?
patrick
firepages
11-11-2002, 02:01 PM
Hi, you simply need to output a valid javascript array via PHP, eg
<?$array=array('all','your','scripts','are','belong','to....');?>
<script language="javascript">
var myarray=new Array('<?echo implode("','",$array);?>');
</script>
mpjbrennan
11-11-2002, 03:21 PM
thanks firepages
patrick
pudong
05-27-2003, 09:22 PM
I have a similar problem. I need to pass 2-dimensional array to javascript. Can someone give me an example?
Thanks
just do as said above, only output a 2- or n-dimensional array...
pudong
05-28-2003, 05:32 PM
This is what I want: make second dropdown list box responding to the selection of first list box. The data array $grp comes from php. What has to be done to make it work? Thanks.
<form name="doublecombo">
<p><select name="example" size="1" onChange="redirect(this.options.selectedIndex)">
<option>Technology Sites</option>
<option>News Sites</option>
<option>Search Engines</option>
</select>
<select name="stage2" size="1">
<option value="http://javascriptkit.com">JavaScript Kit</option>
<option value="http://www.news.com">News.com</option>
<option value="http://www.wired.com">Wired News</option>
</select>
<input type="button" name="test" value="Go!"
onClick="go()">
</p>
<?php
$grp[0][0]= "JavaScript Kit";
$grp[0][1]="News.com";
$grp[0][2]="Wired News";
$grp[1][0]="CNN";
$grp[1][1]="ABC News";
$grp[1][2]="NBC";
//$grp[2][0]=new Option("Hotbot","http://www.hotbot.com");
$grp[2][1]="Infoseek";
$grp[2][2]="Excite";
$grp[2][3]="Lycos";
for ($i=0; $i<3; $i++) {
for ($j=0; $j<3; $j++)
echo "{$grp[$i][$j]}";
}
?>
<script language="javascript">
<!--
/*
Double Combo Script */
var groups=3
var group=new Array(groups)
for (i=0; i<groups; i++) {
group[i]=new Array()
for (j=0; j<3; j++) {
grouptext[i][j] = <?php echo "$grp[i][j])"; ?>
groupvalue[i][j] = j
}
}
var temp=document.doublecombo.stage2
function redirect(x){
for (m=temp.options.length-1;m>0;m--)
temp.options[m]=null
for (i=0;i<group[x].length;i++){
temp.options[i]=new Option(grouptext[x][i],groupvalue[x][i])
}
temp.options[0].selected=true
}
function go(){
location=temp.options[temp.selectedIndex].value
}
//-->
</script>
</form>
<p align="center"><font face="arial" size="-2">This free script provided by</font><br>
<font face="arial, helvetica" size="-2"><a href="http://javascriptkit.com">JavaScript
Kit</a></font></p>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.