1

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?

2
  • 1
    to check your checkbox use the attribute 'checked' : <input type="checkbox" name="Value 1" checked> Commented Feb 28, 2017 at 8:34
  • to expand a bit: <input type='checkbox' name='someName' <?php if ($someValue==1) echo 'checked="checked"'>. this will set the checked attribute if your value matches a condition. Hope that helps? Commented Feb 28, 2017 at 10:32

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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"'; ?> >

Comments

0

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"/>

1 Comment

THANKS to all of you guys!!!! :))) thats why i never found a solution online, because i was looking from a wrong perspective. thanks a lot, now it works fine!!!

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.