2014-02-28 160 views
1

如何從一個選擇選項值中獲取多個值。 這裏有一個例子,從一個選擇選項值中獲取多個值。 PHP HTML

<html> 

<form method="post" action="#"> 
    <select name="retrive_destination"> 
     <?php while($row = mysql_fetch_assoc($result)){ 
        $destinationID = $row['destinationID']; 
        $destination_place = $row['destination_place']; 
        echo '<option value="'.$destinationID.'">'.$destination_place.'</option> 
     ?> 
     <input type="submit"> 
</select> 

</form> 
</html> 

我如何獲得這兩個值「$目標-ID」和一個選項值「$ destination_place」,當我提交表單,因爲我需要兩個值,當用戶選擇的一個選項下一步。謝謝你們的歡呼聲。

回答

4

撰寫兩個值,然後爆炸,他們的服務器端

<html> 

<form method="post" action="#"> 
    <select name="retrive_destination"> 
     <?php while($row = mysql_fetch_assoc($result)){ 
        $destinationID = $row['destinationID']; 
        $destination_place = $row['destination_place']; 
        echo '<option value="'.$destinationID."_".htmlentities($destination_place).'">'.$destination_place.'</option> 
     ?> 
     <input type="submit"> 
</select> 

</form> 
</html> 

PHP服務器端

$value= $_REQUEST['retrive_destination']; 
$explode=explode("_",$value,2); 
$destinationID =$explode[0]; 
$destination_place =$explode[1]; 
+1

這是我正想作爲回答,你可能想要包括前者plode聲明以及如果他不知道如何使用它。 – steinmas

+1

也在$ destination_place'「>' – SajithNair

+0

之間加一個點作爲建議編輯答案 –

0

最好是有形式只提交了ID,你可以隨時獲取目的地從數據庫放置在服務器端。

2

jQuery的版本:

添加隱藏表單上輸入類型:

<form method="post" action="#"> 
<select name="retrive_destination" id="des"> 
    <option value="1">Ibiza</option> 
    <option value="2">Mallorca</option> 
    <option value="3">Chile</option> 
</select> 
<input type="hidden" name="destination" id="dest"/> 
<input type="submit" /> 
</form> 

的Jquery:

$(document).ready(function(){ 
$("#des").change(function(){ 
    $("#dest").val($("#des option:selected").text()); 
}); 
}); 

PHP:

$destId = $_POST['retrive_destination']; 
$destVal = $_POST['destination']; 
相關問題