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.
Related
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 have a contact form built with PHP and I had a radio option box (one click) and have changed it to a group check box, which means multiple boxes can be clicked.
However, only the last most click is sent through to my email and playing with the code has messed me up, I am not very clear with the php array code and multiple (({{
Here is the html code
<label><input type="checkbox" name="addon" value="NONE" <?php if (isset($_POST['addon']) && $_POST['addon'] == 'NONE') echo 'checked="checked"'; ?> tabindex="4" /> None <br /></label>
<label><input type="checkbox" name="addon" value="HKG" <?php if (isset($_POST['addon']) && $_POST['addon'] == 'HKG') echo 'checked="checked"'; ?> tabindex="5" /> Hong Kong <br /></label>
....
<label><input type="checkbox" name="addon" value="Other Start City" <?php if (isset($_POST['trip']) && $_POST['addon'] == 'Other Start City') echo 'checked="checked"'; ?> tabindex="4" /> Other</label>
and here is the php code I have at the moment, but this only gives one answer.
$Indhold .= "Tour Extension: ".$_POST['addon']."\n";
I tried changing it to an array (as I followed the tutorial http://www.html-form-guide.com/php-form/php-form-checkbox.html) , but then only array was printed on the email.
I also want to include validation on that combi box, if possible. So they can't choose NONE and HKG, and must click at least one.
PHP only populates $_POST/GET with arrays if the name ends in [] (or [index]).
Use name="addon[]"
Arrays aren't strings, so you can't just concatenate them. You can use implode to convert the members of an array into a single string. You could also use a for loop to deal with them one by one.
You just need to handle the $_POST['addon'] as an array (after naming your checkboxes "addon[]")
// make sure at least one checkbox is checked
if (isset($_POST['addon']))
{
foreach ($_POST['addon'] as $k => $v)
{
$Indhold .= "Tour Extension: {$v}\n";
}
}
Or as an alternative:
// make sure at least one checkbox is checked
if (isset($_POST['addon']))
{
$indhold .= 'Tour Extension: ' . implode(', ', $_POST['addon']) . "\n";
}
If in the form there are multiple input elements with a single name(i.e. not followed by '[]') only the latest of all those elements can be retrieved from the submitted form. So if you check more that one checkbox in a single group(i.e. the checkboxes having a single name which is not in the format 'name[]' but simple 'name') then you can get the last checked checkbox value from the form submit because all the previously checked values in the sigle group get overwritten. Hence you need to use an array for the group name to get all the checked checkbox values in the single group.
So always use the following syntax in case you have more than one checkbox in a single group:
<input type=checkbox name="check1[]" value="v1">v1
<input type=checkbox name="check1[]" value="v2">v2
<input type=checkbox name="check1[]" value="v3">v3
<input type=checkbox name="check1[]" value="v4">v4
But in case of a single checkbox use the following:
<input type=checkbox name="check2" value="v1">v1 //No need to append square bracket at the end of the assigned name.
I have a html form
<form action="process.php" method="post">
<input type="checkbox" name="name[v1]" />
<input type="checkbox" name="name[v2]" />
<input type="checkbox" name="name[v3]" />
<input type="submit" name="update" value="update">
</form>
If they is only one check box is ticked then I only see that check box
Array
(
[\'v3\'] => on
)
If I have checked all three box then I see them all.
Array
(
[\'v1\'] => on
[\'v2\'] => on
[\'v3\'] => on
)
Is they any way I can see all of my checkbox even if they are not checked.
process.php
foreach( $_POST['name'] as $k => $v )
{
echo "key: ".$k;
}
Checkboxes and radio buttons are not passed on to the processing script if they don't have a "checked" attribute set. This is HTML4 by design.
The only way you can set a state is using something like:
if(!isset($_POST['mycheckbox'])){ $_POST['mycheckbox'] = 0; }
or better yet:
$_POST['mycheckbox'] = isset($_POST['checkbox']);
Regarding radio buttons, you should only use the first version since radio buttons can have more than one value so instead of setting a TRUE/FALSE in them, you want to set a default value instead.
Another note, DISABLED elements are not posted, even if they have a value, you will never see them, this is another design feature of HTML4+
I'm generating a form with php/mysql. I'm using checkbox that looks like that:
<input type="checkbox" id="my2_3" name="my2_3" />
<input type="checkbox" id="my2_4" name="my2_4" />
<input type="checkbox" id="my2_5" name="my2_5" />
My issue is to retrieve those data (whether the checkbox is checked or not and the id).
How can I retrieve that with php without knowing in advance what will be the $_POST[""] to request?
<input type="checkbox" id="my2_3" name="my[]" value="my2_3" />
<input type="checkbox" id="my2_4" name="my[]" value="my2_4" />
<input type="checkbox" id="my2_5" name="my[]" value="my2_5" />
I changed the name attribute to an array,
foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";
}
You can use foreach($_POST as $key => $value) { ... } to iterate over all POST vars.
It'd be easier for you if you changed the name attribute to create a php array. Check the documentation on creating arrays in HTML form.
Checkboxes are only posted when they are ticked. So you need to inspect $_POST and use isset() to determine whether or not the key you are looking for (the name attribute of a checkbox) is present. If it is, the checkbox was ticked. If not, the checkbox was unticked.
i have an html form full of text fields, checkbox's , and radio fields.
i wanted to get all the names of the fields so that i can get started in validating the information in them.
the method i am using to get them is
if(isset($_POST['submit'])) {
foreach($_POST as $name => $value) {
print $name."<br/>";
}
}
but i noticed that it only displays textbox and textarea field names and it doesnt include checkbox and radio field names through this submission. do i need to include anything for it to grab the field names of those?
Checkboxes and radio buttons work a little differently than your standard inputs. If a checkbox is present on a form that doesn't necessarily mean that it will be available in the resulting POST information. Rather, those values will only be avialable if they are actually marked (checkboxes checked and radio buttons selected). The proper way to test for their value in PHP is not to check the field value but rather to check isset() first.
For a checkbox:
$data['my_checkbox'] = isset($_POST['my_checkbox']) ? 'on' : 'off';
and for a radio button:
$data['my_radio'] = isset($_POST['my_radio']) ? $_POST['my_radio'] : false;
To be a little more descriptive let's say you have the following form:
<form action="test.php" method="post">
<input type="text" name="email" value="" />
<input type="checkbox" name="active" value="Yes" />
<input type="submit" value="Submit" />
</form>
If I were to submit that form with an email value of 'test#email.com' but not check the checkbox I would have the following in $_POST:
Array (
'email' => 'test#email.com'
)
However, if I were to submit the same form with the same email address and check the checkbox I would have the following:
Array (
'email' => 'test#email.com',
'active' => 'Yes'
)
Hope that helps.
0./ Try using the following code to see the raw posted data:
echo '<pre>';
print_r($_POST);
echo '</pre>';
1./ Make sure you use a name attribute value for your checkbox and radio inputs.
Typically for checkboxes, it will be an array.
<input type="checkbox" id"=fruit-apple" name="fruits[]" value="apple" />
<input type="checkbox" id="fruit-pear" name="fruits[]" value="pears" />
2./ Make sure they sit inside the form tag.
3./ If you submit using a javascript call, try disabling javascript and see if the error stays. If it does not, you know your javascript is the culprit.