chadpeppers
chadpeppers

Reputation: 2057

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

So I have a select option list with values in it. But as with most select option lists the first one states dummy text for the user. The issue is that when the $_POST variable is carried over the isset value is true, but is there a way to make it false? Here is just an example. The bottom always echos true even if trying to use the dummy text. Frustrating or maybe I am doing this wrong? BTW THIS IS JUST EXAMPLE CODE

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

Upvotes: 0

Views: 2240

Answers (2)

xkeshav
xkeshav

Reputation: 54016

Two Way

1) set value="0" for first option

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

and check with empty() instead of isset() because

empty() return false for "0"

2) use disabled attribute for first option

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

and then you can check with 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>

Upvotes: 2

COLD TOLD
COLD TOLD

Reputation: 13579

you can set specific value in the form first option and then make sure in not selected

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

Upvotes: 0

Related Questions