The part of the form this post goes about is this:
<input type="checkbox" id="SOMEVALUE1" value="SOMEVALUE1" class="css-checkbox" name="checkbox1" />
<input type="checkbox" id="SOMEVALUE2" value="SOMEVALUE2" class="css-checkbox" name="checkbox2" />
Now I want to make a PHP/MySQL script that posts every selected value, so if SOMEVALUE1 is selected and the form is posted it puts SOMEVALUE1 in the table, if both values are selected it puts SOMEVALUE1 in the table and it puts SOMEVALUE2 in the table.
Summed up: I want to get all values of checked checkboxes
Change the name from checkbox to checkbox[]. This will change it from a single value in $_REQUEST to an array of values in $_REQUEST.
Related
I need to get value 1 - when checkbox is checked and 0, when it is unchecked.
<input type="checkbox" name="data[color][]" class="color" value="1" checked>
Data array - stores other data that is retrieved from input type text and select. It's working fine. The problem occurs when I try to retrieve data from an input of type checkbox. Brief description of the "project": the script is to add several records to the database simultaneously. To start with, one section is available for data entry, for a single record - using JS after clicking the appropriate button - the next section appears and so on up to a certain maximum number of sections/possible records. In general, the script works correctly - records are added. The problem is with the color column that gets the data from the aforementioned checkbox. After the form is submitted - there is a reorganization of the board to somehow present this more reasonably.
PHP code:
foreach($data as $columnName => $columnValues){
foreach($columnValues as $rowIndex => $columnValue){
$result[$rowIndex][$columnName] = $columnValue;
}
}
To get at the value:
foreach($result as $r => $value){
if(isset($value['color'])){
$color = 1;
} else {
$color = 0;
}
}
While with the number of records <= 2 it works correctly, when there are > 2 values are not assigned correctly. What am I doing wrong? If you need more information - please write. I seem to have pasted everything related to this.
When using var_dump for $value['color'] with 4 records there is this result:
string(1) "1"
string(1) "1"
NULL
NULL
Where only the first and fourth checkbox was checked.
Unlike input type text and select, only checked checkbox inputs will be submitted. So using dynamic arrays [], if you have 3 and only the first is checked, then it will be submitted as $_POST['data']['color'][0]. If you only check the third one it will also be submitted as $_POST['data']['color'][0], etc... With the current code there is no way to track them.
Probably the best way to do this is to use a hidden input with the same name before the checkbox with value 0 as default, so that all checkboxes are submitted, either with 0 or 1. You need to specify the numerical indexes for each:
<input type="hidden" name="data[color][0]" value="0">
<input type="checkbox" name="data[color][0]" class="color" value="1" checked>
<input type="hidden" name="data[color][1]" value="0">
<input type="checkbox" name="data[color][1]" class="color" value="1" checked>
<input type="hidden" name="data[color][2]" value="0">
<input type="checkbox" name="data[color][2]" class="color" value="1" checked>
Now you'll always get 3 $_POST['data']['color'] elements, either 0 or 1.
I have a bike I have a car
I know when the form is submitted the value will be vehicle=Bike&vehicle=Car if both are ticked
Is there a way to make the value to be vehicle=Bike,Car
Put them into one variable then separated in a comma
Since you are using POST and multiple check boxes, set the name of each check box like this:
<input type="checkbox" name="vehicle[]" value="Bike">
<input type="checkbox" name="vehicle[]" value="Car">
Then when your form is submitted you will receive an array of all the checked boxes and their values in the array:
$_POST['vehicle'][];
Now if both boxes are checked you can retrieve the values in a foreach loop:
foreach($_POST['vehicle'] as $type){
echo "Type = ".$type;
}
With this you will get an output of
Type = Bike
Type = Car
Try this one,
<input type="hidden" name="vechiclesStr" id="vechiclesStr">
//on submitting,
document.getElementById("vechiclesStr").value = document.formname.vechicle.join();
I have code similar to the following:
<input type="checkbox" name="visitProperty" value="1" id="visit-0">
<input type="checkbox" name="visitProperty" value="1" id="visit-1">
<input type="checkbox" name="visitProperty" value="1" id="visit-2">
...
Once the form is submitted I want to get checked checkboxes, so far I've been using
if (isset($_POST['visitProperty']) {..}
But to my understanding it only gets one checkbox? Where as I need to check all of them and see if they were checked, so inside the if statement I can create a loop that gets id's of all submitted checkboxes and then gets the number from id, to update a certain array.
<input type="checkbox" name="visitProperty[]" value="1" id="visit-0">
<input type="checkbox" name="visitProperty[]" value="2" id="visit-1">
<input type="checkbox" name="visitProperty[]" value="3" id="visit-2">
<?php
foreach($_POST['visitProperty'] as $check) {
echo $check . "<br>"; // for example
}
?>
NOTE: $_POST['visitProperty'] will hold checked checkbox values. You will access all the checkboxs as an array as following $_POST['visitProperty'][]
When you put in the name, you are declaring a variable. You need to declare it as an array, or each checkbox will bump out the last one. Add some empty square brackets to the name.
You would need or defined ID or a unique value, otherwise you will not be able to identify them on the server-side (the ID does not get sent in $_POST).
So in case of a unique, identifyable ID, you could do something like:
<input type="checkbox" name="visitProperty[<?php // echo some unique id from a database for example ?>]" value="1" id="visit-0">
The reason you would need the ID to be identifyable, is that unchecked checkboxes do not get sent to the server, so you might end up with an array of 2 if visitProperty is an array, but you would not know which 2.
A simple question but I can't seem to find the answer.
I have a simple form that inserts the value 'on' to a database when a checkbox is checked
How do I get the checkbox checked when the data is pulled from the database when the form is revisited and displaying with the database information?
I have tried this but it doesn't work
<input type="checkbox" name="positioning" class="input_margin" value="<? $row['positioning']; ?>">Positioning<br />
(I only need help with this, I have the sql query etc set up fine)
Thanks
Use a conditional statement to check the database value and echo 'checked' if it should be checked
<input type="checkbox" name="positioning" class="input_margin" <? if($row['positioning']=='on'){echo 'checked';} ?>>
You need to use the checked property and not value.
The value field is what is provided if the box is checked (e.g positioning=on) for value="on"
<input type="checkbox" name="positioning" class="input_margin" <? if($row['positioning']) { ?> checked<? } ?>/>Positioning<br />
Include the value attribute so the checkbox input will post "on" if checked.
Otherwise the form will initially retrieve a value but fail to post on any subsequent form submission. to the database
<input type="checkbox" name="positioning" class="input_margin" value="on" <? if($row['positioning'] =='on'){echo 'checked';} ?> />Positioning<br />
I have a checkbox that if checked stores the value "testing" to the field "checkbox" om my database.
<input type="checkbox" name="checkbox" value="testing">
I would like to store two values, on two fields on my database, from one checkbox.
If checked:
store value testing to the checkbox field
store value testing2 to the checkbox2 field
Is this possible?
Thanks in advance
<input type="checkbox" name="checkbox" <?php if($val=="testing"): echo "value='testing' echo checked='on'" ?>">
<input type="checkbox2" name="checkbox2" <?php if($val=="testing2"): echo "value='testing2' echo checked='on'" ?>">
Hope this is what you are looking for..