Jump to content

Using php with javascript


bird_man11

Recommended Posts

Hey guys i new to this forum and are having some trouble with double combo boxes, i have a javascript script that allows me to do this fine but the data i want to be put in the second combo box is stored into a database. this is my code at the moment. the while loop in the php seems to be the problem

 

This is the original javascript:

 

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

<script>
<!--

/*
Double Combo Script Credit
By JavaScript Kit (www.javascriptkit.com)
Over 200+ free JavaScripts here!
*/

var groups=document.doublecombo.example.options.length
var group=new Array(groups)
for (i=0; i<groups; i++)
group[i]=new Array()

group[0][0]=new Option("JavaScript Kit","http://javascriptkit.com")
group[0][1]=new Option("News.com","http://www.news.com")
group[0][2]=new Option("Wired News","http://www.wired.com")

group[1][0]=new Option("CNN","http://www.cnn.com")
group[1][1]=new Option("ABC News","http://www.abcnews.com")

group[2][0]=new Option("Hotbot","http://www.hotbot.com")
group[2][1]=new Option("Infoseek","http://www.infoseek.com")
group[2][2]=new Option("Excite","http://www.excite.com")
group[2][3]=new Option("Lycos","http://www.lycos.com")

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(group[x][i].text,group[x][i].value)
}
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>

 

 

 

And this is the altered one i have done:

 

<script>
<!--

/*
Double Combo Script Credit
By JavaScript Kit (www.javascriptkit.com)
Over 200+ free JavaScripts here!
*/

var groups=document.doublecombo.example.options.length
var group=new Array(groups)
for (i=0; i<groups; i++)
group[i]=new Array()
<?php
$i = 0;
$lensdesignq = mysql_query("select LensDesign from lenstypes group by lensdesign");
while ($option2 = mysql_fetch_array($lensdesignq, MYSQL_ASSOC)){
$lenstype = $option2['LensDesign'];
$lenstypeq = mysql_query("select LensType from lenstypes WHERE lensdesign = '". $lenstype . "' group by lenstype");
$j = 0;
while ($option3 = mysql_fetch_array($lenstypeq, MYSQL_ASSOC)){ ?>
group[<?php echo '"'.$i.'"';?>][<?php echo '"'.$j.'"';?>]=new Option("<?php echo '"'.$option3["LensType"].'"';?>")
<?php
$j = $j + 1; 
}
$i = $i + 1;
}
?>


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(group[x][i].text)
}
temp.options[0].selected=true
}
//-->
</script>

 

any help would be great guys

 

cheers

Link to comment
https://forums.phpfreaks.com/topic/37084-using-php-with-javascript/
Share on other sites

your php code executes server side and should only be outputting valid javascript code because of its position in the code.  I assume you meant to do that because it looks like you were trying to that.  The php probably just returns invalid js.  Just look at the js in the html source code returned to the browser and you probably will see a simple syntax error in the js produced which you can then trace back to the php code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.