jquery check checkboxes based on php array - php

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.'>';
}

Related

Submitting HTML Form Values as Array (To PHP)

I've got a lot of radio boxes on a single page within my website.
I know it is possible to submit all of the options as an array that can be directly manipulated by PHP.
<input type="radio" name="SOMETHING_HERE" value="1" />
There are several radio groups and I'd like all of them to submit to one array.
All I'd like to know is the syntax which has to be used in the name="".
Use like this
<input type="radio" name="optionname[]" value="1" />
<input type="radio" name="optionname[]" value="2" />
.
.
You can have your form in either of the following ways.
<input type="radio" name="name[]" value="1" />
<input type="radio" name="name[]" value="2" />
Or
<input type="radio" name="question['question1']" value="1" />
<input type="radio" name="question['question2']" value="2" />
There by you setting the array definition yourself. Hope this answers your query.
HTML looks like this:
<input type="radio" name="name[]" value="1" />
<input type="radio" name="name[]" value="2" />
PHP accesses it like this:
<?php
$_POST['name'] = array('1','2');
Some things to note:
Order in array as compared to as it exists in HTML is not guaranteed

comma seperated checkbox $_GET query request with jquery

Trying to create a search function using multiple checkboxes, but seem to be only finding information related to $_POST requests.
<input type="checkbox" name="fruit" value="1">
<input type="checkbox" name="fruit" value="2">
<input type="checkbox" name="fruit" value="3">
<input type="checkbox" name="fruit" value="4">
<input type="checkbox" name="fruit" value="5">
What is the best way to get the query to look like this
search?fruit=1,2,3,4
Is there a way to do this non ajax?
Just to clarify...
Each value represents a different fruit.
Originally I did this
search?apple=1&orange=1
As I added more checkboxes, the query seemed inefficient.
I know that I can add the checkboxes into the array using the $_POST method
<input type="checkbox" name="fruit[]" value="1">
<input type="checkbox" name="fruit[]" value="2">
As a few have suggested using this technique for a $_GET query would look something like this
search?fruit[]=1&fruit[]=2&fruit[]=5
So the question is really how to clean it up (comma seperated)
NOTE: As others have pointed out, it isn't necessary to pass a single parameter with a comma-separated value to end up with an array or a comma-separated value on the server. Frameworks like PHP can handle this with no JavaScript required. You can simply give each checkbox the same "name" attribute. That will cause multiple parameters with the same name to be passed to the server, which is just fine. In fact, it is the same result you would get if you used a <select multiptle="multiple"> element.
In PHP, if you use a name with square brackets at the end, like fruit[], you can then get an array on the server with:
$_GET['fruit']
And if you want a comma-separated value on the server, you can use:
implode(',', $_GET['fruit'])
But if you really want a single parameter with a comma-separated value, here is how you can do it:
You can use a form with a hidden input. Set the form's "method" to "get" and the hidden input's "name" to "fruit". Then add an event handler that sets the value of the hidden input to the comma-separated string when the form is submitted.
HTML:
<form id="fruitForm" action="search" method="get">
<input type="hidden" name="fruit" />
<label><input type="checkbox" value="1" />apple</label><br />
<label><input type="checkbox" value="2" />banana</label><br />
<label><input type="checkbox" value="3" />orange</label><br />
<label><input type="checkbox" value="4" />pineapple</label><br />
<label><input type="checkbox" value="5" />grapefruit</label><br />
<button type="submit">submit</button>
</form>
JQuery:
$('#fruitForm').submit(function() {
var fruits = $('#fruitForm').find('input:checkbox:checked').map(function() {
return $(this).val();
}).get().join(',');
$('#fruitForm').find('input[name=fruit]').val(fruits);
});
Note: The checkbox elements do not have "name" attributes, so they do not get included in the form submission.
jsfiddle
Use fruit[] as the input name
<input type="checkbox" name="fruit[]" value="1">
<input type="checkbox" name="fruit[]" value="2">
<input type="checkbox" name="fruit[]" value="3">
<input type="checkbox" name="fruit[]" value="4">
<input type="checkbox" name="fruit[]" value="5">
You will get the following:
echo "<pre>", print_r($_GET["fruit"], true);
Output
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Try this, Use checkbox name as fruit[]. In php get the fruit array and converted as string using implode()
<?php
echo $fruits = implode(",",$_GET['fruit']); //1,2,3,4,5
?>
<form>
<input type="checkbox" name="fruit[]" value="1">
<input type="checkbox" name="fruit[]" value="2">
<input type="checkbox" name="fruit[]" value="3">
<input type="checkbox" name="fruit[]" value="4">
<input type="checkbox" name="fruit[]" value="5">
<input type="submit">
</form>

Process HTML checkboxes and their 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" />

how i can select multiple values from check box?

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']);

How to validate that a specific ammount of checkboxes are checked in PHP

I have to validate that maximum 3 checkboxes are clicked. There are 11. How could I do this efficiently and without testing every possible situation?
You can do like this:
if (count($_POST['checkbox_name']) === 3)
{
// your code here.....
}
where your checkbox names should be suffixed with [] eg:
<input type="checkbox" name="checkbox_name[]" value="1" />
<input type="checkbox" name="checkbox_name[]" value="2" />
<input type="checkbox" name="checkbox_name[]" value="3" />

Categories