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.
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
apologies if this is simple, I am trying to grab the selections from multiple checkboxes.
I have created multiple checkboxes which look as follows:
<input type="checkbox" name="checkbox" value="a" id="selection">
<input type="checkbox" name="checkbox" value="b" id="selection">
<input type="checkbox" name="checkbox" value="c" id="selection">
I then can have a button and retrieve the form data using codeigniters built in form helper:
$temp = $this->input->post('checkbox');
if I select more than one checkbox however, and try to echo $temp, I only see one selection that a person has made. any ideas - ideally I don't want to use JS.
Many thanks in advance
You just need to add square brackets after checkbox name, like this:
<input type="checkbox" name="checkbox[]" value="a" class="selection">
<input type="checkbox" name="checkbox[]" value="b" class="selection">
<input type="checkbox" name="checkbox[]" value="c" class="selection">
This will pass checkboxes as array. Now you will get $this->input->post('checkbox') as something like this:
Array ( [0] => b [1] => c )
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'; }?> />
How can I process the checkboxes only if they're checked and grab the value of the checked ones only.
php
if (is_array($_POST['add'])) {
foreach ($_POST['add'] as $key => $value) {
$_POST['add'][$key] = mysql_real_escape_string(stripslashes($value));
}
}
html
<input type="checkbox" id="wmeet_ce"
value="ce"
name="add[wmeet]"
title="Wanting To Meet"
class="checkbox {validate:{required:true,minlength:1}}"/>
<input type="checkbox" id="wmeet_sf"
value="sf"
name="add[wmeet]"
class="checkbox"/>
<input type="checkbox" id="wmeet_sm"
value="sm"
name="add[wmeet]"
class="checkbox" />
Only checked checkboxes are ever presented to PHP, so your PHP code is correct.
However, your HTML isn't correct, as all your checkboxes have the same name. This means PHP will only ever see one of them.
To get an array of checkboxes you either need to give your checkboxes unique names like this
<input type="checkbox" id="wmeet_ce"
value="ce"
name="add[ce]"
title="Wanting To Meet"
class="checkbox {validate:{required:true,minlength:1}}"/>
<input type="checkbox" id="wmeet_sf"
value="sf"
name="add[sf]"
class="checkbox"/>
<input type="checkbox" id="wmeet_sm"
value="sm"
name="add[sm]"
class="checkbox" />
Or use the empty box technique like this.
<input type="checkbox" id="wmeet_ce"
value="ce"
name="add[]"
title="Wanting To Meet"
class="checkbox {validate:{required:true,minlength:1}}"/>
<input type="checkbox" id="wmeet_sf"
value="sf"
name="add[]"
class="checkbox"/>
<input type="checkbox" id="wmeet_sm"
value="sm"
name="add[]"
class="checkbox" />
I have a php array containing the mysql values of checkboxes, which has been selected previously. I am trying to do an edit page of sorts which will show the already selected checkboxes, but seem to be having issues with it. I've tried different ways but can't seem to get it working.
Here's my php array of previously selected checkboxes:
Array
(
[0] => 1
[1] => 3
)
And here's my checkboxes:
<input type="checkbox" name="company[]" id="company[]" value="1">
<input type="checkbox" name="company[]" id="company[]" value="4">
<input type="checkbox" name="company[]" id="company[]" value="2">
<input type="checkbox" name="company[]" id="company[]" value="3">
I can't seem to work out how to get the checkboxes (from the php array - value 1 and 3) to already be selected..
Here's a server side solution to do it when the page is created.
<?php
function check_checked($index,$check_array){
if (in_array($index,$check_array)){ echo 'checked="checked"';}
}
$checked=array(1,3);
?>
<input type="checkbox" name="company[]" id="company[]" value="1" <?php check_checked(1,$checked);?>>
<input type="checkbox" name="company[]" id="company[]" value="4" <?php check_checked(4,$checked);?>>
<input type="checkbox" name="company[]" id="company[]" value="2" <?php check_checked(2,$checked);?>>
<input type="checkbox" name="company[]" id="company[]" value="3" <?php check_checked(3,$checked);?>>
If you were going to do it with JavaScript, I'd suggest printing the array into a JS var with json_encode and going from there. Server side makes more sense, though, since you already have the data to start with.
<input type="checkbox" name="company[]" id="company[]" value="1" checked>
If you specifically want jQuery to do it:
http://www.electrictoolbox.com/check-uncheck-checkbox-jquery/
The simpliest way is to do it on the server side:
foreach ($array as $value) {
$che = $value? "checked":"";
print '<input type="checkbox" name="company[]" id="company[]" value="1" '.$che.'>';
}