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'; }?> />
Related
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
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!!
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
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?
Im trying to populate the POST checkboxes this way
foreach ($chk as $key => $value) {
if (isset($_POST[$key])) $chk[$key][$_POST[$key]] = 'checked="checked"';
}
But for some reason is not populating them for the following checkboxes
<input type="checkbox" name="chk[]" value="A" />A
<input type="checkbox" name="chk[]" value="B" />B
<input type="checkbox" name="chk[]" value="C" />C
Any help will be appreciate it.
Checkboxes won't populate themselves by magic, you must actually insert the checked="checked" there. And I think you're no better off populating the data beforehand, this is usually the simplest way:
<input type="checkbox" name="chk[]" value="A" <?php if(isset($_POST['chk']['A'])) echo 'checked="checked"'; ?>/>A
<input type="checkbox" name="chk[]" value="B" <?php if(isset($_POST['chk']['B'])) echo 'checked="checked"'; ?>/>B
<input type="checkbox" name="chk[]" value="C" <?php if(isset($_POST['chk']['C'])) echo 'checked="checked"'; ?>/>C
The browser won't care whether you pre-populate some PHP variables in your script: It sees only the generated HTML. You need to write the "checked='checked'" directly into your HTML output.