Array of checkbox values in PHP - php

I have a HTML form with multiple checkbox selection. They are defined as:
<label class="container">Afghanistan
<input type="checkbox" id="Afghanistan" name="country[]" value="Afghanistan" checked="checked">
<span class="checkmark"></span>
</label>
<label class="container">Armenia
<input type="checkbox" id="Armenia" name="country[]" value="Armenia" checked="checked">
<span class="checkmark"></span>
</label>
...
After submitting, I call a PHP file where I want to store their values in an array.
for($i=0;$i<sizeof($_POST["country[]"]);$i++){
$country[i] = htmlspecialchars($_POST["country[i]"]);
}
But this code doesn't work. Can anyone help me to solve it?

$_POST["country"] is an array for which you can get the values using the index using $i
Try it like this:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
for($i=0;$i<sizeof($_POST["country"]);$i++){
$country[] = htmlspecialchars($_POST["country"][$i]);
}
echo $country[0];
}

Related

Use database to check a checkbox

I have a page to view assets with an Edit link. When I click the link it goes to edit_case.php which has a form to edit what elements of the row are in the database as checkboxes. However the boxes do not show them as checked. I have the following code...
// get already checked box values
$repop = "SELECT * FROM case_audit WHERE case_id = $case_id";
$popresults = mysqli_query($dbc, $repop);
$row = mysqli_fetch_array($popresults, MYSQLI_ASSOC);
print_r ($row);
The print_r does show the whole record row from DB. which is either a 1 or 0, checked || not checked.
The form...
<div id="facepics">
<label><input type="checkbox" name="Facial1" value="<?php echo $row['frontrest']; ?>" >Front at Rest </label><br>
<label><input type="checkbox" name="Facial2" value="<?php echo $row['frontbigsmile']; ?>" >Front Big Smille</label><br>
<label><input type="checkbox" name="Facial3" value="<?php echo $row['profile']; ?>" >Profile</label><br>
<label><input type="checkbox" name="Facial4" value="<?php echo $row['subvertex']; ?>" >SubMento Vertex</label><br>
</div>
I know I need to turn the 1's to "checked" just not sure how best to do that.
so basically checked="true" attribute in input creates a checked checkbox.
HTML Code looks like
<input type="checkbox" checked="true">
In your case you can do it like:
<input type="checkbox" name="Facial1" value="frontrest" <?= (intval($row['frontrest']) == 1) ? 'checked' : '';>
Also note that I changed value attribute, with frontrest so that you can identify the checkbox uniquely
EDIT: I have modified the code
<input type="checkbox" name="Facial1" <?=$row['frontrest']==1?'checked':''?>>
I often have the same issue where the browser ignores checked="false" and checks all
so I use
<input type="checkbox" checked>

PHP implode function not working right

So I am trying to email the results of a form using PHP. inside my form I have a bunch of checkboxes. The script below works when at least 1 checkbox in a group is checked. If none of the checkboxes are checked I receive the following error:
Warning: Implode() [function.implode]: Invalid augments passed in {name of php doc} on line {xxx} Array
Here is a sample of the code I'm using:
<?php
$data = array();
foreach ($_POST as $key => $value) {
$data[] = $value;
}
if(!$data[14]) //$data[14] is an array of checkbox values
{echo 'No User Selection';}
else
{echo implode(" | ", $data[14]);} //This is where the error occurs
?>
HTML code
<label for="b0" class="left">Item 1</label>
<input type="checkbox" name="b[0]" id="b0" value="Item 1"/>
<label for="b1" class="left">Item 2</label>
<input type="checkbox" name="b[1]" id="b1" value="Item 2"/>
<label for="b2" class="left">Item 3</label>
<input type="checkbox" name="b[2]" id="b2" value="Item 3"/>
ect....
Does anyone have an idea why I'm receiving this error?
Make sure the variable a) is set and b) is an array.
$data = array();
foreach ($_POST as $key => $value) {
$data[] = $value;
}
if ( !isset($data[14]) || !is_array($data[14]) ) {
echo 'No User Selection';
} else {
echo implode(" | ", $data[14]);
}
Always properly check variables using isset(), unless of course you like giant error logs! I also suggest using the $_POST keys as the keys for $data, thus making life even easier when you want to look up a specific $_POST item.
If the checkbox is not checked, it is not sent to the server.
I suggest you to do something like this:
<label for="b0" class="left">Item 1</label>
<input type="hidden" name="b[0]" value=""/>
<input type="checkbox" name="b[0]" id="b0" value="Item 1"/>
<label for="b1" class="left">Item 2</label>
<input type="hidden" name="b[1]" value=""/>
<input type="checkbox" name="b[1]" id="b1" value="Item 2"/>
<label for="b2" class="left">Item 3</label>
<input type="hidden" name="b[2]" value=""/>
<input type="checkbox" name="b[2]" id="b2" value="Item 3"/>
This way, you are sure that b[0], b[1], etc. are always sent
You are causing the problem yourself by making the data array. Just go straight to the POST array. Everything else is overcomplicating the problem.
<?php
if(!isset ($_POST['b']) || !is_array($_POST['b'])) {
echo 'No User Selection';
} else {
echo implode(" | ", $_POST['b']);
}
?>
In addition, if the content of your form changes slightly, or you want to reorder the fields then your magic number 14 will no longer work.
Your code will be unbelievably fragile unless you change it.

PHP form check box from db table (beginner)

Had an a question answered on a different post; now I need to solve the next step! :)
We have a DB that has secondary_phone_number as either a 1 or a 0.
In our View (MVC); I have
{
$secondaryNumber = $company->getSecondaryPhoneNumber();
if ($secondaryNumber = 1) {
$data['secondaryphonenumber'] = TRUE;
} else {
$data['secondaryphonenumber'] = FALSE;
}
}
Then in our template (HTML), I am trying to display either the checkbox as being checked if Yes (1) or No (2), depending on the DB entry:
<label>
<input type="checkbox" name="secondary_phone_number" value="1" {{#secondaryphonenumber}}checked="checked"{{/secondaryphonenumber}}/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" {{#secondaryphonenumber}}checked="checked"{{/secondaryphonenumber}}/>
No
</label>
The problem is, both checkboxes are displaying as Checked, no matter what the DB says. Any thoughts?
I'm not sure what kind of MVC framework you're using or you made your own framework, but your approach doesn't really care what is in secondaryphonenumber when generating checkbox
try something like this
<label>
<input type="checkbox" name="secondary_phone_number" value="1" <?=$secondaryphonenumber?"checked":""?>/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" <?=$secondaryphonenumber?"":"checked"?>/>
No
</label>
But I'd suggest using radio button instead because checkbox allows user to select both options.
Try this one..
<label>
<input type="checkbox" name="secondary_phone_number" value="1" <?php if($data['secondaryphonenumber'] == TRUE){?>checked="checked"<?php }?>/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" <?php if($data['secondaryphonenumber'] == FALSE){?>checked="checked"<?php }?>/>
No
</label>
Using radio button will be a better option..
Try this :
$secondaryNumber = $company->getSecondaryPhoneNumber();
if ($secondaryNumber = 1) {
$data['secondaryphonenumber'] = TRUE;
} else {
$data['secondaryphonenumber_false'] = TRUE;
}
<label>
<input type="checkbox" name="secondary_phone_number" value="1" {{#secondaryphonenumber}}checked="checked"{{/secondaryphonenumber}}/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" {{#secondaryphonenumber_false}}checked="checked"{{/secondaryphonenumber_false}}/>
No
</label>

Combine arrays depending on checked boxes

I have a form with check boxes.
I want it so that when a check box is checked, it includes an array.
<input type="checkbox" name="main" value="main" checked> Main/unsorted<br />
<input type="checkbox" name="art" value="art" checked> Art/literature/music<br />
<input type="checkbox" name="games" value="games" checked> Games/gaming<br />
If main is checked include the array 'main', if art is checked include the array 'art', etc.
I've tried, but I can't find a function that would work for this scenario.
Edit: I'm cheating a bit and am now doing it like so.
foreach($_GET as $get) {
$end = array_merge($end, $$get);
}
From your information it sounds like you want to merge an array depending on which checkboxes have been ticked? Am I correct in assuming this?
Is something like this what you are looking for?
<?php
$combinationArray = array();
$mainArray = array('item1','item2','item3');
$artArray = array('item4','item5','item6');
$gamesArray = array('item7','item8','item9');
if(isset($_POST['main']) && $_POST['main']=='main'){
$combinationArray = array_merge($combinationArray,$mainArray);
}
if(isset($_POST['art']) && $_POST['art']=='art'){
$combinationArray = array_merge($combinationArray,$artArray);
}
if(isset($_POST['games']) && $_POST['games']=='games'){
$combinationArray = array_merge($combinationArray,$gamesArray);
}
?>
HTML:
<form action="yourpage.php" method="post">
<input type="checkbox" name="main" value="main" checked> Main/unsorted<br />
<input type="checkbox" name="art" value="art" checked> Art/literature/music<br />
<input type="checkbox" name="games" value="games" checked> Games/gaming<br />
<button>
Submit
</button>
</form>

keeping radio button value after post

HI
i'm using a php page and i need to keep the value of and check box and radio button (checked or not checked) after post page.
how could i make it?
thanks
First get the radio button value.
$radiobuttonvalue = $_POST['radiobuttoname']
Then for each radio button with the same name, do this
<input type="radio" name="radiobuttonname" value="value" id="radiobuttonname" <?php if($radiobuttonvalue == "value") { echo 'checked="checked"';} ?>
You need something like:-
<?php
$postCheckboxName = '';
if (isset($_POST['checkbox_name']) || 'any_value' == $_POST['checkbox_name']) {
$postCheckboxName = ' checked="checked"';
}
?>
<input type="checkbox" name="checkbox_name" value="any_value"<?php echo $postCheckboxName;?> />
<?php
$postRadioName = '';
if (isset($_POST['radio_name']) || 'any_other_value' == $_POST['radio_name']) {
$postRadioName = ' checked="checked"';
}
?>
<input type="checkbox" name="radio_name" value="any_other_value"<?php echo $postRadioName;?> />
This code should get you going. I'm basically checking whether the POST value of either the checkbox / radio element is set or not & whether the corresponding element's value matches with my respective element's value or not.
Hope it helps.
Something like this:
<?php if (isset($_POST['checkbox_name']))?>
<input type="checkbox" checked="checked" value="<?php echo $_POST['checkbox_name'];?>" />
<?php} ?>
<?php if (isset($_POST['radio_name']))?>
<input type="radio" checked="checked" value="<?php echo $_POST['radio_name'];?>" />
<?php} ?>
What happens is that you check if the input variables are in the $_POST and if so you add checked="checked" to the input fields to make them checked.
This worked for me, and is self explanatory
sample code usage:
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="time" value="lunch" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='lunch' ){echo ' checked="checked"';}?>>Lunch</label>
<label class="radio-inline">
<input type="radio" name="time" value="dinner" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='dinner' ){echo ' checked="checked"';}?>>Dinner</label>
</div>

Categories