How do I check a checkbox?
I've tried 1, On, and Yes. That doesn't work. Putting the worked "checked" alone works, but then how do I check with PHP after a form post of the checkbox is checked?
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
A checkbox will only be a successful control if it is checked.
Controls that are not successful are not submitted as data.
Therefore, you can tell if a checkbox is checked by seeing if its value has been submitted.
E.g.
if ($_POST['chk']['newmsg2'] == 1) {
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" <?php if ($_POST['chk']['newmsg2']): ?>checked="checked"<?php endif; ?> />
Here is the code;
<form action="test.php" method="POST">
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
<input type="submit">
</form>
<?php
$check = $_POST['chk']['newmsg2'];
echo "***$check****"
?>
If it is checked $check will show 1.
Related
I have this HTML code:
<form action="index.php" method="post">
<input id="firstCheck" type="checkbox" name="test[]" value="first" checked>
<label for="firstCheck">1</label><br>
<input id="secondCheck" type="checkbox" name="test[]" value="second" checked>
<label for="secondCheck">2</label><br>
<input type="submit" name="test_submit" value="Send">
</form>
Also I have this PHP code:
function create_cookies()
{
if (isset($_POST['test_submit'])) {
$checkboxes_array = $_POST['test'];
setcookie("testcookie1234556", json_encode($checkboxes_array), time()+3600);
}
}
create_cookies();
function check_cookies()
{
if (isset($_COOKIE['testcookie1234556'])) {
$my_cookie_array = json_decode($_COOKIE['testcookie1234556']);
return $my_cookie_array;
} else {
return false;
}
}
check_cookies();
As you can see, I have got all checked checkboxes and stored it to the php cookie.
Now, when someone is logging to the system, all checkboxes is checked by default. But I need them to be checked or unchecked based on the info from php cookie!
I mean, for example, if user logged on to the system and unchecked some checkboxes, logged off, then logged in again, previously unchecked checkboxes must remains unchecked.
Please, give me some advices for doing it.
Put your cookie in an array say
$cookie_aray=check_cookies();
and check it every time you load a check box, whether it was selected or not.
<html><form action="abc.php" method="post">
<input id="firstCheck" type="checkbox" name="test[]" value="first" <?php if(in_array('first',$cookie_aray)) echo 'checked';?>>
<label for="firstCheck">1</label><br>
<input id="secondCheck" type="checkbox" name="test[]" value="second" <?php if(in_array('first',$cookie_aray)) echo 'checked';?>>
<label for="secondCheck">2</label><br>
<input type="submit" name="test_submit" value="Send">
I have this html form with checkboxes that keep their status checked or unchecked after submitting the form and reloading the page:
<form method="post" action="">
<input type="checkbox" name="keyword1" value="keyword1" <?php if(isset($_POST['keyword1'])) echo "checked='checked'"; ?> />keyword1
<input type="checkbox" name="keyword2" value="keyword2" <?php if(isset($_POST['keyword2'])) echo "checked='checked'"; ?> />keyword2
<input type="checkbox" name="keyword2" value="keyword3" <?php if(isset($_POST['keyword2'])) echo "checked='checked'"; ?> />keyword3
<input type="submit" />
</form>
Problem is, that at first page load the checkboxes are unchecked. Is there any possibility to have all checkboxes with status checked at the beginning and then keep their new status after submit? So far I could not figure out how to do this. Any help would be much appreciated. Thanks,
you could use $_SESSION instead of $_POST :
<input type="checkbox" name="keyword1" value="keyword1" <?php if(isset($_SESSION['keyword1'])) echo "checked='checked'"; ?> />keyword1
And then put this on the top of your file :
session_start();
if (isset($_POST['my_form'])) {
if (isset($_POST['keyword1'])) {
$_SESSION['keyword1'] = 'checked';
}
}
I have a input in a form
<form name="frmAdd" method="POST" action="/index.php?a=save">
Status : <input type="checkbox" id="status" name="chkActive" value="" ><label for="status">Active</label>
</form>
but when I am calling the value of it, via $_POST['chkActive'], it's giving same value on that page. Whether I have checked the value or not.
Please tell me how can I know that this checkbox is checked or not (in PHP).
If you are using just a single checkbox you can do this :
<input type="checkbox" id="status" name="chkActive" value="1" >
within PHP
if (isset($_POST['chkActive'])) {
//its checked
}
but you need to ensure there is a value set within the HTML
<form name="frmAdd" method="POST" action="/index.php?a=save">
Status : <input type="checkbox" id="status" name="chkActive" value="1" ><label for="status">Active</label>
</form>
<?php if($_POST['chkActive']=='1'){
echo "is Active";
}else{
echo "not Active";
}
Checkbox only passes the value if is checked.
to clarify how it works.
<input type="checkbox" id="status" name="chkActive"
if the checkbox not ticked (checked by user) nothing sent to server. "chkActive" will not exist. So we use isset() to check it.
I have an array of checkboxes name="box[]". Through PHP I make sure that they're checked after they're submitted by echoing "checked='checked'" if they were checked at submit event.
Now, if I check the third box, the value jumps down to the first checkbox after submit, since the array was empty up until the third checkbox. Same, if I check the 2nd and 3rd checkbox, they jump down to 1st and 2nd after submit. This is the code I'm using:
<form method="post">
<input type="checkbox" name="box[]" value="true" <?php if ($box[0] == true) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="true" <?php if ($box[1] == true) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="true" <?php if ($box[2] == true) echo "checked='checked'"; ?>><br>
<p>
<input type="submit" value="Submit">
</form>
Try it at:
http://experiencerapanui.com/selecttest.php
Can I make the checkboxes fill up the array with a value "false" or whatever, if the box is unchecked? Which way should I go?
****** EDIT ******
Thanks to phant0m, I managed to come up with a solution:
<form method="post">
<input type="checkbox" name="box[]" value="1" <?php if (in_array("1", $box)) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="2" <?php if (in_array("2", $box)) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="3" <?php if (in_array("3", $box)) echo "checked='checked'"; ?>><br>
<p>
<input type="submit" value="Submit">
</form>
Putting unique values for the checkboxes, then if I find the value in the array $box[], the box is marked as checked.
This does not work, because only those checkboxes, that are checked, are being put into the $box array.
Either use different names, or different values to distinguish between them.
Consider this: You check the second and the third checkbox. In PHP, you will receive:
$_POST['box'] = array(0 => "true", 1 => "true");
You cannot know, which checkboxes have been checked, unless all of them are.
The POST value should start with isset, then !empty($array) determines if the POST value is an array and prevents a null array error when no options are selected.
&& is_array($_POST['box']) could be used in addition to !empty($_POST['box']) as well to check the validity of the array.
A variable is used in the following examples for the value field, as it makes it easier to define and populate inputs when using a foreach loop and may be sanitized if needed as a preventive measure.
It would probably be a good idea to sanitize the $_POST array also, and enclosing it in a function with the validation would allow it all to be called from the checkbox input and keep the input area tidy.
<input type="checkbox" name="box[]" value="<?php echo $unique_id; ?>" <?php if(isset($_POST['box']) && !empty($_POST['box']) && in_array($unique_id, $_POST['box'])) echo "checked='checked'"; ?>>
OR
<input type="checkbox" name="box[]" value="<?php echo $unique_id; ?>" <?php my_function(); ?>>
Excellent question and solutions! There seem to be relatively few examples that use an array method to preserve Post selections, and the one provided here is relevant and very helpful even years later.
<?php
session_start();
if (count($_POST) > 0) {
$_SESSION['link'] = $_POST['link'];
}
?>
<form method="post">
Gmail: <input type="checkbox" name="link" value="gmail" id="gmail" <?php if ($_SESSION['link'] == 'gmail') echo "checked"; ?>>
Hotmail: <input type="checkbox" name="link" value="hotmail" id="hotmail" <?php if ($_SESSION['link'] == 'hotmail') echo "checked"; ?>>
<input type="submit" value="Spara">
</form>
Problem is if a box is checked you have to uncheck that then check another to change.
Is there a way so it unchecks the checked one when i check another? that sounds weird...
Thanks
You could just use Radio buttons instead, e.g:
<input type="radio" name="rdGroup1" value="John"> John
<input type="radio" name="rdGroup1" value="Jane"> Jane
There is a reason God created radio buttons. :)