make an array from checkbox values PHP - 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;

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);
}
?>

Form Check boxes not coming through

I can not get the form check boxes to come through to the email for "recycling". I do not know php so I have no idea what is wrong. When the form comes through to email the "Recycling:" subject is there, but the checked boxes are not.
HTML:
<label>CRT Monitors <input name="recycleobject2[]" type="checkbox" value="crtmonitors"
/></label>
<label>Printers <input name="recycleobject2[]" type="checkbox" value="printers"
/></label>
<label>Computers <input name="recycleobject2[]" type="checkbox" value="computers"
/></label>
<label>Fluorescent Lamps and Batteries <input name="recycleobject2[]" type="checkbox"
value="lamps" /></label>
<label>Televisions <input name="recycleobject2[]" type="checkbox" value="television"
/></label>
<label>Other Equipment <input name="recycleobject2[]" type="checkbox" value="other" />
</label><br />
PHP:
$recycleobject2 = $_POST['recycleobject2']; // not required
$recycleobject2 = array();
$email_message .= "Recycling:" .implode(", ",$recycleobject2)."\n";
your problem is right here:
$recycleobject2 = array();
That line is resetting the $recycleobject2 variable to a brand new empty array. Remove that line and your code should be fine.

join () function if data entered

I have a jQuery mobile form with the data posting to a review.php form. When there is no data entered in an array of check boxs (no data needed from user) the review returns
Warning: join() [function.join]: Invalid arguments passed in /hermes/waloraweb076/b2830/moo.revolveis/hg/order/review.php on line 35
Here is a sample of the form:
<li data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Choose Grind:</legend>
<input type="checkbox" name="grind51[vgrind]" id="vgrind" class="custom" value="V-Grind">
<label for="vgrind">V-Grind</label>
<input type="checkbox" name="grind51[tourgrind]" id="tourgrind" class="custom" value="Tour-Grind">
<label for="tourgrind">Tour Grind</label>
<input type="checkbox" name="grind51[healgrind]" id="healgrind" class="custom" value="Heal-Grind">
<label for="healgrind">Heal Grind</label>
<input type="checkbox" name="grind51[nogrind]" id="nogrind" class="custom" value="No Grind">
<label for="nogrind">No Grind</label>
</fieldset>
</li>
Here is a sample of the review.php:
<?php
$grind51 = join(", ", $_REQUEST["grind51"]);
echo (!empty($_REQUEST['grind51'])) ? "<div class='reviewItem'><span class='reviewTitle'>51 Grind:</span>{$grind51}</div>" : "";
?>
When there is data entered for the array if returns the information fine. Is there a way to not get an error if no information is entered? By the way, line 35 pertains to
$grind51 = join(", ", $_REQUEST["grind51"]);
in my code.
Just check whether $_REQUEST['grind51'] is set before you try to join it. The error is caused be the fact that an empty return isn't an array (and can't be joined).
Your PHP looks good, the HTML I would change: set the element names to just name[] in order to populate a regular (not associative) array.
<li data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Choose Grind:</legend>
<input type="checkbox" name="grind51[]" id="vgrind" class="custom" value="V-Grind">
<label for="vgrind">V-Grind</label>
<input type="checkbox" name="grind51[]" id="tourgrind" class="custom" value="Tour-Grind">
<label for="tourgrind">Tour Grind</label>
<input type="checkbox" name="grind51[]" id="healgrind" class="custom" value="Heal-Grind">
<label for="healgrind">Heal Grind</label>
<input type="checkbox" name="grind51[]" id="nogrind" class="custom" value="No Grind">
<label for="nogrind">No Grind</label>
</fieldset>
</li>
Do some quick error checking in PHP to be sure you have data in an array passed in:
if ( isset( $_REQUEST['grind51'] ) && is_array( $_REQUEST['grind51'] ) ) {
$grind51 = join(", ", $_REQUEST["grind51"]);
}

HTML form with associated inputs

I've got a system where I allow a user to select multiple checkboxes from n amount of checkboxes, but also want two more inputs associated with each checkbox. This is for a message, a date and a time. When I post the data to be processed by a PHP script, I'd like to be able to access each of the sets of checkbox and two other inputs so I can see what date and time a user has filled in for each of the messages they've selected. I'm having trouble coming up with a method to associate the two other inputs with each checkbox.
Any ideas how to do this?
You can use arrays in your html inputs like so...
<input type="text" name="messages[1][message]" value="herp" />
<input type="text" name="messages[1][date]" value="24th April" />
<input type="text" name="messages[1][time]" value="13:00" />
<input type="text" name="messages[2][message]" value="derp" />
<input type="text" name="messages[2][date]" value="26th April" />
<input type="text" name="messages[2][time]" value="18:00" />
<?php
$messages = $_REQUEST['messages'];
foreach ($messages as $messageId => $value){
echo $value['message'];
echo $value['date'];
echo $value['time'];
}
Your HTML:
<input type="checkbox" name="car" />
<input type="text" name="msg_car" value="Car message" />
<input type="text" name="date_car" value="Car date" />
<input type="checkbox" name="bike" />
<input type="text" name="msg_bike" value="Bike message" />
<input type="text" name="date_bike" value="Bike date" />
<input type="checkbox" name="train" />
<input type="text" name="msg_train" value="Train message" />
<input type="text" name="date_train" value="Train date" />
<input type="checkbox" name="plane" />
<input type="text" name="msg_plane" value="Plane message" />
<input type="text" name="date_plane" value="Plane date" />
Your PHP script:
$array = array("car", "bike", "train", "plane");
for ($i = 0; $i < count($array); $i++) {
if (isset($_POST[$array[$i]])) {
//Checkbox was checked, get values
$msg = "";
$date = "";
$msg_id = "msg_" . $array[$i];
$date_id = "date_" . $array[$i];
if (isset($_POST[$msg_id]))
$msg = $_POST[$msg_id];
if (isset($_POST[$date_id]))
$date = $_POST[$date_id];
}
}
I think something like this should work. I didn't test it.. So forgive me if this example still contains some minor errors.

insert multi value from checkboxs into database by php

i want insert this form value to datanase :
<input type="checkbox" name="brand1" id="brand1" value="1"> <label for="brand1">Brand 1</label>
<input type="checkbox" name="brand2" id="brand2" value="1"> <label for="brand2">Brand 2</label>
<input type="checkbox" name="brand3" id="brand3" value="1"> <label for="brand3">Brand 3</label>
<input type="checkbox" name="brand4" id="brand4" value="1"> <label for="brand4">Brand 4</label>
<input type="checkbox" name="brand5" id="brand5" value="1"> <label for="brand5">Brand 5</label>
these text box are get by php from a table in database and may be Variable
i want insert to database by this format
if brand 1 are checked $brand="1,";
and Finally like this :
insert($name,$brands); and $brands = "1,2,3,4,5,";
if write this by if and while but it doesn't work because if insert run in while {} Five times insert Done and if insert run out of while {} , $brand = "5,"
thanks for your help or idea for this problem
it's mean :
<form method="post" action="#">
<?php
$result = $db->getall(brands);
if(!empty($result)) {
while ( list($key,$val)=each($result) ) {
$brand_id = stripslashes($val["id"]);
$brand_name = stripslashes($val["name"]);
?>
<input type="checkbox" name="brand<?php print"$brand_id"; ?>" value="1" style="cursor:pointer;"><label for="brand<?php print"$brand_id"; ?>" style="cursor:pointer;"> <?php print"$brand_name"; ?></label>
<?php }} ?>
Source Output:
<input type="checkbox" name="brand1" value="1"> <label for="brand1">Brand Name 1</label>
<input type="checkbox" name="brand2" value="1"> <label for="brand2">Brand Name 2</label>
<input type="checkbox" name="brand3" value="1"> <label for="brand3">Brand Name 3</label>
<input type="checkbox" name="brand4" value="1"> <label for="brand4">Brand Name 4</label>
<input type="checkbox" name="brand5" value="1"> <label for="brand5">Brand Name 5</label>
<input type="submit" value="Submit" />
</form>
when submit form , insert source is :
<?php
$result = $db->getall(brands);
if(!empty($result)) {
while ( list($key,$val)=each($result) ) {
$brand_id = brand.stripslashes($val["id"]);
$brand_name = stripslashes($val["name"]);
$brand_ids = "brand.$brand_id";
if($$brand_ids==1) {$brands="$brandid,"}
}} ?>
$db->add_submenu("$brands");
You should change the name of your checkboxes to brand[]. It will give you an array once submitted at $_POST['brand']
Ex.
<input type="checkbox" name="brand[]" value="1" ... />
<input type="checkbox" name="brand[]" value="2" ... />
<input type="checkbox" name="brand[]" value="3" ... />
<input type="checkbox" name="brand[]" value="4" ... />
<input type="checkbox" name="brand[]" value="5" ... />
on the other side you can either do something like the following:
// this will return '1, 2, 3, 4, 5' when all are selected.
$index = implode(", ", $_POST['brand']);
and at that point you will have the brands in comma delimited form.

Categories