2013-02-01 241 views
1

我已經創建了HTML表單與5個選項下拉菜單

我想從下拉列表中選擇的選項調用PHP函數,將呼應所選的下拉列表菜單選項屏幕

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

你能解釋一下嗎? –

回答

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> 

0

PHP是在服務器端 其中所有的HTML,如果你想呼應選擇的選項是用戶側.. ,那麼你要發送選定的選項服務器端使用Ajax Request或使用某種其他技術(如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>