Having trouble getting my POST arrays to show all checkbox values from my form.
I have a form set up as follows:
<form name='foo' method='post' action=''>
<table>
<tr>
<td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
</tr>
<tr>
<td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
</tr>
<tr>
<td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
</tr>
</table>
</form>
I have a button at the bottom bound to a jquery function that adds 5 more empty rows to the form (hence the arrays for the input name cBox[]).
Now, the problem. Lets say the first checkbox is unchecked, and the last 2 are checked. When I output the values (using PHP print_r for debugging), I will get:
Array ( [0] => on [1] => on)
For some reason, the array does not contain any value for unchecked checkboxes.
I have seen some solutions where a hidden variable is passed with each checkbox, but can this solution be implemented in my situation (using arrays)?
That behavior is not surprising, as the browser doesn't submit any value for checkboxes that are unchecked.
If you are in a situation where you need to submit an exact number of elements as an array, why don't you do the same thing you do when there's an id of some sort associated with each checkbox? Just include the PHP array key name as part of the <input> element's name:
<tr>
<!-- NOTE [0] --->
<td class='bla'>Checkbox: <input type='checkbox' name='cBox[0]'/></td>
</tr>
<tr>
<td class='bla'>Checkbox: <input type='checkbox' name='cBox[1]'/></td>
</tr>
<tr>
<td class='bla'>Checkbox: <input type='checkbox' name='cBox[2]'/></td>
</tr>
That still leaves you with the problem that unchecked boxes will still not be present in the array. That may or may not be a problem. For one, you may really not care:
foreach($incoming as $key => $value) {
// if the first $key is 1, do you care that you will never see 0?
}
Even if you do care, you can easily correct the problem. Two straightforward approaches here. One, just do the hidden input element trick:
<tr>
<td class='bla'>
<input type="hidden" name="cBox[0]" value="" />
Checkbox: <input type='checkbox' name='cBox[0]'/>
</td>
</tr>
<tr>
<td class='bla'>
<input type="hidden" name="cBox[1]" value="" />
Checkbox: <input type='checkbox' name='cBox[1]'/>
</td>
</tr>
And two, which I find preferable, fill in the blanks from PHP instead:
// assume this is what comes in:
$input = array(
'1' => 'foo',
'3' => 'bar',
);
// set defaults: array with keys 0-4 all set to empty string
$defaults = array_fill(0, 5, '');
$input = $input + $defaults;
print_r($input);
// If you also want order, sort:
ksort($input);
print_r($input);
See it in action.
ONE TRICK is to override the checkbox value, if checked. otherwise its value will be 0.
<form>
<input type='hidden' value='0' name="smth">
<input type='checkbox' value='1' name="smth">
</form>
Try
<input type='checkbox' value="XXX" name='cBox[]'/>
<input type='checkbox' value="YYY" name='cBox[]'/>
<input type='checkbox' value="ZZZ" name='cBox[]'/>
Checkboxes work that way. If it is checked, only then the value is posted.
If you are handling dynamic checkbox array, you can try this:
HTML:
<label>
<input type="hidden" name="cBox[]" value="" />
<input type="checkbox" class="checkbox" value="on" />
</label>
<label>
<input type="hidden" name="cBox[]" value="" />
<input type="checkbox" class="checkbox" value="on" />
</label>
<!-- extend -->
Javascript (jQuery):
$(document).on("change", "input.checkbox", function() {
var value = $(this).is(":checked") ? $(this).val() : null;
$(this).siblings("input[name='cBox[]']").val(value);
});
Backend result (If only checked the second one):
// PHP $_POST['cBox']
Array
(
[0] =>
[1] => on
)
In this implement, the checkboxes are used to controller each hidden input in a group.
For displaying page, You can render each inputs pair back by assigning the value into hidden inputs and marking checkboxes as checked.
Try setting a value to each checkbox, of 1 or true.
<input type='checkbox' value='1' name='cBox[1]'/>
that may be why its not sending anything?
In controller:
request()->merge(['cBox' => request()->input('cBox', [])]);
Related
I have multiple radio button name in array with same values as 1 for all.
I need want to get if uncheck means set as zero how can I do that. Below is my code
check image its sample image but same form like i having :
html code :
<input type="radio" name="radio_name[]" id="radi_name" value="1" checked >
<label for="radio1">Set as Default</label>
PHP code :
$a[]=$_post['radio_name'];
prinr_r($a);
Current result :
Array ( [0] => 1 )
Expected result :
Array ( [0] => 1,[1] => 0,[2] => 0,[3] => 0 )
I have checked one radio button only, rest all need to zero.
use it like.
<form method='post' id='userform' action='test2.php'> <tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar[]' class="check" value='1'>1<br>
<input type='checkbox' name='checkboxvar[]' class="check" value='1'>2<br>
<input type='checkbox' name='checkboxvar[]' class="check" value='1'>3
</td> </tr> </table> <input type='button' class='buttons'> </form>
<script type="text/javascript">
$(document).ready(function(){
$('.buttons').click(function(){
var checkbox_arr = [];
$('.check').each(function(){
checkbox_arr.push(+$(this).is( ':checked' ));
});
console.log(checkbox_arr);
});
});
</script>
I have a table, that generates rows dynamically. I have used radio buttons for each row. I want the radio button to be checked even after I click my submit button.
This is body of my table:
<form action="" method="POST">
<table>
<thead>
<tr>
<td></td>
<td><h3>Date<h3></td>
<td><h3>Status<h3></td>
</tr>
</thead>
<tbody>
<?php for($i=$start;$i<$end;$i++)
{
$add=$ARRAY[$i]['source_address'];
$Status=$ARRAY[$i]['Status'];
$total=$add.$Status;
?>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $total; ?>"/></td>
<?php
echo'<td>'.$ARRAY[$i]['escl_date'].'</td>';
echo'<td>'.$ARRAY[$i]['escl_status'].'</td>';
?>
</tr>
<?php }?>
</tbody>
</table>
<input type="submit" name="details" value="details" />
How Can I make the radio button selected even after clicking on submit button? Please help me.
You can try this, You need a condition to check with reference value and add checked attribute.
Here your reference value is your POST of ID value. Add this into your radio tag <?php echo $_POST['ID'][0]==$total ? 'checked':'';?>
<input type="radio" name="ID[]" value="<?php echo $total; ?>" <?php echo $_POST['ID'][0]==$total ? 'checked':'';?> />
The correct HTML attribute you are looking for is checked.
Note to other answer: readonly is not necessary, nor is it what OP is asking for.
Since you're doing it in PHP, let's say you're passing something with an HTML markup name of foobar, as follows:
<form><input type="radio" name="foobar"></form>
To make sure that the value is retained even after it is submitted, you could add a code like this (get or post depending on your purpose):
<form><input type="radio" name="foobar" <?php if(isset($_GET[foobar])){ echo "checked"; ?>></form>
This is to make sure that it would be checked if the name was passed in the form.
Note that this is just an example.
Try this, it works:
<input type="radio" checked>
I have a large HTML form where I currently insert filled html fields into and array. When I print out my $_POST['rep_list']; it gives me the keys and values filled out from the form. But what I need is an array with all form fields (even the ones not filled out). Any suggestion/examples on how to achieve this?
<form action="insert-data.php" name="workCard" method="POST">
<fieldset>
<legend><h3>Repair</h3></legend>
<table>
<tr>
<th>Description</th>
<th>Front</th>
<th>Back</th>
</tr>
<tr>
<td>tire</td>
<td><input type="checkbox" name="rep_list[tire_front]" value="tire_front" /></td>
<td><input type="checkbox" name="rep_list[tire_back]" value="tire_back" /></td>
<td>alm <input type="checkbox" name="rep_list[tire_reg]" value="tire_reg" /></td>
<td>indl <input type="checkbox" name="rep_list[tire_indl]" value="tire_indl" /></td>
</tr>
<tr>
<td>Tube</td>
<td><input type="checkbox" name="rep_list[tube_front]" value="tube_front" /></td>
<td><input type="checkbox" name="rep_list[tube_back]" value="tube_back" /></td>
</tr>
<tr>
<td>Hub.rep.</td>
<td><input type="checkbox" name="rep_list[hub_front]" value="hub_front" /></td>
<td><input type="checkbox" name="rep_list[hub_back]" value="hub_back" /></td>
<td>just. <input type="checkbox" name="rep_list[hub_adjust]" value="hub_adjust" /></td>
etc…....
If you want to do this without javascript you can create text input's for all checkboxes. For example:
<td>
<input type="hidden" name="rep_list[tire_front]" value="False" />
<input type="checkbox" name="rep_list[tire_front]" value="True" />
</td>
When you check the POST in PHP you can determine if a checkbox is checked or not by checking the value .When the POST value is True the checkbox was checked and False is unchecked.
With checkboxes if they are ticked they will be set in the HTTP post with their given value (or default of 'on' if a value is not set). If they are not ticked no value will be sent.
To check if a checkbox was ticked in PHP you could simply check if a POST value was set:
$tire_front = isset($_POST['rep_list']['tire_front']);
If you wanted to do this for every item you could create an array of the keys and loop through each of these:
$fields = array(
'tire_front',
'tire_back',
'tire_reg',
'tire_indl',
'tube_front',
'tube_back',
'hub_front',
'hub_back',
'hub_adjust'
);
foreach ($fields as $field) {
$rep_list[$field] = isset($_POST['rep_list'][$field]);
}
$rep_list would then contain an entry for each of the fields with a value of true if ticked, or false if not.
I try to list ALL $_POST array items using var_dump (or echo), but null value items are not displayed. If I use var_dump($_POST) null doesn't appears, but if I use var_dump($_Post["nullitem"]) null appears:
<html>
<head>
</head>
<body>
<?php
if ($_POST["submit"]){
var_dump($_POST);
foreach ($_POST as $key => $value) {
echo $key."=>";
echo $value;
echo " - ";
}
echo "<br>";
echo "ck_1 "; var_dump($_POST["ck_1"]);
echo "ck_2 "; var_dump($_POST["ck_2"]);
echo "ck_3 "; var_dump($_POST["ck_3"]);
}
?>
<form action='test.php' method='post' name='form_example' id='test'>
<label for='ck_1'>
<input type='checkbox' value=1 id='ck_1' name='ck_1' />
1 </label>
<label for='ck_2'>
<input type='checkbox' value=1 id='ck_2' name='ck_2' checked='checked' />
2 </label>
<label for='ck_3'>
<input type='checkbox' value=1 id='ck_3' name='ck_3' />
3 </label>
<input type='submit' name='submit' value='Submit' />
</form>
</body>
</html>
Only ck_2 is checked, so this example will output :
array
'ck_2' => string '1' (length=1)
'submit' => string 'Submit' (length=6)
ck_2=>1 - submit=>Submit -
ck_1 null
ck_2 string '1' (length=1)
ck_3 null
How can I include ALL $_POST values in foreach loop (I don't know how many keys nor names in $_POST array)
Thanks for help
Regards
Sorry.
The unchecked checkbox is not set, so is not member of $_POST array and does not appears
A way to get a value for unchecked checkbox is to set an hidden field with same name and id and unchecked value (like 0), so at post time if unchecked hidden value is returned :
<input type="hidden" name="cx1" value="0" />
<input type="checkbox" name="cx1" value="1" />
Thank's Midzai
I think you are including all the values of $_POST array in your foreach. The thing is, if you don't check in the checkbox the $_POST array won't contain it's key nor it's value.
checkbox i believe has only one value possible and that shows only when you "check-in" the checkbox. othervise the $_POST isn't populated with the key. Why you see NULL when you direcly query the $_POST with the specified key name (name of the checkbox that wasn't set) the key does not exist in the $_POST array and to return something it returns NULL.
If you for some obscure reason need to list all the checkboxes that were available to be chcecked in to the user, you can add
<input type='hidden' name='cbNames[]' value='ck_1'/>
<input type='hidden' name='cbNames[]' value='ck_2'/>
<input type='hidden' name='cbNames[]' value='ck_3'/>
for each of the checkboxes on your site and then list through the $_POST['cbNames'] array and query the $_POST for those:
foreach ($_POST['cbNames'] as $cbName)
print $_POST[$cbName];
Try this
<html>
<head>
</head>
<body>
<?php
if ($_POST["submit"]){
echo "<pre>";
print_r(array_filter($_POST["ck_1"]));
echo "</pre>";
}
?>
<form action='test.php' method='post' name='form_example' id='test'>
<label for='ck_1'>
<input type='checkbox' value=1 id='ck_1' name='ck_1[]' />
1 </label>
<label for='ck_2'>
<input type='checkbox' value=1 id='ck_2' name='ck_2[]' checked='checked' />
2 </label>
<label for='ck_3'>
<input type='checkbox' value=1 id='ck_3' name='ck_3[]' />
3 </label>
<input type='submit' name='submit' value='Submit' />
</form>
</body>
</html>
I'm getting database from database and each row has a id and im showing it like this in html
<td><input type="checkbox" value"1">test1</td></tr>
<td><input type="checkbox" value"2">test2</td></tr>
and so on...
now lets say that user checked ten check boxes out of 15 and then clicked submit .
how to get values of those boxes in php???
Your checkboxes need to have a name & a value attribute:
<input type='checkbox' name='test1' value='1'> Test1
<input type='checkbox' name='test2' value='1'> Test2
then when that is posted you can access the values in PHP via $_POST:
$test1 = $_POST['test1']
$test2 = $_POST['test2']
Keep in mind that the values will only be returned if the box is checked, so most likely instead of the above PHP, you're more than likely just going to want to check if the value exists.
give the checkboxes names:
<td><input type="checkbox" value"2" name='test'>test2</td></tr>
Than in php just read the request
$test = $_REQUEST['test']
if the OP doesn't have these checkboxes inside of a , any amount of PHP code will make absolutely no difference (FROM Blender)
they will be in either the $_GET or the $_POST array
Try this way..
<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="1"><label>Test 1</label><br/>
<input type="checkbox" name="check_list[]" value="2"><label>Test 1</label><br/>
<input type="checkbox" name="check_list[]" value="3"><label>Test 3</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
}
}
?>