15ongm 15ongm -4 years ago 93
PHP Question

Form selector failing $_POST['medalCount'] !== '' test

In my post form, I have a selector for users to select the number of medals. In my

getSearchFields()
function, if the selector isn't empty (aka the selector is not on Select Medal Count), the function completes task 1. However, the selector is failing the
$_POST['medalCount']!== ''
test and is doing task 1.
$_POST['medalCount']
is type string. How do I fix this?

<form action="index.php" method="post" name="search">
<select name="medalCount">
<option value=0 selected> Select Medal Count </option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
</select>
</form>

function getSearchFields(){
if($_POST['medalCount']!== ''){
//Task 1
$search_fields['medalCount'] =$_POST['medalCount'];

}
}

Answer Source

Change this

<option value=0 selected> Select Medal Count </option>

to this

<option value="" selected> Select Medal Count </option>

With all <option> elements having non-empty value attributes, you would never (normally) receive an empty string value in $_POST['medalCount'] and $_POST['medalCount']!== '' would always be true.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download