Checkbox Array only showing checked values - PHP - php

I have a form with many checkboxes.
ex.
...
<input name="dodatkowe[]" type="checkbox" value="1" />
<input name="dodatkowe[]" type="checkbox" value="1" />
<input name="dodatkowe[]" type="checkbox" value="1" />
...
I want to have all the checkboxes in the array. Array 'dodatkowe'.
When i checked all checkboxes have:
Array ( [0] => 1 [1] => 1 [2] => 1 )
but when i checked example only second I have:
Array ( [0] => 1 )
I need that, when i check example second checkbox:
Array ( [0] => 0 [1] => 1 [2] => 0)

give them indexes so you can reference them specifically...
...
<input name="dodatkowe[1]" type="checkbox" value="1" />
<input name="dodatkowe[2]" type="checkbox" value="1" />
<input name="dodatkowe[3]" type="checkbox" value="1" />
...
Not sure why you feel you need to see the unchecked values, this can be assumed to be the inverse of the checked values.... Any attempt to do this is a hack, and is unnecessary.

If a checkbox isn't checked it won't include it's value into the parameters but the first step would be to give the checkboxes a unique id:
<input name="dodatkowe[0]" type="checkbox" value="1" />
<input name="dodatkowe[1]" type="checkbox" value="1" />
<input name="dodatkowe[2]" type="checkbox" value="1" />
Then you can use PHP to check is the value is there:
$maxfields = 3;
$selectboxes = $_REQUEST['dodatkowe'];
for($i = 0; $i < $maxfields; $i++)
if(!isset($selectboxes[$i])) $selectboxes[$i] = 0;
This will set all non existent fields to 0 and $selectboxes should contain the result you are looking for.

Related

How to create an Array from the checked boxes

I have 1000 checkboxes of object fields and I want to create an Array from all the fields that I check after submitting using PHP can you tell me how to do it?
well if you name your checkbox like this, you already have an array:
<input name="mycheckbox[]" value="1"> hello1
<input name="mycheckbox[]" value="2"> hello2
<input name="mycheckbox[]" value="3"> hello3
in PHP you will get:
print_t($_REQUEST['mycheckbox']);
/*
[
0 => '1',
0 => '2',
0 => '3'
]
*/
<input type="checkbox" name="somename[]" value="somevalue"> if you need an array with numerical indices
or
<input type="checkbox" name="someobject[property]" value="somevalue"> if you are looking for an associative array

Set the uncheck checkbox to 0 without using hidden textbox

Im trying to get all of the checked values using array i already get all of the checked box and get the value but my problem is i want to pass to array all of value of checbox, 1 for check and 0 for uncheck. sorry for the english.
this is what i want to get:
array[0] = 1 //check
array[1] = 0 // uncheck
// and so on.
i have already tried a hidden box with same name of checkbox but it give me wrong data of array and i am sure its not the answer
<input type = "checkbox" name="checkbox[]" value="1">
$checkbox1 = ($_POST['checkbox'] <> 0) ? ($_POST['checkbox']) : (empty($_POST['checkbox'])) ?'0' : '';
output: 0
output of print_r($checkbox1) : 0
expected output : array[0]=> 1 --> check array[1] => 0 -> uncheck
When working with a PHP backend where you want to post an array of values (using the name="something[]" syntax) you need to create <input type="hidden"> and <input type="checkbox"> pairs that will refer to the exact same array index.
You do so by explicitly defining the index in the name attribute.
For example
<input type="hidden" name="checkbox[0]" value="0">
<input type="checkbox" name="checkbox[0]" value="1">
<input type="hidden" name="checkbox[1]" value="0">
<input type="checkbox" name="checkbox[1]" value="1">
<input type="hidden" name="checkbox[2]" value="0">
<input type="checkbox" name="checkbox[2]" value="1">
I believe this also works for ASP.NET-MVC backends. Not sure about others though.
If you're looping over records or a range, this is as simple as keeping an iteration index and using it in the name attributes
<?php for ($i = 0; $i < $range; $i++): ?>
<input type="hidden" name="checkbox[<?= $i ?>]" value="0">
<input type="checkbox" name="checkbox[<?= $i ?>]" value="1">
<?php endfor ?>

php inserting multiple radio values with same name

I'm trying to insert multiple values using radio, here is my example:
<input type="radio" name="toppingPrice[]" value="<?= $topping['name'];?>-<?= $topping['price'];?>">
this one work if I insert single input, but if I want to insert more than one for example:
Size: small, medium, large <- name="toppingPrice[]" for all input values
Cheese: yes, no <- name="toppingPrice[]" for all input values
Level: spicy, normal <- name="toppingPrice[]" for all input values
this will not work because it will merge into 1 group so if I have to choose only one of all toppings.
my original code looks like:
foreach ($toppingPrice as $key) {
list($toppingName,$toppingNameEn, $toppingPrice) = explode("-",$key,3);
$tName[] = $toppingName;
$tNameEn[] = $toppingNameEn;
$tPrice += $toppingPrice;
}
$tn = implode(",", $tName);
$tn_en = implode(",", $tNameEn);
$price = $price + $tPrice;
Html:
<input type="checkbox" name="toppingPrice[]" id="<?= $topping[0];?>" value="<?= $topping['name'];?>-<?= $topping['name_en'];?>-<?= $topping['price'];?>" <? if($topping['price'] < 1){echo "checked";}?>>
I hope I delivered the question in the right way
please give me any idea or solution for fix this issue
You should use name attribute as shown in below code.
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
<form name="test" method="post">
<input type="radio" name="toppingPrice.size[]" value="1"> 1
<input type="radio" name="toppingPrice.size[]" value="2"> 2
<input type="radio" name="toppingPrice.size[]" value="3"> 3
<br>
<input type="radio" name="toppingPrice.toping[]" value="4"> 4
<input type="radio" name="toppingPrice.toping[]" value="5"> 5
<input type="radio" name="toppingPrice.toping[]" value="6"> 6
<br>
<input type="radio" name="toppingPrice.level[]" value="7"> 7
<input type="radio" name="toppingPrice.level[]" value="8"> 8
<input type="radio" name="toppingPrice.level[]" value="9"> 9
<button type="submit" value="save" /> Save
</form>
This will be your $_POST after form submit
Array
(
[toppingPrice_size] => Array
(
[0] => 1
)
[toppingPrice_toping] => Array
(
[0] => 5
)
[toppingPrice_level] => Array
(
[0] => 9
)
)

Foreach loop for two arrays?

I have two arrays. One for input boxes and other for checkbox.
inputbox[] checkbox[]
inputbox[] checkbox[]
.
.
.
.
submit button
When I fill check box1 and fill the value in input box1 and try to submit.
Foreach fails because it pass all the indexes of input boxes but only passes checked checkbox.
foreach(array_combine($checkbox, $inputbox) as $check => $input)
Please tell me what can i do?
if you have a control over the HTML form you can make the form in following manner
<input type="text" name="name[1]" />
<input type="checkbox" name="check[1]" />
<input type="text" name="name[2]" />
<input type="checkbox" name="check[2]" />
<input type="text" name="name[3]" />
<input type="checkbox" name="check[3]" />
<input type="text" name="name[4]" />
<input type="checkbox" name="check[4]" />
in that case you will get the post array in the following manner
Array
(
[name] => Array
(
[1] => Swapnil
[2] =>
[3] => Sarwe
[4] => Swapnil Sarwe
)
[check] => Array
(
[1] => on
[3] => on
)
)
Now you can loop over the name(input box) and then check isset for the isset($_POST['check'][$key]) and set the default value
Iterate over textboxes (which are guaranteed to all be present), then fetch the corresponding checkbox (probably by ID, if you have some kind of ID correspondence between them - which you should).

Show checkbox as checked when value matches php

Retriving the roles of user using user id
$userId = $userDetails['user_id'];
$stmt = $db->query ( "SELECT user_role_id FROM user_role_xref where user_id=" . $userId);
$userRoles = $stmt->fetchAll ();
print_r($userRoles);
When i print this
Array (
[0] => Array (
[user_role_id] => 3
)
[1] => Array (
[user_role_id] => 4
)
[2] => Array (
[user_role_id] => 5 )
)
how to show check box as checked when value matches else checkbox is unchecked
<input type="checkbox" id="roles_1" value="3" name="roles[]" />
<label for="roles_1">Role1</label><br />
<input type="checkbox" id="roles_2" value="4" name="roles[]" />
<label for="roles_2">Role2</label><br />
<input type="checkbox" id="roles_3" value="5" name="roles[]" />
<label for="roles_3">Role3</label><br/>
HTML attribute checked="checked".
E.g:
<input type="checkbox" <?php if ($var) echo ' checked="checked"'; ?> id="roles_2" value="4" name="roles[]" />
Use in_array function of php
Check if the value exists or not
If it exists then set checked property otherwise display unchecked.
<input type="checkbox" <?php if(in_array("roles_1",$roles_array){ echo 'checked="checked"';} ?> id="roles_1" value="1" name="roles[]" />

Categories