Setting $_POST checkboxes by default for PHP search - php

I have 3 checkboxes used for searching
<input type="checkbox" onChange="this.form.submit()" ',$var1 ? ' class="checkon" checked="checked"' : '','name="c1" value="c1">
<input type="checkbox" onChange="this.form.submit()" ',$var2 ? ' class="checkon" checked="checked"' : '',' name="c2" value="c2">';
<input type="checkbox" onChange="this.form.submit()" ',$var3 ? ' class="checkon" checked="checked"' : '',' name="c3" value="c3">';
I want them to all be on by default, but if I set them to on:
$_POST['c1'] = 'on';
Then when I uncheck the box it is still on? How do I get it on by default but off when I uncheck it?

I am not sure why you have an onChange event with these inputs. You should let the user check and uncheck whichever boxes they want and then submit all the data together. Then do if(isset($_POST['c1'])){ //Box was checked }. Also have checked="checked" just as a HTML attribute (so <input type="checkbox" name="c1" value="on" checked="checked" />) rather than doing a shorthand if statement in an onChange event.
Here would be the way I would do it:
<form action="" method="post">
<fieldset>
<input type="checkbox" name="c1" value="on" <?php if (isChecked('c1')) echo 'checked="checked"'; ?> />
<input type="checkbox" name="c2" value="on" <?php if (isChecked('c2')) echo 'checked="checked"'; ?> />
<input type="checkbox" name="c3" value="on" <?php if (isChecked('c3')) echo 'checked="checked"'; ?> />
<input type="submit" name="submit" value="Go" />
</fieldset>
</form>
<?php
function isChecked($name) { //Darkbee's edit
return empty($_POST) || isset($_POST[$name]);
}
if(isset($_POST['submit'])){
if(isset($_POST['c1'])){
//Checkbox1 was checked when the form was submitted, code goes here
}
//Do the same for c2 & c3 as necessary
}
?>
Hope this helps!!

Related

How will I populate value in textarea and radio buttons

I have fetch data from database in $tableRe. Now I have to print values in textarea and also have to check the radio button.
Here is my code,
$sql = "select (address, gender) from stud table";
if($result=mysqli_query($conn,$sql)) {
while($row = mysqli_fetch_array($result)) {
$tableRe[]=$row;
}
}
<form>
Address : <br>
<textarea value="<?php echo $tableRe[0]['address']; ?>"></textarea><br>
Gender : <br>
<input type="radio" value="Male">Male
<input type="radio" value="Female">Female <br>
<input type="submit" value="Save">
</form>
Please help me regarding this. Thanks in advance.
You need to apply condition on checked HTML attribute.
Try this:
<form>
Address : <br>
<textarea><?php echo $tableRe[0]['address']; ?></textarea> <br/>
Gender : <br>
<input type="radio" value="Male" <?php echo $tableRe[0]['gender'] == 'Male' ? 'checked' : ''; ?> >Male
<input type="radio" value="Female" <?php echo $tableRe[0]['gender'] == 'Female' ? 'checked' : ''; ?>>Female <br>
<input type="submit" value="Save">
</form>
Value has to be placed between the openning and closing tags :
<texterea name="whatever">Textarea value goes here</textarea>
To set a radio/checkbox as selected/checked, you need to add to it a "checkded" attribute :
<!-- HTML4 -->
<input type="radio" name="whatever" value="value1" checked="checked" /> Label 1
<input type="radio" name="whatever" value="value2" /> Label 2
<input type="radio" name="whatever" value="value3" /> Label 3
<!-- HTML5 -->
<input type="radio" name="whatever" value="value1" checked /> Label 1
<input type="radio" name="whatever" value="value2" /> Label 2
<input type="radio" name="whatever" value="value3" /> Label 3

How to Make Check box is checked

How can I make my check box is checked using PHP, when i visit the page later i want previously selected check boxes are checked
<input name="product[]" type="checkbox" value="1" />
Start Session as
<?php
session_start();
$session_products = array();
if(array_key_exists("products", $_SESSION))
{
if($_SESSION["products"] != null)
{
$session_products = $_SESSION["products"];
}
}
?>
Change your code as follows
<input name="product[]" type="checkbox" value="1" <?php if(in_array("1", $session_products)) echo "checked='checked'"; ?>/>
You can add a checked.
Here an example:
<input name="product[]" type="checkbox" value="1" checked />
By the way, your value=1 seems to be wrong. Normally you use `value to distinguish items.
Example:
<input name="product[]" type="checkbox" value="dvd" checked />
<input name="product[]" type="checkbox" value="cd" />
to do something like this
with php
<input name="product[]" type="checkbox" value="1" <?php echo ($value =="1") ? "checked" : ""; ?> />
where $value is the value from database

How to get the value of a checkbox in PHP

I'm having problem in getting the value of a checkbox when checking the value first in DB then change to POST value after submitting the form. Here's my sample:
1: I need to check first in my db if it's need to be checked or not.
<form action="" method="POST">
<input type="checkbox" name="chckbox" <?=$value['chckbox'] == 'ok' ? 'checked' : ''?> />
<input type="submit" name="btnsubmit" />
</form>
2: Then if I already check in DB I want to get the POST value of checkbox after submitting the form if it is checked or not.
<form action="" method="POST">
<input type="checkbox" name="chckbox" <?=isset($_POST['chckbox']) ? 'checked' : ''?> />
<input type="submit" name="btnsubmit" />
</form>
My question is... how to combined my two sample when I'm updating the form?
Any help is appreciated!
Thanks.
How about this,
<input type="checkbox" name="chckbox" <?php echo $value['chckbox'] == 'ok' || isset($_POST['chckbox']) ? 'checked' : '' ?> >
After submitting the form,
<?php
$checked = $value['chckbox'] == 'ok'?'checked':'';
if(isset($_POST['chckbox']) && $_POST['chckbox']=='on'){
$checked = "checked";
}elseif(count($_POST)>0 && !isset($_POST['chckbox'])){
$checked = "";
}
?>
<input type="checkbox" name="chckbox" value="on" <?php echo $checked; ?> >
html:
<form action="" method="POST">
<input type="checkbox" name="code" value="'.$row['Code'].'">
<input type="submit" name="btnsubmit" />
</form>
php:
$Code=$_REQUEST['code'];

if checkbox is checked change the value using PHP

I'm trying to accomplish one simple task (but is not simple for me).
I have a form and I'm having a trouble with this check box.
<input type="checkbox" name="b"
<?php if (isset($_POST[b])) echo "value='y'"; else echo "value='n'"; ?>/>
I'm not sure if I use the right one, but it doesn't work for me.
So basically I want the value of the input of b will be y if the checkbox is checked else it will always be n if the checkbox is unchecked.
That's not how a checkbox works.
It's checked when the checked attribute is there.
<input type="checkbox" name="a" value="a" checked /> Checked
<input type="checkbox" name="a" value="a" /> NOT Checked
So you want to use
<input type="checkbox" name="a" value="a" <?php echo isset($_POST['b']) ? "checked" : ""; ?>/>
Now if $_POST['b'] is set, the checkbox will be checked.
Also, you have $_POST[b]. The b should be in quotes. It should be $_POST['b']
You have to use two conditions one is for showing checked/unchecked and second for showing y/n
<input type="checkbox" name="b" <?php echo (isset($_POST['b'])?"value='y'":"value='n'")?>
<?php echo (isset($_POST['b'])?"checked":"") ?> />
tested code!
<form method="post">
<input type="checkbox" name="b" <?php if (isset($_POST['b'])) echo "value='y'"; else echo "value='n'"; ?>/>
<input type="submit" name="asd" value="asd">
</form>
So go with the following
<?php if (isset($_POST['b'])) echo "value='y'"; else echo "value='n'"; ?>
This worked for me:
first: Giving the checkbox a default value
then: assign the desired value only if the box is checked.
<input type="hidden" name="b" value="n">
<input type="checkbox" name="b" value="y" >
*Approved by Deployment:
<input type="radio" name="dep_approval_status" value="Approved"
<?php
if ($deploy['dep_approval_status'] === "Approved")
{
echo ' checked';
}
?>
/> Yes, approved
<input type="radio" name="dep_approval_status" value="Not Approved"
<?php
if ($deploy['dep_approval_status'] === "Not Approved")
{
echo ' checked';
}
?>
/> Not Approved

PHP If Variable isset add input with checkbox checked not working

I am having a problem with some code...
Here is the code:
<?php
if (isset($myVar)) {
echo '<input type="checkbox" name="ck1" id="ck1" checked />Checked/Not checked';
}
?>
I've tried checked, checked="checked" checked="true" ...but for some reason the checkbox doesn't come out checked.
it should workd...
try the following to debug
die("var->" . $myVar);
if().....
as for the checkbox....
<input type="checkbox" name="vehicle" value="Car" checked="checked" />
Should be: checked="yes"
So <input type="checkbox" name="ck1" id="ck1" checked="yes" />
I have gotten this to work in the past:
<input type="checkbox" name="name" value="value" <?php if($var == 'N'){echo 'checked'; }?> />

Categories