带有php功能的下拉菜单

时间:2013-02-01 11:01:57

标签: php function

我已经使用5个选项的下拉列表菜单创建了我的html表单

我想从下拉列表中选择一个选项来调用php函数,该函数将选定的选项回显到屏幕

<label>Fruits</label>
<select name="Fruits">
<option value="Ap">Apple</option>
<option value="BN">Banana</option>
<option value="OR">Orange</option>

2 个答案:

答案 0 :(得分:2)

例如,这是您的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>

现在,您将使用以下代码在下拉列表中使用上表。

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

答案 1 :(得分:0)

php在服务器端 所有html在用户端的位置.. 如果要回显所选选项,则必须使用Ajax请求将所选选项发送到服务器端,或使用POST表单等其他技术。

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