I have a form using the GET method consisting of checkboxes.
This form is sending data to another page that is receiving the GET info and using it to pull info from a json api. I need to have it send the name once with all values combined into one string like this: example.com/color=RedGreenBlue
I am able to get all values combined and echoed onto the page but because they are in a foreach loop I am not able to pass them in the form. I tried below using the hidden field to pass them with no luck.
This is the method I've seen suggested but does not work for me:
<form action="" method="get">
Red<input type="checkbox" name="color[]" value="Red">
Green<input type="checkbox" name="color[]" value="Green">
Blue<input type="checkbox" name="color[]" value="blue">
<input type="submit" value="submit">
<?php
$name = $_GET['color'];
if (isset($_GET['color'])) {
foreach ($name as $color){
echo $color;
}
}
?>
<input type="hidden" name="MajorArea" value="<?php echo $color; ?>" />
</form>
Is there a way to assign one name to a group of checkboxes? Is there a way to pull the foreach loop data and use it outside a loop? Am I overlooking a way that is much easier than this?
Thanks for any advice!
I'll go out on a limb here and delete if it isn't what you're after. $_GET['color'] will be an array or it will be empty. You could also use isset:
<form action="" method="get">
Red<input type="checkbox" name="color[]" value="Red">
Green<input type="checkbox" name="color[]" value="Green">
Blue<input type="checkbox" name="color[]" value="blue">
<input type="submit" value="submit">
</form>
<?php
if (!empty($_GET['color'])) {
echo implode($_GET['color']);
}
?>
Related
I have multiple checkboxes with names of adminMeta[], such as:
<input type="checkbox" name="adminMeta[name1]" value="1" />
<input type="checkbox" name="adminMeta[name2]" value="1" />
and so on and I also have text inputs like this too with the same names.
When the data is posted, I am looping through using a foreach loop:
foreach($_POST["adminMeta"] as $a => $b) {
}
inside the loop, I add/update the record in my database depending on whether it exists already or not.
But I am having some issues with checkboxes and knowing whether they are checked or not.
I have tried using if(isset($b)) but that hasn't worked.
How can I tell inside my loop, whether a checkbox is checked or not?
If a checkbox is not checked, then it is not a successful control.
If it is not a successful control, then it won't be included in the form data at all.
If it isn't in the form data, then it won't appear when you loop over the form data.
So
If it is in the form data, then it is checked
Otherwise it is not checked
Normally I'd approach this problem with something along the lines of:
$list_of_checkboxes = [ "name1", "name2" ];
Then generate the form with:
foreach ($list_of_checkboxes as $name) {
?>
<label>
<input type="checkbox"
name="adminMeta[]"
value="<?php echo htmlspecialchars($name); ?>">
<?php echo htmlspecialchars($name); ?>
</label>
<?php
}
Then test the data with:
foreach ($list_of_checkboxes as $name) {
if (in_array($name, $_POST['adminMeta'])) {
# Checked
} else {
# Not checked
}
}
Another approach would be to set hidden inputs before each check with default value of 0:
<input type="hidden" name="adminMeta[name1]" value="0" />
<input type="checkbox" name="adminMeta[name1]" value="1" />
<input type="hidden" name="adminMeta[name2]" value="0" />
<input type="checkbox" name="adminMeta[name2]" value="1" />
Now you will receive the data even if you don't check the checkboxes.
I have html checkbox like this:
<form action="" method="post">
<input type="checkbox" name="language[]" value="php" />PHP<br />
<input type="checkbox" name="language[]" value="html" />HTML<br />
<input type="checkbox" name="language[]" value="java" />Java<br />
<input type="checkbox" name="language[]" value="c++" />C++<br />
<input type="submit" value="send" />
</form>
Now I want to detect the checkbox is not checked using this PHP
if($_POST)
{
if(empty($_POST['language']))
{
echo "bla";
}
else
{
foreach($_POST['language'] as $value)
{
echo 'Checked: '.$value.'
';
}
}
}
The output is always show the checbox checked.
My question is, how can I detect the checkbox is not checked?
Example I do not check PHP and Java.
You don't need to validate checkbox by checkbox in order to determine if they are checked or not, you won't get the unchecked checkboxes values at the time you send the form, so, sending the form like this:
<form action="" method="post">
<input type="checkbox" name="language[]" value="php" />PHP<br /> <!-- checked -->
<input type="checkbox" name="language[]" value="html" />HTML<br /><!-- checked -->
<input type="checkbox" name="language[]" value="java" />Java<br /><!-- unchecked -->
<input type="checkbox" name="language[]" value="c++" />C++<br /><!-- unchecked -->
<input type="submit" value="send" />
</form>
In your PHP, you will get an array as follows:
$_POST['languages'] = array("php", "html");
Now, lets say you have an array of all the values in order to check which ones you need to delete, and which ones you need to add, a rough code example would be as follows:
$allValues = array('php', 'html', 'java', 'c++');
$valuesForAdd = $_POST['language'];
$valuesForDeletion = array_diff($allValues, $valuesForAdd);
First you need the selectable items array in the backend:
$items = array('php','html','java','c++');
You have the posted (selected) languages array here:
$_POST['language']
Not selected languages array:
$not_selected_languages = array_diff($items,$_POST['language']);
I hope it helps.
Only 'checked' checkboxes get sent as parameters in a POST request.
If you want to know which aren't checked, you could have the value list stored on PHP side; then once you receive POST data - compare the array on PHP side with POST array.
$all_vals = array('php', 'c++', 'html', 'java');
$post_vals = $_POST['languages'];
foreach ($post_vals as $post_val)
if in_array($post_val, $all_vals)
$checkbox checked
else
$checkbox not checked
I assume this gives you enough liberty to do what you need.
I having around 20 check boxes in my form as
<input type="checkbox" name="c1" value="1" />
<input type="checkbox" name="c2" value="2" />
<input type="checkbox" name="c3" value="3" />
<input type="checkbox" name="c4" value="4" />
i would like to these values to a database. I just thought rather than creating 20 fields in my database grab all the values at store in the db as 1,2,3,4 and then when querying to explode the values and display it accordingly using php.
can someone please tell me how am i supposed to concatenate the values 1,2,3,4 from the check fields when submitted so i can pass to the database.
i would appreciate if anyone can suggest a different effective way to achieve this using php.
You can change the name of the checkboxes to be the same, something like
<input type="checkbox" name="cb[]" value="1" />
<input type="checkbox" name="cb[]" value="2" />
Then access them via $_GET or $_POST via
if (isset($_POST['cb'])) {
$my_values = implode(",", $_POST['cb']);
}
If you want them sorted, then you will want to do something like this:
if (isset($_POST['cb'])) {
$my_values = $_POST['cb'];
sort($my_values);
$my_db_value = implode(',', $my_values);
}
For the record, I agree with #Shef in the case that the database can handle the extra load. Depending on when this information will be needed in a highly scalable solution, however, this is a perfectly acceptable way to handle this.
To answer your initial question, first off you need to name your checkboxes all the same name, so:
<input type="checkbox" name="box[]" value="1" />
....
<input type="checkbox" name="box[]" value="20" />
PHP script:
$boxes = $_POST['box'];
$values = implode(",", $boxes); // $values now has "1,2,3,4...", insert into table
A better way would be to have a separate table (see #Shef 's comment).
I have an array of checkboxes name="box[]". Through PHP I make sure that they're checked after they're submitted by echoing "checked='checked'" if they were checked at submit event.
Now, if I check the third box, the value jumps down to the first checkbox after submit, since the array was empty up until the third checkbox. Same, if I check the 2nd and 3rd checkbox, they jump down to 1st and 2nd after submit. This is the code I'm using:
<form method="post">
<input type="checkbox" name="box[]" value="true" <?php if ($box[0] == true) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="true" <?php if ($box[1] == true) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="true" <?php if ($box[2] == true) echo "checked='checked'"; ?>><br>
<p>
<input type="submit" value="Submit">
</form>
Try it at:
http://experiencerapanui.com/selecttest.php
Can I make the checkboxes fill up the array with a value "false" or whatever, if the box is unchecked? Which way should I go?
****** EDIT ******
Thanks to phant0m, I managed to come up with a solution:
<form method="post">
<input type="checkbox" name="box[]" value="1" <?php if (in_array("1", $box)) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="2" <?php if (in_array("2", $box)) echo "checked='checked'"; ?>><br>
<input type="checkbox" name="box[]" value="3" <?php if (in_array("3", $box)) echo "checked='checked'"; ?>><br>
<p>
<input type="submit" value="Submit">
</form>
Putting unique values for the checkboxes, then if I find the value in the array $box[], the box is marked as checked.
This does not work, because only those checkboxes, that are checked, are being put into the $box array.
Either use different names, or different values to distinguish between them.
Consider this: You check the second and the third checkbox. In PHP, you will receive:
$_POST['box'] = array(0 => "true", 1 => "true");
You cannot know, which checkboxes have been checked, unless all of them are.
The POST value should start with isset, then !empty($array) determines if the POST value is an array and prevents a null array error when no options are selected.
&& is_array($_POST['box']) could be used in addition to !empty($_POST['box']) as well to check the validity of the array.
A variable is used in the following examples for the value field, as it makes it easier to define and populate inputs when using a foreach loop and may be sanitized if needed as a preventive measure.
It would probably be a good idea to sanitize the $_POST array also, and enclosing it in a function with the validation would allow it all to be called from the checkbox input and keep the input area tidy.
<input type="checkbox" name="box[]" value="<?php echo $unique_id; ?>" <?php if(isset($_POST['box']) && !empty($_POST['box']) && in_array($unique_id, $_POST['box'])) echo "checked='checked'"; ?>>
OR
<input type="checkbox" name="box[]" value="<?php echo $unique_id; ?>" <?php my_function(); ?>>
Excellent question and solutions! There seem to be relatively few examples that use an array method to preserve Post selections, and the one provided here is relevant and very helpful even years later.
I'm getting database from database and each row has a id and im showing it like this in html
<td><input type="checkbox" value"1">test1</td></tr>
<td><input type="checkbox" value"2">test2</td></tr>
and so on...
now lets say that user checked ten check boxes out of 15 and then clicked submit .
how to get values of those boxes in php???
Your checkboxes need to have a name & a value attribute:
<input type='checkbox' name='test1' value='1'> Test1
<input type='checkbox' name='test2' value='1'> Test2
then when that is posted you can access the values in PHP via $_POST:
$test1 = $_POST['test1']
$test2 = $_POST['test2']
Keep in mind that the values will only be returned if the box is checked, so most likely instead of the above PHP, you're more than likely just going to want to check if the value exists.
give the checkboxes names:
<td><input type="checkbox" value"2" name='test'>test2</td></tr>
Than in php just read the request
$test = $_REQUEST['test']
if the OP doesn't have these checkboxes inside of a , any amount of PHP code will make absolutely no difference (FROM Blender)
they will be in either the $_GET or the $_POST array
Try this way..
<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="1"><label>Test 1</label><br/>
<input type="checkbox" name="check_list[]" value="2"><label>Test 1</label><br/>
<input type="checkbox" name="check_list[]" value="3"><label>Test 3</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
}
}
?>