Reputation: 23
I made a small user database with some checkboxes in a html input form. In there I have some basic infos about the users like value 1 and 2 (among others) and some checkboxes as well (here you see one). Default is always 0, but when I click the checkbox its changes to 1.
<input type="text" placeholder="Value 1" value="<?php echo set_value('value1'); ?>" name="value1" size="6"/>
<input type="hidden" value="0" name="spec1"/><input type="checkbox" value="1" name="spec1"/>
<input type="text" placeholder="Value 2" value="<?php echo set_value('value2'); ?>" name="value2" size="6"/>
Now I would like to have a form in which I can see all these informations later on and edit it if necessary. But the checkbox I can't display properly. It is always empty. So I had to change it to a textbox. So I can see whether that specific values are 0 or 1.
<input type="text" name="Value 1" value="<?php echo $value1; ?>">
<input type="text" name="spec1" value="<?php echo $spec1; ?>">
<input type="text" name="Value 2" value="<?php echo $value2; ?>">
Is it possible somehow to show the values in a checkbox again, so I can change the type of the second line to "checkbox" again instead of "text"? Means can I display a value 1 as a checked checkbox and 0 as an empty checkbox?
Upvotes: 1
Views: 4344
Reputation: 2007
What checks a checkbox is the checked
attribute not value
You can add that attribute depending on the value you are retrieving from your database like so:
<?php if($value == 1): ?>
<?php echo '<input type="checkbox" checked name="spec1"/>' ?>
<?php else: ?>
<?php echo '<input type="checkbox" name="spec1"/>' ?>
<?php endif; ?>
or for short
<input type="checkbox" <?php if(value == 1) echo 'checked'; ?> name="spec1"/>
Upvotes: 0
Reputation: 5690
check this code , you can use checkbox and check value is set and you need to show user so you checkbox level then you can use
<input type="checkbox" name="Value 1" value="<?php echo $value1; ?>"><?php echo $value1; ?>
<input type="checkbox" name="spec1" value="<?php echo $spec1; ?>"> <?php echo $spec1; ?>
<input type="checkbox" name="Value 2" value="<?php echo $value2; ?>"> <?php echo $value2; ?>
for demo test check this code also
<input type="checkbox" name="Value 1" value="1">Test1
<input type="checkbox" name="spec1" value="2"> Test2
<input type="checkbox" name="Value 2" value="3"> Test3
and if you want to checkbox checked then use also this
<?php
$value1 = 1;
$spec1 = 2;
$value2 = 3;
$compare_value = 3;
?>
<input type="checkbox" name="Value 1" value="1" <?php if($value1 ==$compare_value) { echo 'checked';}?>>Test1
<input type="checkbox" name="spec1" value="2" <?php if($spec1 ==$compare_value) { echo 'checked';}?>> Test2
<input type="checkbox" name="Value 2" value="3" <?php if($value2 ==$compare_value) { echo 'checked';}?>> Test3
Upvotes: 2
Reputation: 4534
Checkboxes aren't checked or not by their value attribute. They have a checked attribute: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox
So in your example:
<input type="checkbox" name="..." <?php if($value1) echo 'checked="checked"'; ?> >
Upvotes: 1