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
}
Related
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.
I'm not sure how to ask this question. It procedural question I believe.
<input type="hidden" name="1[]" value="dummy">
<input type="radio" name="1[]" value="5">
<label> Very Good </label>
<input type="radio" name="1[]" value="4">
<label> Good </label>
<input type="text" name="1[]" size="20">
<br>
<input type="hidden" name="2[]" value="dummy">
<input type="radio" name="2[]" value="5">
<label> Very Good </label>
<input type="radio" name="2[]" value="4">
<label> Good </label>
<input type="text" name="2[]" size="20">
$_POST output:
[1] => Array
(
[0] => Text misc
)
[2] => Array
(
[0] => 5
[1] =>
)
From this I construct and INSERT statement.
INSERT INTO coached_tracked (coached_id, value, note)
VALUES ($key, $value[0], $value[1]);
This is are dynamically generated form inputs. A radio button, text field pair.
How can I handle an occurrence where the radio is not selected and the text field has value, like in the first instance. I want the option of having nothing selected so a default value seems not called for. I tried with both with and without a dummy value (I saw an example suggesting a hidden field as a possible solution.)
Suggestions.
You should not tell the database what ID to use. Let the database itself determine that by using an auto-incremented column.
First, start with a logical input name. Using just numbers is extremely confusing and looking at your code, I have absolutely no idea what you're doing. We also want everything to go into the same PHP $_POST variable to not have to iterate over all possible number cominations. That means we can just iterate over the one single array.
Let's say you're adding a coach to a database, so logically we would start with:
<input name="coach">
Now when we want to add multiple coaches instead of just one, we can use HTML array names, however I would recommend you hard-code them instead of auto-incrementing in your HTML, which should simplify things later on. We also pluralize it to coaches:
<?php
for ($i=0;$i<10;$i++) {
?>
<input name="coaches[<?=$i?>]">
<?php
}
Now if each coach contains a certain properties, let's say name, salary, note, etc, we can add the properties to the input names like so:
<?php
for ($i=0;$i<10;$i++) {
?>
<input name="coaches[<?=$i?>][name]">
<input name="coaches[<?=$i?>][salary]">
<input name="coaches[<?=$i?>][note]">
<?php
}
Then in PHP you just iterate over $_POST['coaches'] and then use the properties for each coach how you wish:
if (isset($_POST['coaches'])) {
foreach ($_POST['coaches'] as $coach) {
$name = $coach['name'];
$salary = $coach['salary'];
$note = $coach['note'];
// Now execute the query:
// INSERT INTO coached_tracked (name, salary, note)
// VALUES ($name, $salary, $note);
}
}
Note: remember to sanitize any user-supplied data by using prepared statements with bound parameters to make sure you're not open to SQL injection attacks.
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 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.