简体   繁体   中英

Javascript Drop-Down with PHP Function?

I am using this code I found to try and make this drop down menu. http://www.javascriptkit.com/script/cut183.shtml

Ignore my variables and values, they are all place holders.

The problem is, it calls the value in Javascript, but I want to call a specific function in PHP depending on what option in the menu you chose.

<html>
<body>
<form name="doublecombo" form action"index.php" method="POST">
<p><select name="example" size="1" onChange="redirect(this.options.selectedIndex)">
<option>Amazon</option>
<option>Apple</option>
<option>Logitech</option>
<option>Nike</option>
</select>
<select name="stage2" size="1">
<option value="http://javascriptkit.com">Kindle Fire</option>
<option value="http://www.news.com">Kindle DX</option>
<option value="http://www.wired.com">Kindle Charger</option>
<option value="http://www.microsoft.com">Kindle Paperweight</option>
</select>
<input type="button" name="test" value="Generate"
onClick="gen()">
</p>

<script>
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("Kindle Fire","http://javascriptkit.com")
group[0][1]=new Option("Kindle DX","http://www.news.com")
group[0][2]=new Option("Kindle Charger","http://www.wired.com")
group[0][2]=new Option("Kindle Paperweight","http://www.microsoft.com")

group[1][0]=new Option("MacBook","http://www.cnn.com")
group[1][1]=new Option("iPhone","http://www.abcnews.com")
group[1][2]=new Option("iPad","http://www.yahoo.com")
group[1][3]=new Option("iMac","http://www.apple.com")

group[2][0]=new Option("G602 Wireless Gaming Mouse","http://www.hotbot.com")
group[2][1]=new Option("G19s Gaming Keyboard","http://www.infoseek.com")
group[2][2]=new Option("G430 Surround Sound Gaming Headset","http://www.excite.com")
group[2][3]=new Option("PowerShell Controller","http://www.lycos.com")

group[3][0]=new Option("Nike FuelBand","http://www.nike.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 gen(){
location=temp.options[temp.selectedIndex].value
}
</script>
<?
function kindlegen(){
?>
<textarea name="message" placeholder="CODES" rows="10">
<?
{ $amount = "5"; $i = 1; while ($i <= $amount) 
{ $rand_letter1 = substr(str_shuffle("123456789"), 0, 2); $ran = rand(1, 6); echo "D0FB     A0A0 343".$ran." 0A".$rand_letter1."\n"; $i++; } } ?>
</textarea>
<?
}
?>

</form>
</html>
</body>

PHP is executed server side, HTML and Javascript client side.

This means no, there is no way you can execute PHP inside your Javascript (never ever).

You can only post information back to the server, via ajax, which allows you to make further operations. When these are finished on the server, your ajax call receives the result and returns it back to the client for further processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM