Melanie Sumner
Melanie Sumner

Reputation: 180

despite defaultvalue being set, no value shows up in database

 <p>Special Meal Request:<br />
      <label>
      <input type="checkbox" name="dietaryveg" value="yes" />Vegetarian
      </label>
      <label><input type="checkbox" name="dietaryglut" value="yes" />Gluten-Free
      </label>

This is what I decided to go with for a form I created. I didn't use radio buttons because I only want one checkbox; I didn't give the checkboxes the same names because they are different columns in the database, since a user could need both a vegetarian meal AND a gluten free meal.

However, when I pull up my database in mysql workbench, unchecked boxes still show up with no value in the column. I tried then to put a hidden checkbox with a value of no and it's the default checked, etc. But then both values show up as no in the database.

Now, I realize that php only cares about a checked box value, and not an unchecked box value- but I would think that if there is a default value in my database, that should be in there, much like a NULL, wouldn't it?

I really don't want to have a completely separate table just for rows of food preferences- I'm sure that there's just something that I'm missing.

Could I get some advice on the best way to handle this one?

Upvotes: 0

Views: 82

Answers (2)

Major Productions
Major Productions

Reputation: 6042

What do you consider to be your default value? With HTML check boxes, the value attribute does not mean a default value, but rather the value the box will send along if it's checked. If one is not/none are checked, the value 'yes' will not be sent along.

Like mlishn said in his answer, you'll need to programatically check if one/both of the boxes are set, and if so, insert those values.

Upvotes: 0

mlishn
mlishn

Reputation: 1667

If you are doing something like : (isset($_POST['dietarygut'])) and it is not set, then the value will be NULL and uploaded to the db. Post your PHP script

Upvotes: 1

Related Questions