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.
Related
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.
I am using position absolute's validation engine for my form. I would like to check whether at least one checkbox from group is selected. In examples it is done by setting the same name attribute for group of checkboxes.
I cannot name checkboxes with the same name, because I am saving their state in database with following code:
$values = array(
'checkbox1' => null,
'checkbox2' => null
);
foreach (array_intersect_key($_POST, $values) as $key => $value) {
$values[$key] = mysql_real_escape_string($value);
}
$query_add_candidate=sprintf("INSERT INTO dbase (checkbox1, checkbox2) VALUES ('$values[checkbox1]', '$dates[checkbox2]')"
Now checkbox1 and checkbox2 are validated individually, beacuse they have different names. How can I check if selected is at least one of them?
Here is my HTML code:
<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="checkbox1" id="maxcheck1" value="1"/> Text1
<input class="validate[minCheckbox[1]] checkbox" type="checkbox" name="checkbox2" id="maxcheck2" value="2"/> Text2
on php ,
if(!$_POST['checkbox1'] && !$_POST['checkbox2']){
echo 'Error check at least one';
}
but what you really want is an array,
HTML,
<input type="checkbox" value="ch1" name="check[]" />
<input type="checkbox" value="ch2" name="check[]" />
php
<?php
if(empty($_POST['check'])){
echo 'Error: hey, check at least one will you!?';
}
?>
so this way you don't have to check all of them one by one, especially if you have loads of them on the same page.
NOTICE: You should also know, if checkbox is not ticked it will also not be set on the php $_POST superglobal, otherwise if it is ticked, it will show whatever the value="..." holds,
if its posted then its checked,
so if you have it in $_POST["checkbox_name"] then its checked, otherwise it wont be posted.
You can either add loads of code to reimplement control arrays in a poor way, or you can alter the code that builds your query so it can accept control arrays.
I would prefer the latter.
I'm presented with a problem using PHP and MYSQL. I have a dynamic list of options which the user can select (maximum of 3) that are added from the administration panel as shown below:
<input type="checkbox" name="<?php echo "category".$i; ?>"
value="<?php echo $cat_id; ?>" />
<?php echo $cat_name; ?><br />
<?php
$i++;
}
echo "<input type='hidden' name='num_cat' value='$num_cat' />";
?>
I now want to count how many check boxes are 'checked' and if there are more than 0 and less than 4 checked it will update the mysql table with these stored. They are stored by means of 1's and 0's. So they tick 'yes' and a 1 is stored, they tick 'no' and a 0 is stored.
I've been trying to use jQuery and Javascript but they all seem to be for Check Box Forms which have the values pre-written within a form, mine are dynamic from a database.
Many thanks for your help.
Use [] in the end of your checkbox names and use the sizeof function on the corresponding $_POST[] array in your php script.
<input type="checkbox" name="values[]" value="1" />Value 1
<input type="checkbox" name="values[]" value="2" />Value 2
sizeof($_POST['values']);
Note that the checkboxes have the same name and end with brackets (values[]). This indicates that the checkboxes belong together and are bundled as an array in php. Only the selected values will be present in the array as well.
I am doing this :
function GETVALUE()
{
if(document.getElementById("requirenewpage").checked == true)
{
document.getElementById("requirenewpage").value;
var cval= parseInt(document.getElementById("requirenewpage").value);
}
}
HTML-----------------------------
<input type="checkbox" id="requirenewpage" unchecked value= "GETVALUE();" >
I need to insert into a mysql table , 0 or 1, which is taken from the VALUE attribute of the checkbox.....but am not able to do it...please help???
Its always inserting 0 into the database, albeit am setting the value as 1 in the function GETVALUE().....
Actually you don't need any of this i think. You can use this html:
<input type="checkbox" id="requirenewpage" value= "1" >
and the checkbox will send a value of 1 to the server if checked, otherwise it won't send anything (and the corresponding $_POST['requirenewpage'] or $_GET['requirenewpage'] won't be set).
If the checkbox is checked it's value is sent to the server and a key in the $_POST array (if you use POST) is created with the name of the checkbox and the value of hte checkbox.
you can do, serverside:
$chkboxval = 0;
if (isset($_POST['requirenewpage'])){
$chkboxval = $_POST['requirenewpage'];
}
I'm shocked that nobody has answered this correctly yet...
Change the checkbox to the following:
<input type="checkbox" id="requirenewpage" name="requirenewpage" value= "1" />
The ID of an input element is used for script access and styling only, if you want to submit the element in a form it must have a name attached to it.
You are wrong with your html. Change your checkbox code from
<input type="checkbox" id="requirenewpage" "unchecked" value= "GETVALUE();" >
to
<input type="checkbox" id="requirenewpage" onclick= "GETVALUE();" >