Matt
Matt

Reputation: 187

Check box will not update with a tick from mysql

I'm creating a form that has two checkboxes yes and no. I have added a yes or no into a mysql field called Completed.

The problem I have is getting the tick boxes to show a tick if the field that has a Yes or a No.

So depending what is in the field Completed "Yes" or "No" the tick boxes should be ticked or not ticked.

My code is like this:

<input type="checkbox" name="Completed" Completed="Yes" value="<?php echo $stuff["Completed"]; ?>" <?php echo $stuff["Completed"] ? 'checked=" checked"' : ''; ?> > Yes</td>

<input type="checkbox" name="Completed" Completed="No" value="<?php echo $stuff["Completed"]; ?>" <?php echo $stuff["Completed"] ? 'checked=" checked"' : ''; ?> > No</td>

Upvotes: 0

Views: 263

Answers (2)

Evernoob
Evernoob

Reputation: 5561

Check boxes don't strike me as an appropriate control for this type of information. Have you considered using radio buttons instead? In any case, the answer to your question depends on how you are storing the data in the database. If it is a simple "Yes" or "No", then try something like the following:

<input type="radio" name="Completed" value="Yes" <?php echo $stuff["Completed"] == 'Yes' ? 'checked="checked"' : ''; ?> />

Upvotes: 2

tonymarschall
tonymarschall

Reputation: 3882

<?php echo $stuff["Completed"] ? 'checked=" checked"' : ''; ?> will always return true because you check for 'Yes' and 'No'. This values are true for php. You have to set the values 0 or 1 in db or check do something like this:

<?php echo $stuff["Completed"] == 'Yes' ? 'checked=" checked"' : ''; ?>

...

<?php echo $stuff["Completed"] == 'No' ? 'checked=" checked"' : ''; ?>

Upvotes: 2

Related Questions