Get several checkboxes id's after submission - php

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.

Related

Checkbox and MySQL database with PHP

I've got a problem.
I don't know how to put the value of my checkbox in my MySQL database with PHP.
So :
<input type="checkbox" name="checkiard" value="IARD">
How to put the value "IARD" in my database ONLY if the checkbox is check ?
Thank you guys.
You can access the value of checkbox as any other input types. Its simply
if(isset($_POST['checkiard'])) {
$optionArray = implode(",", $_POST["checkiard"]);
}
Try this.
If you have more than one checkbox then you markup should be something like this
<input type="checkbox" name="checkiard[]" value="IARD-1">
<input type="checkbox" name="checkiard[]" value="IARD-2">
<input type="checkbox" name="checkiard[]" value="IARD-3">
you must keep the name same for all the checkbox.

codeigniter create array of checked checkboxes

I have php form which has numerous check boxes. I need to store all the selected check boxes in an array and then use them from my controller. So far, in my controller I have
$data['categories']= array($this->input->post('category'))
where my check boxes are called "category". however this method is only storing a single check box value, even when numerous check boxes are selected.
I then intend to pass this array to a model for processing.
Thank you for the help, I appreciate any suggestions.
In your view, use category[] as a name for checkboxes.
Example:
<input type="checkbox" name="category[]" checked> Option 1
<input type="checkbox" name="category[]" checked> Option 2
etc...
In your view
<td><input type="text" name="category[]"/></td>
<td><input type="text" name="category[]"/></td>
<td><input type="text" name="category[]"/></td>
Notice, we added two brackets, to indicate that this is an array.
Then in your Controller you can loop through it or whatever you like.
foreach ( $this->input->post('category') as $category)
{
// some stuff here
}

Get checkbox value in a html form

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.

Display mysql checkbox value in form

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 />

How to validate multi-checkbox state?

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.

Categories