comma seperated checkbox $_GET query request with jquery - php

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>

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

Checkbox with the same name POST

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 )

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

getting multiple checkboxes names/id's with php

How can i get the names or id's of the multiple selected checkboxes on submit, using the PHP? Following is example form. Thanks.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="orange" id="orange">
<input type="checkbox" name="apple" id="apple">
<input type="checkbox" name="sky" id="sky">
<input type="checkbox" name="sea" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
Checkbox values are submitted from a form only if the checkbox is selected. What's more, it's the name attribute that counts, not the ID.
There are several ways of handling checkboxes in PHP:
Give all checkboxes the same name followed by a pair of square brackets, so the entire set is treated as an array. In this case, give each checkbox a value.
Give each checkbox a different name and a value.
Give each checkbox a different name, but no value.
In each case, you need to check for the existence of the checkbox name in the $_POST array.
For example:
<input type="checkbox" name="color[]" id="orange" value="orange">
<input type="checkbox" name="color[]" id="apple" value="apple">
To get the values for these checkboxes:
if (isset($_POST['color'])) {
$colors = $_POST['color'];
// $colors is an array of selected values
}
However, if each checkbox has a different name and an explicit value like this:
<input type="checkbox" name="orange" id="orange" value="orange">
<input type="checkbox" name="apple" id="apple" value="apple">
You still need to use isset():
if (isset($_POST['orange'])) {
// orange has been set and its value is "orange"
}
If you don't set a value, the default value is "on", but it won't be in the $_POST array unless it has been selected, so you still need to use isset().
You need to give the inputs the same name:
<input type="checkbox" name="selection[]" value="orange">
<input type="checkbox" name="selection[]" value="apple">
<input type="checkbox" name="selection[]" value="sky">
<input type="checkbox" name="selection[]" value="sea">
Then iterate over the $_POST['selection'] array in PHP.
You won't get the ids but the names will be associative indexes in the $_POST array (and $_REQUEST). NOTE: They will only be available in the array if they were checked by the client.
if ($_POST['oragne'] == 'on')
You can set them up to post to PHP as arrays, if you build them similar to below:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="box_group_1[oragne]" id="oragne">
<input type="checkbox" name="box_group_1[apple]" id="apple">
<input type="checkbox" name="box_group_1[sky]" id="sky">
<input type="checkbox" name="box_group_1[sea]" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
print_r($_POST['box_group_1']);
?>

jquery check checkboxes based on php array

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

Categories