0

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">&lt;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">&gt; 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?

3
  • 1
    wouldnt it make more sense to use the number in the first place instead of switch/casing it like this? Commented Apr 15, 2016 at 5:36
  • Why you are checking "<200 cal", as your values don't contain 'cal' word, either add it in value in HTML <option> or remove it from case Commented Apr 15, 2016 at 5:43
  • @fittaoee please select the correct answer, so in future if somebody refers this thread can come to know what was the correct solution. Commented Apr 15, 2016 at 5:46

4 Answers 4

1

Value return in $_POST['nutri-info'] is like 200 - 399 and you are checking in switch case with 200 - 399 cal.

Try to remove cal from switch case or add cal in option value

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Can you explain why the value in $_POST['nutri-info'] will only contain ""200 - 399", but not "cal"?
$_POST['nutri-info'] return value of option and if value attribute not in option then it will return text of option.
So does it suggest that, if I add "cal" into the value="" attribute in <option>, then it will also be passed into the $_POST[] variable?
If you have value in option tag then it will return that as a submitting value otherwise it will return text inside option tag --> <option>text</option>
0

The switch should include exactly the values you have set in the HTML select.

switch($nutrition){
        case "<200":
           $calories = 1;
           break;
        case "200 - 399":
           $calories = 2;
           break;
        case "400 - 599":
           $calories = 3;
           break;
        case "600 - 799":
           $calories = 4;
           break;
        case "800 - 999":
           $calories = 5;
           break;
        case ">1000":
           $calories = 6;
           break;         
        default:
            $calories = 10;   
}

Just remove the cal part

Comments

0

change your options value to this

<option value="200 - 399 cal">200 - 399 cal</option>
<option value="400 - 599 cal">400 - 599 cal</option>
<option value="600 - 799 cal">600 - 799 cal</option>

...do like this

Beacuse form submiting the form element value

Comments

0

It's great you learned how to correct the mistake with the switch statement..!

You may find it useful to learn one more thing: The value attributes of your options are for you, not for your user. What I mean is, they don't see it, so you can put whatever you want in there.

So, rather than passing yourself a string description of the selection (which requires you to 'figure out' the integer value using the switch), just simply pass yourself the integer as the value to begin with!

So instead of:

<select class="sel-dropdown" name="nutri-info">
     <option value="empty"></option>     
     <option value="<200">&lt;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">&gt; 1000 cal</option>
</select>

Do this:

<select class="sel-dropdown" name="nutri-info">
     <option value=""></option>     
     <option value="1">&lt;200 cal</option>
     <option value="2">200 - 399 cal</option>
     <option value="3">400 - 599 cal</option>
     <option value="4">600 - 799 cal</option>
     <option value="5">800 - 999 cal</option>
     <option value="6">&gt; 1000 cal</option>
</select>

Then you get exactly the value you want, without needing the switch at all:

$calories = $_POST['nutri-info'];

// The switch is no longer needed..!
//
// $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;   
//     }

The moral of the story is: set the value attributes so they are meaningful for YOU. The user only sees what is in between the angled brackets:

<option value="2"> 200 - 399 cal </option>

So, that's all that has to be meaningful for them.

1 Comment

Ah yeah! Very clever! Didn't think about doing it this way. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.