Below is the code for the checkbox
foreach($fruits as $key=>$values) {
echo "<td bgcolor=\"#EAEAEA\" style=\"color:#003399\"><input type=\"checkbox\" name=\"fruits[]\" value='$fruits[$key]'>$fruits[$key]</td>";
How can i retain the values selected once i click submit?
Checkboxes don't use value attributes. You have the attribute checked='checked' or simply checked which means it's selected. You then get a $_POST array where if you have the checkboxes checked, they will show up with the names in the keys, but if not checked, they will not be submitted at all. So you would need to make hidden fields if you really want to pass these unchecked checkboxes as well. See this post on the details of how to accomplish it: Post the checkboxes that are unchecked
Related
I have a foreach statement that echos radio buttons.
All those radio buttons have same name.
When I want to get the clicked ones, I use $_POST['radio_name']
But I got an error (it can't find the radio name)
This my code :
<form method="post">
<div class="repas-inside-bloc breakfast-bloc" id="brkID">
<?php
foreach($breakfast_array as $brk){
echo '<label for="'.$brk['id_plat'].'" class="plan-meal-box">'.$brk['titre_plat'].'</label><input name="brk_check" type="radio" id="'.$brk['id_plat'].'" value="'.$brk['titre_plat'].'">';
}
?>
</div>
How to get values of every radio button clicked ? $_POST['brk_check'] doesn't work
The idea of radio buttons is they are grouped by their name attribute. If all radio buttons have same name, only one can be clicked at a time.
Therefore, the server value you receive as $_POST['brk_check'] is value of radio button that was clicked, which can only be one value at any given time.
If you want to receive multiple values, you have to name radio buttons differently. But with this usage, if you want to allow multiple selections, you should probably type='checkbox' instead of radio buttons. Here is an answer of how to read multiple checkbox values.
You can try this:
$_REQUEST['brk_check'];
I'm creating a CMS in which I have an overview of pages. I want the user to be able to mass delete these pages and so I have created a form in which each page has a checkbox with the pages database ID as value and name:
<input class="mass-delete-check" type="checkbox" name="<?=$page["id"]?>" value="<?=$page["id"]?>" id="<?=$page["id"]?>">
Now when I submit this form I need to get the values of the checkboxes that are actually checked and put them in an array I can go through to delete them. The thing here is that I will have to get checkbox values based on if they are checked and not on their name because I can't know all names.
Does anyone have a solution to this?
Use the same name for all checkboxes. So after submiting you will have array with page IDs to delete.
<input class="mass-delete-check" type="checkbox" name="delete_pages[]" value="<?=$page["id"]?>" id="<?=$page["id"]?>">
After submit you would get array of IDs with $_POST['delete_pages'], which contains actual page IDs what you need to delete.
I am developing apps in Codeigniter, i am stuck in some code...i just want to know how can i get complete row values if any one check the checkbox and submit, so i should get row value of selected checkbox....
You can use array for while creating rows, use name for your each row's input field as array and when you submit the your form loop over your checkbox array and get the values from other elements using the same key which selected checkbox is having
I have a checkbox field in my HTML table. The table is generated dynamically, and the field is stored in an array as follows:
<input type="checkbox" name="checked[]" value="1">
In the PHP, I am inserting the checked rows into the table, but I also need to display the rows that have not been checked to the user after the submission, but there is no way of knowing which rows were not passed since checked[] for unchecked checkboxes are not being submitted.
What I want to achieve is the user is displayed a table with multiple rows, he checks which rows he wants to add to the database. After form submission, a page is to display which rows were inserted and which rows were not selected by the user. The unchecked rows need not be inserted into any database, but should be displayed to the user only ONCE, right after the submission, so that he can print the page for record purpose.
What is the best way to tackle this problem?
since you are creating the chekbox in first place so you know the total number of checkbox.
Total Checkbox minus Checked checkbox will give you what you want i.e unchecked box.
unchecked boxes = total checkboxes - checked checkboxes ;
Use 3 arrays, 1 holding the options you are sending, one with the responses from the user and one empty that will hold the difference. The difference between the two will get you the unchecked fields.
<?php
$myOptions = array('option1', 'option2', 'option3');
$userArray = array('option2'); // This is your $_POST['checkboxes'] array
$leftOptions = array();
foreach ($myOptions as $value){
if (!in_array($value, $userArray)){
$leftOptions[] = $value;
}
}
?>
Note: you can replace the foreach with array_diff as #deceze mentioned in the comment.
When a user ticks a checkbox and enters data into the corresponding textbox and submits, I want to show a form which will display the textbox values which checkboxes were ticked
Have you considered trying a tutorial and seeing how that works?
http://www.homeandlearn.co.uk/php/php4p11.html