简体   繁体   English

使用PHP函数的Javascript下拉列表?

[英]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 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. 问题是,它调用Javascript中的值,但是我想根据您在菜单中选择的选项来调用PHP中的特定函数。

<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. PHP是在服务器端,HTML和Javascript客户端执行的。

This means no, there is no way you can execute PHP inside your Javascript (never ever). 这意味着,没有, 没有办法 ,你可以你的Javascript(永远)内执行PHP。

You can only post information back to the server, via ajax, which allows you to make further operations. 您只能通过ajax将信息发布回服务器,这使您可以进行进一步的操作。 When these are finished on the server, your ajax call receives the result and returns it back to the client for further processing. 在服务器上完成这些操作后,您的ajax调用将接收结果并将其返回给客户端以进行进一步处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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