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';
}
}
Related
I know there's "PHP keep checkbox checked after submitting form" on here, but that thread does not solve my problem, because I have multiple checkbox, what I need is when you check a checkbox, this stay checked after submit.
At the moment with this code nothing happens, I tried another way but when I check "id7" checkbox, all the checkbox get checked.
I have to know which checkbox was checked by the id that I give it, but I do not know how.
while ($fila = mysql_fetch_array($rs)) {
echo utf8_encode("
<tr>
<td>
".$fila['title']."
</td>
");?>
<td>
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist[]']) && is_array($_POST['checklist[]']) && in_array('$fila', $_POST['checklist[]'])) echo 'checked="checked"'; ?> />
</td>
<?php
}
}
?>
First, the value of checkbox is $fila['id'] so when you are checking, use $fila['id'] instead of $fila. Also, when PHP receive array input fields with [] in their names the [] will be removed so that the correct POST variable is $_POST['checklist'].
Try changing this line:
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist[]']) && is_array($_POST['checklist[]']) && in_array('$fila', $_POST['checklist[]'])) echo 'checked="checked"'; ?> />
to
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist']) && is_array($_POST['checklist']) && in_array($fila['id'], $_POST['checklist'])) echo 'checked="checked"'; ?> />
"name" is like a variable name - by using checklist[] you're defining an array as the variable.
Why not just name each checkbox properly? When the form is submitted, use the contents of $_POST to set each variable into the user's $_SESSION.
If the page is refreshed, use the values from $_SESSION to determine if the checkbox should be ticked. Something like (untested):
<input type="checkbox" name="vehicle" value="Bike"
<?php if (isset($_SESSION['bike_checked']) echo 'checked'; ?>> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car"
<?php if (isset($_SESSION['car_checked']) echo 'checked'; ?>> I have a car<br>
<input type="submit" value="Submit">
two problem i have:
first:
i want users see my form with no prechecked in radio buttons but it does when they see it at first time(as you see)(explain that i use checked to show user which radio he/she selected after pushing the button)
second:
why when i name submit button "select sex" and push it in the form, it doesn't echo "it's done" but when i name it "select" it works?! i want my submit name has two words.
and the codes:
<html>
<body>
<?php
if(isset($_POST['select sex']))
echo "it's done";
?>
<form name="input" action="" method="post">
<input type="radio" name="sex" value="male" checked="
<?php if(isset($_POST['select sex']) and $_POST['sex']=='male') echo 'checked'; else echo '';?>
"> Male<br />
<input type="radio" name="sex" value="female" checked="
<?php if(isset($_POST['select sex']) and $_POST['sex']=='female') echo 'checked'; else echo '';?>
"> Female<br />
<input type="submit" name="select sex" value="Submit" />
</form>
</body>
For checkbox, not mentioned checked attribute for any of the check box option.
For submit button, normally we are not using the space in field name and this is the best practice. Though you have used then you have written the wrong code. Please check below updated code line for if statement.
if(isset($_POST['select sex']))
I cant seem to get my php script to recognize that the checkbox in my form is checked.
I want to accomplish this:
If checkbox is checked, the php script should submit to my DB.
PHP:
<?php
if (!empty($_POST['approve_student'])) {
if (isset($_POST['approve'])) {
//submit
} else {
//do nothing
}
}
?>
FORM:
<input class="checkbox" name="approve" type="checkbox" id="approve">
<label name="approve" for="approve"><span><div data-textbox="1" ></div></span></label>
<input class='button_submit_2' name="approve_student" type="submit" value='Submit'>
NOTES:
Im using Jquery to keep checkbox checked during session.
Im using CSS to customize checkbox.
I dont know if these two might corrupt anything.
Hope u can help.
Your code is fine it is working
if (!empty($_POST['approve_student'])) {
if (isset($_POST['approve'])) {
echo "Approved";
} else {
echo "Not Approved";
}
}
<form action="index.php" method="post">
<input class="checkbox" name="approve" type="checkbox" id="approve">
<label name="approve" for="approve"><span><div data-textbox="1" ></div></span></label>
<input class='button_submit_2' name="approve_student" type="submit" value='Submit'>
</form>
If the checkbox isn't checked then the value shouldn't be coming through in the first place, so this should work;
if(isset($_POST["testvariabel"])){
//whatever you wanna do here
}
or if it's always coming through...
Change the markup to something like this, setting a value property
<input type="checkbox" class='form' onclick="this.value=!this.value" value=true name="checkbox[]" />
and to get the values submitted, use a simple loop
if($_POST['checkbox'] == 0){
echo $checkbox . ' ';
}
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.
<?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. :)