你能做一个选择选项列表不设置$_POST值吗?


Can you make a select option list not set a $_POST value?

所以我有一个包含值的选择选项列表。 但与大多数选择选项列表一样,第一个选项列表为用户声明了虚拟文本。 问题是,当 $_POST 变量被转移到 isset 值上时,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>

双向

1) 为第一个选项设置值="0"

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

并检查empty()而不是 isset(),因为

empty()返回 false 表示"0"

2) 使用disabled属性作为第一个选项

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

然后你可以检查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>

您可以在表单第一个选项中设置特定值,然后确保未选中

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