2012-06-27 58 views
0

所以我有一個選擇選項列表中的值。但是與大多數選擇選項列表一樣,第一個列出了用戶的虛擬文本。問題是,當$ _POST變量通過isset值是真的,但是有沒有辦法使它失效?這只是一個例子。即使嘗試使用虛擬文本,底部總是回聲正確。令人沮喪或者我做錯了?順便說一句,這只是示例代碼

<?php 
if(isset($_POST['curreny_pairs'])): 
    echo "true"; 
endif; 
?> 
<html> 
    <form action="blah" method="post"> 
     <select name="currency_pairs" id="currency_pairs"> 
      <option selected="selected">Currency Pair</option> 
      <option value="AUDCAD" >AUD/CAD</option> 
      <option value="AUDCHF" >AUD/CHF</option> 
     </select>  
     <input type="submit" value="submit"> 
    </form> 
</html> 

回答

2

雙向

1)設置值=」 0「第一選項

<option selected="selected" value="0">Currency Pair</option> 

並與empty()代替isset()函數因爲

empty()回報爲 「0」

2)使用第一個選項

<option selected="selected" disabled="disabled" >Currency Pair</option> 

disabled屬性,然後您可以檢查與isset()

<?php 
if(!empty($_POST['curreny_pairs'])): 
echo "true"; 
endif; 
?> 

<html> 
<form action="blah" method="post"> 
<select name="currency_pairs" id="currency_pairs"> 
<option selected="selected" value="0">Currency Pair</option> 
<option value="AUDCAD" >AUD/CAD</option> 
<option value="AUDCHF" >AUD/CHF</option> 
</select> 

<input type="submit" value="submit"> 
</form> 
+1

非常感謝,爲我工作。 – chadpeppers

0

您可以在表單第一個選項設置特定的值,並確保在不選擇

<?php 
    if(isset($_POST['curreny_pairs']) && $_POST['curreny_pairs']!="false") 
    echo "true"; 
    endif; 
    ?> 
    <html> 
    <form action="blah" method="post"> 
    <select name="currency_pairs" id="currency_pairs"> 
    <option value="false" selected="selected">Currency Pair</option> 
    <option value="AUDCAD" >AUD/CAD</option> 
    <option value="AUDCHF" >AUD/CHF</option> 
    </select> 

    <input type="submit" value="submit"> 
    </form> 
    </html> 
相關問題