I currently have a page called "user.php", where I have a HTML form that allows users to select from a list of nutrition-related information, and some PHP code that handles the input and eventually store them into a MySQL database:
<form action="user.php" method="post">
......
<select class="sel-dropdown" name="nutri-info">
<option value="empty"></option>
<option value="<200"><200 cal</option>
<option value="200 - 399">200 - 399 cal</option>
<option value="400 - 599">400 - 599 cal</option>
<option value="600 - 799">600 - 799 cal</option>
<option value="800 - 999">800 - 999 cal</option>
<option value=">1000">> 1000 cal</option>
</select>
......
</form>
And the PHP at the top of the page is as follows. What I'm trying to do is to convert each range of calories into a number, and store that number into the database.
...
$nutrition = $_POST['nutri-info'];
switch($nutrition){
case "<200 cal":
$calories = 1;
break;
case "200 - 399 cal":
$calories = 2;
break;
case "400 - 599 cal":
$calories = 3;
break;
case "600 - 799 cal":
$calories = 4;
break;
case "800 - 999 cal":
$calories = 5;
break;
case ">1000 cal":
$calories = 6;
break;
default:
$calories = 10;
}
But the problem is that, no matter which item I select in the HTML form, the result of $calories is always 10, the value of the default case. Any idea on what might gone wrong?
<200 cal", as your values don't contain 'cal' word, either add it in value in HTML<option>or remove it fromcase