Submit mutiple checkbox selections in contact form - php

Im new to PHP and Im having trouble getting a contact from to submit multiple checkbox selections.
Here is the part of my php that handles the selections:
if(trim($_POST['interest']) == '') {
$hasError = true;
}
else {
$interest = trim($_POST['interest']);
}
And this is the part of my HTML:
<label for="interest"><strong>I'm interested in:</strong></label>
<input type="checkbox" size="" name="interest" value="Photography" /> Photography
<input style="margin-left: 20px;" type="checkbox" size="" name="interest" value="Editorial" /> Editorial
<input style="margin-left: 20px;" type="checkbox" size="" name="interest" value="Other" /> Other
I would appreciate any help on this issue. Thanks in advance!

You need to send it as an array.
In the HTML add [] to each name field like so:
<input type="checkbox" name="interest[]" value="...">
In the PHP:
if( isset( $_POST['interest'] ) && is_array($_POST['interest']) ) {
foreach( $_POST['interest'] as $value ) {
//each $value is a selected value from the form
}
}
Hope that helps!

Related

How to Keep an array and dynamic Checkbox stays checked after submit?

I have one dynamic checkbox (can be ++) that needs to stay checked after user submit.
I already tried some tricks like using a hidden input before my checkbox code in HTML. Now I stuck in doing an isset(_POST) and the checkbox didn't stay checked.
Here's my HTML:
<input type="hidden" name="hidden_name[]" id="hidden_name0">
<input type="checkbox" name ="name[]" id="name0">
<label for="name">Name</label>
--------UPDATE--------
And in my PHP file, here's the code:
$valueName = array();
if(isset($_POST["hidden_name"]))
{
foreach($_GET['hidden_name'] as $value)
{
array_push($valueName,$value);
}
}
That code doesn't work :/
How to make the checkbox stays checked after user check it and submit the form? What should I write in PHP? Do I really need a hidden input before my checkbox?
isset might be returning false since the submitted value is NULL. I suggest adding a value='1' on the hidden input field -- or are you updating that field with the corresponding checkbox value?
Alternatively, you have some other options:
Change the name for each dynamic checkbox to have an identifier.
<input type="checkbox" name="name-x" <?php echo isset( $_POST['name-x'] ) ? 'checked="checked"' : '' ?> />
Where x could be your dynamic ID. Notice the addition of the PHP code and using a ternary operator, you can check the checkbox if the $_POST['name-x'] is set.
Add a value to the checkbox.
<input type="checkbox" name="name[]" id="name0" value="name0" />
<input type="checkbox" name="name[]" id="name1" value="name1" />
<input type="checkbox" name="name[]" id="name2" value="name2" />
However, you need to match this value in your PHP code.
<?php
if ( isset( $_POST['name'] ) ) {
$values = array();
foreach( $_POST['name'] as $value ) {
array_push( $values, $value );
}
}
?>
Then you have to modify your checkbox again to have inline PHP.
<input type="checkbox" name="name[]" id="name0" value="name0" <?php echo in_array( "name0", $values ) ? 'checked="checked"' : '' ?> />
<input type="checkbox" name="name[]" id="name1" value="name1" <?php echo in_array( "name1", $values ) ? 'checked="checked"' : '' ?> />
<input type="checkbox" name="name[]" id="name2" value="name2" <?php echo in_array( "name2", $values ) ? 'checked="checked"' : '' ?> />
You can also create a function to display these inline codes to make it much cleaner. HTH!
i assume that you have a form that have some checkboxes and try to check which checkbox is checked. here is an example code
<form action="" method="post">
<label for="name">Name 1</label>
<input type="checkbox" name ="name[]" value="name1">
<label for="name">Name 2</label>
<input type="checkbox" name ="name[]" value="name2">
<label for="name">Name 3</label>
<input type="checkbox" name ="name[]" value="name3">
<label for="name">Name 4</label>
<input type="checkbox" name ="name[]" value="name4">
<button type="submit" name="submit">Submit</button>
</form>
and the php code
<?php
if(isset($_POST["submit"])){
$valueName = array();
foreach($_POST['name'] as $value){
array_push($valueName,$value);
print_r($valueName);
}
?>

Select all checkbox with jQuery form POST for multiple checkboxes

For a project I want to implement a nice filtering mechanism with multiple checkboxes. I get the checkboxes to work correctly as well as a jQuery function to automatically POST the form when checking a checkbox.
Now I want to add a "select all" checkbox above the checkboxes, but I cannot seem to find the correct way. I have tried a dozen solutions for (somewhat) similar questions but I cannot get it to work correctly and consistent.
The HTML part is something like this:
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" /> Select All colors<br/>
<label> <input type="checkbox" name="color[]" value="yellow"> Yellow</label><br/>
<label> <input type="checkbox" name="color[]" value="blue"> Blue</label><br/>
<label> <input type="checkbox" name="color[]" value="red"> Red</label><br/>
<label> <input type="checkbox" name="color[]" value="green"> Green</label><br/><br/>
<input type="checkbox" /> Select All brands<br/>
<label> <input type="checkbox" name="brand[]" value="Nike"> Nike</label><br/>
<label> <input type="checkbox" name="brand[]" value="Adidas"> Adidas</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeBrand"> SomeBrand</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeOtherBrand"> SomeOtherBrand</label><br/>
</form>
The jQuery part I use to post the form on each click on the checkbox (is not sufficient):
$('input:checkbox:').live('click',function() {
$(this).closest('form').submit();
});
My question now is what do I need for the jQuery part to make sure this works correctly?
I want to be able to click the label to deselect all from that group and select only that one checkbox. It also needs to POST the form for the array values. And lastly if all checkboxes are checked manually the "select all" one has to be checked as well.
Hopefully someone can help me out as I am stuck with this for a long time...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#colorall").click(function()
{
var checked_status = this.checked;
$(".color").each(function()
{
this.checked = checked_status;
});
});
$(".color").click(function(){
if($(".color").length == $(".color:checked").length) {
document.getElementById("colorall").checked = true;
} else {
$("#colorall").removeAttr("checked");
}
});
$("#brandall").click(function()
{
var checked_status = this.checked;
$(".brand").each(function()
{
this.checked = checked_status;
});
});
$(".brand").click(function(){
if($(".brand").length == $(".brand:checked").length) {
document.getElementById("brandall").checked = true;
} else {
$("#brandall").removeAttr("checked");
}
});
});
</script>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" id="colorall" /> Select All colors<br/>
<label> <input type="checkbox" name="color[]" value="yellow" class="color"> Yellow</label><br/>
<label> <input type="checkbox" name="color[]" value="blue" class="color"> Blue</label><br/>
<label> <input type="checkbox" name="color[]" value="red" class="color"> Red</label><br/>
<label> <input type="checkbox" name="color[]" value="green" class="color"> Green</label><br/><br/>
<input type="checkbox" id="brandall"/> Select All brands<br/>
<label> <input type="checkbox" name="brand[]" value="Nike" class="brand"> Nike</label><br/>
<label> <input type="checkbox" name="brand[]" value="Adidas" class="brand"> Adidas</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeBrand" class="brand"> SomeBrand</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeOtherBrand" class="brand"> SomeOtherBrand</label><br/>
</form>

make an array from checkbox values PHP

I've many check-boxes. I'd like to pull their values into a comma separated array.
If checkbox is diselected value will be empty so:
bar,parking,,,,,tv,etc
how would I do this? after making an array I will submit into a db.
<p>
<label for="meta_box_check_bar">bar</label>
<input type="checkbox" id="meta_box_check_bar" name="meta_box_check_bar" value="bar" />
<label for="meta_box_check_parking">parking</label>
<input type="checkbox" id="meta_box_check_parking" name="meta_box_check_parking" value="parking" />
<label for="">accessible-for-disabled</label>
<input type="checkbox" id="meta_box_check_accessible-for-disabled" name="meta_box_check_accessible-for-disabled" value="accessible-for-disabled" />
<label for="">air-conditioning</label>
<input type="checkbox" id="meta_box_check_air-conditioning" name="meta_box_check_air-conditioning" value="air-conditioning" />
<label for="">frigobar </label>
<input type="checkbox" id="meta_box_check_frigobar" name="meta_box_check_frigobar" value="frigobar" />
<label for="">pets</label>
<input type="checkbox" id="meta_box_check_pets" name="meta_box_check_pets" value="pets" />
<label for="">phone</label>
<input type="checkbox" id="meta_box_check_phone" name="meta_box_check_phone" value="phone" />
<label for="">tv</label>
<input type="checkbox" id="meta_box_check_tv" name="meta_box_check_tv" value="tv" />
<label for="">typical-local-dishes</label>
<input type="checkbox" id="meta_box_check_typical-local-dishes" name="meta_box_check_typical-local-dishes" value="typical-local-dishes" />
</p>
/* make an array for all used checkbox */
$used_checkboxes = array();
/* make an array whit all options */
$avaible_checkboxes = explode(',', "bar,parking,accessible-for-disabled,air-conditioning,frigobar,pets,phone,tv,typical-local-dishes");
/* loop troguht all avaible checkboxes */
foreach($avaible_checkboxes as $current_key)
{
/* check if the checkbox was sent */
if(isset($_POST["meta_box_check_{$current_key}"]))
{
/* if sent, add key to list */
$used_checkboxes[$current_key] = $current_key;
}
else
{
/* if not sent, add empty value to list */
$used_checkboxes[$current_key] = '';
}
}
/* convert list to csv */
$used_checkboxes_csv = implode(',', $used_checkboxes);
Name your fields checkboxes[], then in PHP you can get a similar array with $_GET['checkboxes'].
Sounds like a job for ajax to me. I would consider dojo or jquery to collect and pass the data.
You should give them all the same name for the name field that way when you do the get on the name it'll return array of all those values that are checked.
Here's a reference you can read about:
http://www.html-form-guide.com/php-form/php-form-checkbox.html
If possible, i recomend you to use [] in the variable names to pass an array and retrieve their values in the PHP file. Something like this:
HTML
<p>
<label for="meta_box_check_bar">bar</label>
<input type="checkbox" id="meta_box_check_bar" name="array[]" value="bar" />
<label for="meta_box_check_parking">parking</label>
<input type="checkbox" id="meta_box_check_parking" name="array[]" value="parking" />
<label for="">accessible-for-disabled</label>
<input type="checkbox" id="meta_box_check_accessible-for-disabled" name="array[]" value="accessible-for-disabled" />
<label for="">air-conditioning</label>
<input type="checkbox" id="meta_box_check_air-conditioning" name="array[]" value="air-conditioning" />
<label for="">frigobar </label>
<input type="checkbox" id="meta_box_check_frigobar" name="array[]" value="frigobar" />
<label for="">pets</label>
<input type="checkbox" id="meta_box_check_pets" name="array[]" value="pets" />
<label for="">phone</label>
<input type="checkbox" id="meta_box_check_phone" name="array[]" value="phone" />
<label for="">tv</label>
<input type="checkbox" id="meta_box_check_tv" name="array[]" value="tv" />
<label for="">typical-local-dishes</label>
<input type="checkbox" id="meta_box_check_typical-local-dishes" name="array[]" value="typical-local-dishes" />
</p>
PHP
// Prevent errors when nothing is checked
if( ! is_array($_POST['array']) )
$_POST['array'] = array();
// Possible items
$possible_item[] = "bar";
$possible_item[] = "parking";
$possible_item[] = "accessible-for-disabled";
$possible_item[] = "air-conditioning";
$possible_item[] = "frigobar";
$possible_item[] = "pets";
$possible_item[] = "phone";
$possible_item[] = "tv";
$possible_item[] = "typical-local-dishes";
// Starting output string
$output = '';
// Loop the possible items
foreach($possible_item as $value)
{
// If item is in POST array, add to the output string, else put just comma if needed
if( in_array($value, $_POST['array']) )
$output .= ( empty($output) ? '' : ',' ) . $value;
else
$output .= ( empty($output) ? '' : ',' );
}
echo $output;

Automatic check if input filled

There are 10 boxes in my website that I fill up based on my needs. From php code I prefer not to check them one by one like this. Instead of that I thought it would be a good idea to put check marks in each boxes and if I fill something in input field it should be checked so I can check however many checkboxes are filled and know how many boxes are filled.
if ($input1) {$total = "1";
if ($input2) {$total = "2";
}
}
Anybody knows how can I put automatically check into a checkbox when I start typing anything in it ? But it should be unchecked back if I delete what I wrote before sending it. Or if you guys have a better idea that would be nice also.
Thank you !
you could do this in JavaScript. Basically if the number of input text is the same of the checkboxes then you could use this function for example:
function change() {
var input_lengths = document.getElementsByName("textArray[]");
for(var i= 0; i < input_lengths.length;i++) {
if(document.getElementsByName("textArray[]").item(i).value != "") {
document.getElementsByName("checkArray[]").item(i).checked = true;
}
}
}
and your html could be:
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="AAA" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="button" value="test" onclick="javascript:change()" />
why don't you name your checkboxes as an array e.g.:
<input type="checkbox" name="boxes[1]" />
<input type="checkbox" name="boxes[2]" />
then you can loop through the array in php and check each one individually
This is how i understood your problem:
you have 10 text inputs
you want to know how many of them contain actual input when they are submitted
your idea was to assign a checkbox input to each text input so that the corresponding checkboxes are checked when an input has text and is unchecked when an input has no text
Something very quick and dirty. You might want to name you inputs with a square bracket, so that php interprets that parameter as an array. Then you can iterate over an array and if the values are set you can count.
<html>
<body>
<?php
if(isset($_POST['input'])) {
$count = 0;
foreach($_POST['input'] as $value) {
if($value) {
$count++;
}
}
echo $count;
}
?>
<form action="testinputs.php" method="post">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input type="submit"/>
</form>
</body>
</html>
If you want to do that on the client side, than I'd take something like jQuery and look how many inputs on my page contain any text inside them.

2 options, PHP to ignore details of second if first is selected and vice versa

I'm having some trouble with some PHP.
Here's a shortened version of the HTML:
<label for="yes_or_no">would you like to tell me your favourite colours?</label>
<input type="radio" name="yes_or_no" id="yes" value="yes" />
<label for="yes">yes</label>
<input type="radio" name="yes_or_no" id="no" value="no" />
<label for="no">no</label>
<div id="colours_box">
<label for="colours">great! please select from the following list:</label>
<input type="checkbox" name="colours[]" id="blue" value="blue" />
<label for="blue">blue</label>
<input type="checkbox" name="colours[]" id="yellow" value="yellow" />
<label for="yellow">yellow</label>
<input type="checkbox" name="colours[]" id="red" value="red" />
<label for="red">red</label>
<input type="checkbox" name="colours[]" id="green" value="green" />
<label for="green">green</label>
<input type="checkbox" name="colours[]" id="other_colour" value="other_colour" />
<label for="other_colour">other_colour</label>
<div id="other_colour_box">
<textarea name="other_colour_detail" id="other_colour_detail"></textarea>
</div>
</div>
The colours_box DIV is hidden and appears when #no is selected and disappears when #yes is selected using some basic JavaScript. The other_colour_box DIV does a similar thing- it's hidden by default and when #other_colour is checked it appears, when it's unchecked it disappears.
What I'd like it to do is this:
if 'yes' is selected in the first instance, all the checkboxes and textarea are ignored, even if they selected 'no' first and entered details to the checkboxes and textarea.
if the other_colour_detail textarea has been written in but 'other_colour' has subsequently been unchecked, nothing is returned for the 'other_colour_detail' textarea
Here's the PHP:
$yes_or_no = $_POST['yes_or_no'] ;
$colours = $_POST['colours'] ;
$other_colour_detail = $_POST['other_colour_detail'] ;
$colours_to_email .= implode("\n\t", $colours) ;
if (($yes_or_no == 'no') && ($colours != "")) {
$colours_to_email ;
}
if (($yes_or_no == 'no') && ($other_colour != "") && ($other_colour_detail != "")) {
$details_of_other_colour = ":\n\t$other_colour_detail" ;
}
This would then feed back to me via email something like this:
"Did they want to tell me which colours they preferred?\n" .
"$yes_or_no-\t" . "$colours_to_email" .
"$details_of_other_colour" ;
Thanks for having a look,
Martin.
You should disable the colours[] elements when "No" is checked. Disabled elements are not submitted:
<script type="text/javascript">
function toggle_colour_inputs(enabled) {
if ( "yes" ) {
document.form1.colours.disabled=false;
}
else {
document.form1.colours.disabled=true;
}
}
</script>
<input type="radio" name="yes_or_no" id="yes" value="yes" onchange="toggle_colour_inputs(this.value)" />
<label for="yes">yes</label>
<input type="radio" name="yes_or_no" id="no" value="no" onchange="toggle_colour_inputs(this.value)" />
<label for="no">no</label>
<?php
$yes_or_no = $_POST['yes_or_no'] ;
$colours = $_POST['colours'] ;
$other_colour_detail = $_POST['other_colour_detail'] ;
$colours_to_email .= implode("\n\t", $colours) ;
if ($yes_or_no == 'no') {
if (count($colours) > 0) {
// colours to email
}
} else {
if (($other_colour != '') && ($other_colour_detail != '')) {
// details of other colour
}
}
?>

Categories