Checkbox with the same name POST - php

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 )

Related

i have multiple radio button in same name as array and same value [duplicate]

This question already has an answer here:
I have Multiple radio button with same name in it of array and have same values as 1
(1 answer)
Closed 4 years ago.
i have multiple radio button in same name as array and same value for all.
<input type="radio" name="radio_name[]" id="radi_name" value="1" checked>
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[]" id="radi_name" value="1" >
<label for="radio1">Set as Default</label>
in php i used like this
$a[]=$_post['radio_name'];
prinr_r($a);
im getting result like this :
Array ( [0] => 1 )
if i uncheck the button set as zero i want result to be like this
Array ( [0] => 1,[1] => 0,[2] => 0,[3] => 0 )
Please check this images i have form like this
You need to use only [] instead of array keys also use checkbox instead of radio . So use following code it will work for you.
<input type="checkbox" name="radio_name[]" id="radi_name1" value="1" checked>
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name2" value="1" >
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name3" value="1" >
<label for="radio1">Set as Default</label>
<input type="checkbox" name="radio_name[]" id="radi_name4" value="1" >
<label for="radio1">Set as Default</label>
I am pretty sure that radio buttons don't get sent to the server if none of the values is selected. In your html you really have 4 different radio button groups so only the ones that have a selected value, get sent.
If you want 4 groups where a value always gets sent for each group, you should do something like this:
<input type="radio" name="radio_name[1]" value="1" id="radio1" checked>
<input type="radio" name="radio_name[1]" value="0">
<label for="radio1">Set as Default</label>
<input type="radio" name="radio_name[2]" value="1" id="radio2">
<input type="radio" name="radio_name[2]" value="0" checked>
<label for="radio2">Set as Default</label>
// etc.
This way you will have independent radio button groups and you will get the result you want.
You have to use checkbox instead of radio. Radio only allowed 1 value being posted, and remove the array key like dipmala suggested.

Adding an integer at the end of an input's name to get a specific url

I am trying to get a url where I can retrieve the selected values from.
For example, if I put a check mark on a and b then sumbit, I will get:
index.php?category=1&&category=2
I want to get this instead: index.php?category0=1&&category1=2
So that I can later get this specific value with $_GET['category0']
Is there a way to add a counter for the selected checkboxes and add 0,1,2,3.. at the end of the name of its input?
<form action="" method="get">
<!-- this will be a php loop instead of hardcored
which will retrieve data from the db -->
<label><input type="checkbox" name="category" value="1">a</label>
<label><input type="checkbox" name="category" value="2">b</label>
<label><input type="checkbox" name="category" value="3">c</label>
<label><input type="checkbox" name="category" value="4">d</label>
<label><input type="checkbox" name="category" value="5">e</label>
<input type="submit">
</form>
Category variables are overwriting each other, try this instead:
<form action="" method="get">
<!-- this will be a php loop instead of hardcored
which will retrieve data from the db -->
<label><input type="checkbox" name="category[]" value="1">a</label>
<label><input type="checkbox" name="category[]" value="2">b</label>
<label><input type="checkbox" name="category[]" value="3">c</label>
<label><input type="checkbox" name="category[]" value="4">d</label>
<label><input type="checkbox" name="category[]" value="5">e</label>
<input type="submit">
</form>
Or even:
<form action="" method="get">
<!-- this will be a php loop instead of hardcored
which will retrieve data from the db -->
<label><input type="checkbox" name="category[a]" value="1">a</label>
<label><input type="checkbox" name="category[b]" value="2">b</label>
<label><input type="checkbox" name="category[c]" value="3">c</label>
<label><input type="checkbox" name="category[d]" value="4">d</label>
<label><input type="checkbox" name="category[e]" value="5">e</label>
<input type="submit">
</form>
Then you can get the info this way:
$_GET['category']['a']
This is more of an HTML question, than a PHP question, since regardless of what technology your server uses, the browser will send the information consistently. When submitting a form, the browser uses the name and value attributes of the <form> element's inputs. To receieve the parameters as you said, change your name and value attributes to the following:
<label><input type="checkbox" name="category0" value="1">a</label>
<label><input type="checkbox" name="category1" value="2">b</label>
<label><input type="checkbox" name="category2" value="3">c</label>
<label><input type="checkbox" name="category3" value="4">d</label>
<label><input type="checkbox" name="category4" value="5">e</label>
EDIT: On the server side, you could do the following to generate the above markup:
$category_names = array("a", "b", "c");
for ($i=0;$i<count($category_names);$i++) {
echo '<label><input type="checkbox" name="category'.$i.'" value="'.$i+1.'">'.$category_names[$i].'</label>';
}
You mean something like this ?
for ($i=0;$i<3;$i++)
echo '<label><input type="checkbox" name="category'.$i.'" value="'.$i.'">a</label>';
It depends a bit on what you want to do with the data (you might need to adjust the 'a' label text in this example to your needs), there are usually several ways of passing data that all work, but require different levels of effort.

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>

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

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