$_POST for checkbox - php

I have this form and its details need to be send in an email. As of now I have only managed to do the other fields to be included in the email but I am having a trouble with this multiple checkboxes. I have lots of checkboxes, 23 to be exact. Now, the problem I am encountering is, how will I include the value of the checkbox if one checkbox is checked or just two or three checkboxes are checked.
HTML
<input type="text" name="state" id="txt1">State:</input>
<input type="text" name="name" id="txt2">Name:</input>
Package A<input type="checkbox" name="radiog_dark" value="150.00" id="tcbx3" class="css-checkbox"/>
Package B<input type="checkbox" name="radiog_dark" value="175.00" id="tcbx3" class="css-checkbox"/>
Package C<input type="checkbox" name="radiog_dark" value="200.00" id="tcbx3" class="css-checkbox"/>
PHP
$state = $_POST['state'];
$entity = $_POST['entity'];
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Entity: ".clean_string($entity)."\n";

Use isset() method to check for the checkbox is checked or not
if(isset($_POST['radiog_dark'])){
// do something
}
Further more you cant have same names for two checkboxes as you will get only one, other 2 will over that one

Use Array Syntax to set your checkbox field like this way and check your checkbox value using isset, you can set the array index like this way to more readablity e.g
name="radiog_dark['package_a']"
name="radiog_dark['package_b']"
<input type="checkbox" name="radiog_dark[]" value="150.00" id="tcbx3" class="css-checkbox"/>
if(isset($_POST['radiog_dark'])){
// do other stuffs
}

change chekbox name radiog_dark to radiog_dark[]
Package A<input type="checkbox" name="radiog_dark[]" value="150.00" id="tcbx3" class="css-checkbox"/>
if(!empty($_POST['radiog_dark'])){
// Loop to store display values of individual checked checkbox.
foreach($_POST['radiog_dark'] as $selected){
echo $selected."</br>";
}

Related

form submit checkbox value

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();

Get several checkboxes id's after submission

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.

contact form group check box array

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.

Jquery+PHP form - Multiple recipients based on checkbox selection

I'm asking this question in regards to my friend, so I do not have code samples to post here at the moment. Hopefully I'm clear enough that someone can help.
So he has a simple contact form except it has multiple checkboxes that the user can choose to send their requests to multiple recipients... like so...
x I would like to know about flight school
x I'm interested in becoming a teacher
x I would like someone to contact me about your degrees
Name
Email
Comments
And so based on which checkboxes are selected it should add that recipient to the email function so that they receive the users comments and interest.
The form is validated by jquery and uses the $.ajax function to POST the Name, Email and Comments fields over to a process.php... we are validating that at least one of the checkboxes is selected, however, we haven't been able to figure out how to pass its boolean value to the process.php and in-turn add the relevant email address to the mail() function.
I do realize this is semi-vague without posting our code, but I don't have access to it right now... and I have been searching google for about 30 minutes trying to find something to work with. Any help would be appreciated. Thanks.
you can simply check if the value you got is true or not:
basic idea :
if(checkbox-1-ischecked)
//send email to first recipent
end if
if(checkbox-2-ischecked)
//send email to 2nd recipent
end if
if(checkbox-3-ischecked)
//send email to 3rd recipent
end if
if(checkbox-4-ischecked)
//send email to 4th recipent
end if
Etc
This would seem to answer you check box query. (http://stackoverflow.com/questions/908708/how-to-pass-multiple-checkboxes-using-jquery-ajax-post)
In basic terms it would post an array back to the php script which you could then parse and depending what was ticked / the vars passed back you could then append more email addresses to the 'to' part of the mail function.
For a simplier implimentation you could just keep your three check boxs seperate not in an array and ajax post them back individually.
HTML
<input type='checkbox' name='flight' value='1' id='flight' />
<input type='checkbox' name='teacher' value='1' id='teacher' />
Then simply on the server via PHP
$to="";
if($_POST['teacher'] == 1) {$to = $to."joe#email.com,"};//append email
if($_POST['flight'] == 1) {$to = $to."bob#email.com,"};//append email
$to = rtrim($to, ","); //remove trailing comma
NOTE as with all web to mail scripts make sure you sanitize all vars to prevent spam abuse!
Name your elements as an array like this:
<input type="checkbox" name="mybox[]" value="foo#example.com">Foo</input>
<input type="checkbox" name="mybox[]" value="bar#example.com">Bar</input>
<input type="checkbox" name="mybox[]" value="hello#example.com">Hello</input>
<input type="checkbox" name="mybox[]" value="world#example.com">World</input>
After POSTing the form to your PHP, $_POST['mybox'] will be an array holding the values of the boxes checked.
In your PHP
if(isset($_POST['my_box']))
{
$subject = "sub";
$body = "body";
if (is_array($_POST['mybox']))
{
//multiple items were selected.
$to = implode(',',$_POST['my_box']);
mail($to,$subject,$body);
}
else //only one item was selected
{
echo $_POST['my_box'];
$to = $_POST['my_box'];
mail($to,$subject,$body);
}
}
else
//none were selected
You can simply assign the same name to all checkboxes, which actually results in a checkbox array.
<form name="someform" onsubmit="return validate(this)" action="process.php" method="post">
<input type="checkbox" name="names[]" value="Saji">Saji
<input type="checkbox" name="names[]" value="Muhaimin">Muhaimin
<input type="checkbox" name="names[]" value='Muhsin'>Muhsin
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
In process.php, you could have-
$name_val=$_POST['names'];
foreach($name_val as $values){
//Here $values will contain only the values of the checkboxes you had selected.
echo $values."<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