php 带有php功能的下拉菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14644863/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
drop-down menu with php function
提问by TSHUKELA GEORGE
I've already created my html form with a drop-list menu of 5 selections
我已经创建了一个包含 5 个选项的下拉列表菜单的 html 表单
I want a selected option from the drop-list to call php function that will echo a selected option to the screen
我想要从下拉列表中选择一个选项来调用 php 函数,该函数会将所选选项回显到屏幕上
<label>Fruits</label>
<select name="Fruits">
<option value="Ap">Apple</option>
<option value="BN">Banana</option>
<option value="OR">Orange</option>
回答by Usman Ali
For example, here is your html code
例如,这是您的 html 代码
<html>
 <head></head>
 <title>Static Dropdown List</title>
 <body bgcolor="pink">
 Employee List :
 <select>
  <option value="Select">Select</option>}
   <option value="Moyed">Moyed Ansari</option>
   <option value="Asad">Asadullah</option>
   <option value="Usman">Usman Ali</option>
 </select> 
 </body>
</html>
Now You will use the above table in the dropdown list using the following code.
现在您将使用以下代码在下拉列表中使用上表。
 <html>
<head>
<title>Dynamic Drop Down List</title>
</head>
<BODY bgcolor ="pink">
    <form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
        Employee List :
        <select Emp Name='NEW'>
        <option value="">--- Select ---</option>
        <?
            mysql_connect ("localhost","root","");
            mysql_select_db ("company");
            $select="company";
            if (isset ($select)&&$select!=""){
            $select=$_POST ['NEW'];
        }
        ?>
        <?
            $list=mysql_query("select * from employee order by emp_id asc");
        while($row_list=mysql_fetch_assoc($list)){
            ?>
                <option value="<? echo $row_list['emp_id']; ?>"<? if($row_list['emp_id']==$select){ echo "selected"; } ?>>
                                     <?echo $row_list['emp_name'];?>
                </option>
            <?
            }
            ?>
        </select>
        <input type="submit" name="Submit" value="Select" />
    </form>
  </body>
回答by Ali Haider
php is on the server side where as all the html is on the user side.. if you want to echo the selected option, then you have to send the selected option to the server side using Ajax Request or using some other technique like POST a form.
php 在服务器端,而所有的 html 都在用户端。形式。
// using jquery you can get your selected value
  <script>
     var selected_value = $("#idOfTheSelectTag").val();
     // or you can also do
     var selected_value = $("#idOfTheSelectTag :selected").text();
     // now you can post it to the php page using AJAX where you can echo it
     $.post('page_name.php', {SELECTED_VALUE : selected_value}, function(data){
                         // on the server side in page_name.php file if you echo the value then it will be returned in the data object in the function
                  });
  </script>

