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

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

Related

saving multiple checkbox to database in PHP

i am having problem in printing the values of the different checkbox with the same name & different values..
PHP
//$infect_type=array();
$infect_type = isset($_POST['infect_type']) ? $_POST['infect_type'] : null;
$values= implode(",",$infect_type);
print_r($values);
HTML
<input type="checkbox" name="infect_type" value="Blood Born" />
<input type="checkbox" name="infect_type" value="Air Born" />
i can only get the value which is selected last before submitting.
Use array notation for checkboxes names:
<input type="checkbox" name="infect_type[]" value="Blood Born" />
<input type="checkbox" name="infect_type[]" value="Air Born" />
In this case $_POST['infect_type'] is going to be an array of checked values.
HTML
<input type="checkbox" name="infect_type[]" value="Blood Born" />
<input type="checkbox" name="infect_type[]" value="Air Born" />
PHP
<?php
$infect_type = $_POST['infect_type'];
foreach ($infect_type as $i) {
echo $i;
//Change the code here
}
?>
This will do.

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"]);
}

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;

getting multiple checkboxes names/id's with php

How can i get the names or id's of the multiple selected checkboxes on submit, using the PHP? Following is example form. Thanks.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="orange" id="orange">
<input type="checkbox" name="apple" id="apple">
<input type="checkbox" name="sky" id="sky">
<input type="checkbox" name="sea" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
Checkbox values are submitted from a form only if the checkbox is selected. What's more, it's the name attribute that counts, not the ID.
There are several ways of handling checkboxes in PHP:
Give all checkboxes the same name followed by a pair of square brackets, so the entire set is treated as an array. In this case, give each checkbox a value.
Give each checkbox a different name and a value.
Give each checkbox a different name, but no value.
In each case, you need to check for the existence of the checkbox name in the $_POST array.
For example:
<input type="checkbox" name="color[]" id="orange" value="orange">
<input type="checkbox" name="color[]" id="apple" value="apple">
To get the values for these checkboxes:
if (isset($_POST['color'])) {
$colors = $_POST['color'];
// $colors is an array of selected values
}
However, if each checkbox has a different name and an explicit value like this:
<input type="checkbox" name="orange" id="orange" value="orange">
<input type="checkbox" name="apple" id="apple" value="apple">
You still need to use isset():
if (isset($_POST['orange'])) {
// orange has been set and its value is "orange"
}
If you don't set a value, the default value is "on", but it won't be in the $_POST array unless it has been selected, so you still need to use isset().
You need to give the inputs the same name:
<input type="checkbox" name="selection[]" value="orange">
<input type="checkbox" name="selection[]" value="apple">
<input type="checkbox" name="selection[]" value="sky">
<input type="checkbox" name="selection[]" value="sea">
Then iterate over the $_POST['selection'] array in PHP.
You won't get the ids but the names will be associative indexes in the $_POST array (and $_REQUEST). NOTE: They will only be available in the array if they were checked by the client.
if ($_POST['oragne'] == 'on')
You can set them up to post to PHP as arrays, if you build them similar to below:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="box_group_1[oragne]" id="oragne">
<input type="checkbox" name="box_group_1[apple]" id="apple">
<input type="checkbox" name="box_group_1[sky]" id="sky">
<input type="checkbox" name="box_group_1[sea]" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
print_r($_POST['box_group_1']);
?>

Submit mutiple checkbox selections in contact form

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!

Categories