How to Make Check box is checked - php

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

Related

Setting $_POST checkboxes by default for PHP search

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!!

Using a default value with an html form radio button

I have a web form that updates a customer record. The entries are prefilled with the data stored in mysql when called. One field shows if equipment was returned as follows:
<font size=5>Returned:</font><input type="text" name="ud_Returned" value="<?php echo $rtrnd; ?>" /><br />
My question is I setup to convert this to radio button like:
<font size=5>Returned:</font><input type="radio" name="ud_Returned" value=Yes /> Yes<br />
<input type="radio" name="ud_Returned" value="No" /> No<br />
But I want it so the value stored in the $rtrnd variable fills in the existing radio button with the appropriate Yes/No for that customer when form is called. Any ideas?
Maybe something like this would do the trick:
<input type="radio" name="ud_Returned" value="Yes" <?php if($rtrnd) echo 'checked'; ?> />
<input type="radio" name="ud_Returned" value="No" <?php if(!$rtrnd) echo 'checked'; ?> />
Assuming that $rtrnd is a boolean value. If it isn't just use a comparison in the if statements like if($rtrnd == 'yes').
Assuming your store this information as 1 or 0 in the database you'll have to -
<?php
// Get stuff from database
$result = /* The resulting array from your query */;
if( $result["ud_Returned"] == 1 )
{
echo('<font size=5>Returned:</font>
<input type="radio" name="ud_Returned" value="Yes" checked="checked" /> Yes<br />
<input type="radio" name="ud_Returned" value="No" /> No<br />');
}
else
{
echo('<font size=5>Returned:</font>
<input type="radio" name="ud_Returned" value="Yes" /> Yes<br />
<input type="radio" name="ud_Returned" value="No" checked="checked" /> No<br />');
}
I'm not suggesting that this is the exact implementation you should pursue but this is the logic - get information from database, make a comparison, add the checked attribute to the appropriate radio button.

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'; }?> />

PHP set the value of a radio group dynamically?

To allow a user to edit information in a record, this is done:
$case=$_GET['case'];
$query="SELECT * FROM `cases` WHERE `case`= '$case'";
$result=mysql_query($query);
<input type="text" name="firstname" value="<?php echo $firstname; ?>" />
I need to set the value of a radio group based on what its value is in the "cases" table.
<input type="radio" name="flight1_departing" value="AM" />
<input type="radio" name="flight1_departing" value="PM" />
How is this possible?
<input <?php if ($somevalue == 'AM') echo 'checked="checked"'; ?> type="radio" name="flight1_departing" value="AM" />
<input <?php if ($somevalue == 'PM') echo 'checked="checked"'; ?> type="radio" name="flight1_departing" value="PM" />
Given a known value $val, you just need to check it against each radio button value and set the checked attribute, eg
<input type="radio" name="flight1_departing" value="AM"
<?php if ($val == 'AM') : ?>checked="checked"<?php endif ?>
/>
<input type="radio" name="flight1_departing" value="PM"
<?php if ($val == 'PM') : ?>checked="checked"<?php endif ?>
/>
That example is very manual. It would be easier if the radio elements are created in a loop.
Your questions is a little ambiguous but I'm going on the assumption that you mean you need to determine which value is default checked based on the value in the cases table?
Something like,
<input type="radio" name="flight1_departing" value="AM" <?php if ($some_cases_value) { print 'CHECKED'; } ?>/>
<input type="radio" name="flight1_departing" value="PM" <?php if ($some_cases_value) { print 'CHECKED'; } ?> />
Though there is likely a very more elegant way of doing it?

Categories