I have PHP script that sets checked="checked" to checkboxes based on the database record. However all, but the first occurence of the checked checkbox are displaying checked. Here is what HTML looks like when it browser parses it:
<input type="checkbox" id="not_online"><label for="not_online">Not Online</label>
<input type="checkbox" id="facebook" checked="checked"><label for="facebook">Facebook</label>
<input type="checkbox" id="twitter" checked="checked"><label for="twitter">Twitter</label>
And this is what I see in FF26.0
[ ] Not Online
[ ] Facebook
[×] Twitter
What could cause the issue?
Add name to each of your checkbox fields:
<input type="checkbox" id="not_online" name="not_online"><label for="not_online">Not Online</label>
<input type="checkbox" id="facebook" name="facebook" checked="checked"><label for="facebook">Facebook</label>
<input type="checkbox" id="twitter" name="twitter" checked="checked"><label for="twitter">Twitter</label>
Its is working fine for me what problem are you having?
Here is my fiddle
http://jsfiddle.net/dbDj3/
using your code
<input type="checkbox" id="not_online"><label for="not_online">Not Online</label>
<input type="checkbox" id="facebook" checked="checked"><label for="facebook">Facebook</label>
<input type="checkbox" id="twitter" checked="checked"><label for="twitter">Twitter</label>
It must be some other part of your code that is causing problems
Related
I'm new to checkboxes. I want to let users do a search based on three possible filters represented by checkbox lists. For example, if I used the form below, I'd like the user to be able to include all shapes that are (red or blue) and large. The advice I've been able to find about checkbox queries hasn't hit this issue exactly. Is there a way I can do this with one MySQL query?
<form action="dbdquery.php" method="get">
<p>
Color:
<br/>
<input type="checkbox" name="color[]" value="Include All" checked/> Include All<br/>
<input type="checkbox" name="color[]" value="Red"/> Red<br/>
<input type="checkbox" name="color[]" value="Blue"/> Blue<br/>
<input type="checkbox" name="color[]" value="Yellow"/> Yellow<br/>
</p>
<p>
Size:
<br/>
<input type="checkbox" name="size[]" value="Include All" checked/> Include All<br/>
<input type="checkbox" name="size[]" value="Small"/> Small<br/>
<input type="checkbox" name="size[]" value="Medium"/> Medium<br/>
<input type="checkbox" name="size[]" value="Large"/> Large<br/>
</p>
<p>
Shape:
<br/>
<input type="checkbox" name="shape[]" value="Include All" checked/> Include All<br/>
<input type="checkbox" name="shape[]" value="Round"/> Round<br/>
<input type="checkbox" name="shape[]" value="Square"/> Square<br/>
<input type="checkbox" name="shape[]" value="Irregular"/> Irregular<br/>
</p>
<input type="submit" value="Search">
</form>
Try this:
Use implode function,
$colors = implode("," , $_GET['color']);
$size = implode("," , $_GET['size']);
$shape = implode("," , $_GET['shape']);
Query:
select * from table where color in ($colors) or size in ($size) or shape in ($shape);
You need to add condition to check Include all. (if user check select all the variables include all the checking values)
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 multiple checkboxes on my form:
<input type="checkbox" name="animal" value="Cat" />
<input type="checkbox" name="animal" value="Dog" />
<input type="checkbox" name="animal" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['submit']) {
echo $_POST['animal'];
}
I get "Bear", i.e. the last chosen checkbox value even though I picked all three. How to get all 3?
See the changes I have made in the name:
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
you have to set it up as array.
print_r($_POST['animal']);
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['animal'])){
foreach($_POST['animal'] as $animal){
echo $animal;
}
}
use square brackets following the field name
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
On the PHP side, you can treat it like any other array.
hi i have multiple option in check box and when visitor or customer select multiple option then how i can get multiple values? plz explain with code thanks
Name the checkboxes with [] (or PHP will drop all but one of them (I don't recall if it is the first or last)).
<input type=checkbox name="foo[]" value="some value">
Then they will be accessible as an array in the $_GET or $_POST superglobal.
$_GET['foo'][]
Basically, set all the name tags to be the same for all your checkboxes (with []). Then in your script, the values will be available as an array
Html:
<input type="checkbox" name="tags[]" value="1" />
<input type="checkbox" name="tags[]" value="2" />
<input type="checkbox" name="tags[]" value="3" />
<input type="checkbox" name="tags[]" value="4" />
PHP:
print_r($_REQUEST['tags']);
Reference: http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html
Like this
<input type="checkbox" name="foo[]" value="bar" />
<input type="checkbox" name="foo[]" value="baz" />
<input type="checkbox" name="foo[]" value="qux" />
<?php
print_r($_POST['foo']);
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.'>';
}