Ive got a 3 part form and i want to store the users selections in a session so if they need to go back and change a selection they can.
The form consists of radio buttons and checkboxes. I have it working for the radio buttons they are stored in a session so it always remembers.
I can't think how to go it working on the checkboxes as they are stored in an array.
This is my form:
So when they select jam and Flora, i get an array like this
Array ( [0] => Jam [1] => Flora )
This is my checkbox:
<input type="checkbox" name="<?php echo $course_menuname."extras[]"; ?>" <?php if($_SESSION[''.$course_menuname.''] == $extraitems) { echo "checked"; } ?> value="<?php echo $extraitems; ?>">
So i need a session to store each of the values they selected then if its one they selected put a check on the checkbox.
Hope this makes sense.
Thanks
i was also suffering from same problem, below solution worked fine for me, make appropriate changes before use,
<input type='checkbox' name='list[0]' id='product' value='Product'></input>
<input type='checkbox' name='list[1]' id='product' value='Product'></input>
Here I am setting my variable with the POST of the checkboxes.
$checkboxes = $_POST['list'];
$_SESSION['list'] = $checkboxes;
And use foreach so we know that in $_SESSION['list'] we have only checked ones!
foreach ($_SESSION['list'] as $key => $value)
{
echo '<input type="checkbox" name="list['$key']" value="'.$value.'" checked="checked >';
}
Related
I have tried searched, but I am missing somthing or not phrasing myself correctly I guess.
php, sql and Codeigniter.
What I have is a list of reports in a database.
Theese have a column that states if they are "active" 1 or 0.
What I want to do is add a checkbox on the list that prints the rows in my view as that I can select and then when I have selected the rows I want to be able to press a button and by sending form data change the selected rows in the database to a 1 or 0.
example of how I am thinking of presenting it:
echo "<form action='' method='POST'><table>";
foreach($reports->result() as $row)
{
echo "<tr>";
echo "<td><input type="checkbox" name=""></td>;
echo "<td>".$row->name."</td>";
echo "<td>".$row->time."</td>";
echo "<td>".$row->spot."</td>";
echo "<td>".$row->priority."</td>";
echo "</tr>";
}
echo "<tr><td colspan='5'><input type='submit'></td></tr>";
echo "</table></form>";
What would be the best is if I could get the checkboxes I select into a array for example that would be lovely.
I have tried with array_push, but I could not find a way to select only the ones I selected to send. Is there some way to do this that I dont know of?
I have found a way to make the selected checkboxes into an array by adding id[] to all checkboxes name like this this:
form_checkbox('id[]',$row->id, FALSE);
I loop out alot of these and might get this for example:
<input type="checkbox" name="id[]" value="12340">
<input type="checkbox" name="id[]" value="12353">
<input type="checkbox" name="id[]" value="12367">
<input type="checkbox" name="id[]" value="12389">
<input type="checkbox" name="id[]" value="12391">
Then in my controller I added this where my form takes action to and loops through the ones that I have checked and take action on them.
$arr = array();
if(!empty($_POST['id'])) {
foreach($_POST['id'] as $selected) {
$this->db->where('id', $selected);
$this->db->update('rapporter' , $data);
}
}else{}
This is working great for me. Now I only update the rows I have checked!
If someone have a more efficient way of doing this I am all ears!
This is my form and categories comes from the database with while loop.
I want to insert the checked inputs only in to database.
How can i detect which checkbox is selected ?
<form action="account.php" method="POST">
<ul class="account-info">
<li>Category1 : <input type="checkbox" value="val1" name="cat1"></li>
<li>Category2 : <input type="checkbox" value="val2" name="cat1"></li>
<li>Category3 : <input type="checkbox" value="val3" name="cat1"></li>
<!-- while continues -->
<li>Category100 : <input type="checkbox" value="val100" name="cat1"></li>
</ul>
<input type="submit" value="submit" />
</form>
In account.php only the check boxes will be posted. As you've named them all the same though, only 1 will be posted, the last checkbox. If you want them to have the same name and come through as an array you need to add [] after the name, like this:
<input type="checkbox" value="val100" name="cat1[]">
Then in your account.php where they are submitted you can do this:
foreach($_POST['cat1'] as $val)
{
echo "$val<br>";
}
That will echo out the values of all the checked boxes.
First off, you have your options set up as if they were radio buttons all having the same name turning into 1 value out of the 3 possible. If you want them to be conventional checkboxes you have to give them separate names.
in your PHP you would check if the array_key_exists for the checkbox name in question, this is how I usually translate the checkbox fields in a form to be easier to use:
<?php
$checked = array(
'cat1' => array_key_exists('cat1', $_POST),
'cat2' => array_key_exists('cat2', $_POST),
'cat3' => array_key_exists('cat3', $_POST)
);
print_r($checked);
exit;
?>
change the name="cat1" to name="cat[1][]"
in your account.php page
foreach($_POST['cat'] as $category){
foreach($category as $value){
echo $value;
}
}
You can check it like that:
This code will check which categories are activated
assuming that your database returns categories from 0 to x
$i = 0;
while(true){
if ($_POST["cat".$i."]) {
//category activated
}
else {
//no category found -> loop ends
break;
}
$i++;
}
In my html I have this,
<tr>
<td class="grid_cell" width="5%">
<input type="checkbox" name="workspace_trees_rpt_target" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
</td>
</tr>
this are inside a loop so what will be displayed is a lot of checkboxes with different values. And in my script I have this,
if(confirm("Delete cannot be undone, click OK button to proceed.")) {
document.forms[0].method="POST";
document.forms[0].action="delete.php";
document.forms[0].submit();
}
after selecting two of the checkboxes and click the OK button, I then go to delete.php to print_r my post data. But when the result of print_r is displayed it only showed one value for the checked checkboxes, but I was expecting two of them. How can I make it such that when I check multiple checkboxes, the post data for the checkboxes will be an array of values of the checked checkboxes?
you must set the name of the inputs as array:
name="workspace_trees_rpt_target[]"
You have given the same name to all of your checkboxes. So only one value can be returned.
Change to
<input type="checkbox" name="workspace_trees_rpt_target[]" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
Then in your PHP code you will get and array of $_POST['workspace_trees_rpt_target'][]
So process it like this:
if ( isset( $_POST['workspace_trees_rpt_target'] ) ) {
// at least one checkbox has been ticked
foreach ( $_POST['workspace_trees_rpt_target'] as $checkboxe ) {
// do whatever $checkbox it will be set to the value="" that you set earlier
}
}
I am using the code below in my php file to get the values from a multiple checkbox.
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
update_comment_meta($check, 'consider', 1);
}
}
The problem is that this code is apparently putting in the array $_POST['check_list'] only the checked values.
My need is to perfom the function update_comment_meta also on uncheked values, by putting '0' as the third parameter instead of '1'.
For more details, I give the code generating the HTML form:
<form action="" id="primaryPostForm" method="POST">
<?php
$defaults = array(
'post_id' => $current_post);
$com= get_comments( $defaults );
foreach ($com as $co) {
if(get_comment_meta($co->comment_ID, 'consider', true)==1) {
?><input type="checkbox" name="check_list[]" value="<?php echo $co->comment_ID; ?>" checked="checked">
<?php }
else {
?><input type="checkbox" name="check_list[]" value="<?php echo $co->comment_ID; ?>" >
<?php
}}
</form>
Your usual help is always appreciated.
sending unchecked value to post is somewhat not that easy.Better solution is that you name checkbox in a way using which you can easily iterate over them in post page.
Use hidden input along with checkbox.Checkbox prioritize over hidden input.
<form>
<input type='hidden' value='0' name='check_box_con'>
<input type='checkbox' value='1' name='check_box_con'>
</form>
Now after submit, as both have same name , check_box_con will show hidden field value if unchecked , else will override and show original.
For more see
Post the checkboxes that are unchecked
Here is the solution I used ( based on PeeHaa comment):
if(!empty($_POST['check_list'])) {
foreach ($com as $co) {
if (in_array($co->comment_ID,$_POST['check_list']))
update_comment_meta($co->comment_ID, 'consider', 1);
else
update_comment_meta($co->comment_ID, 'consider', 0);
}
}
In fact POST variable works like this with checkboxes, so the simple way is to use server side language to know what are values not sent via POST.
Thank you for your time.
I am simply trying to pass my checkbox values through a session variable for use if the user goes back at some point. After going to my first page and POSTING, I want these selections stored in a SESSION variable. I've had no luck so far in figuring this one out. My code is below.
Here is my html of my checkboxes. I have about 15 checkboxes with the same name as below. I take all those checkboxes and break them down in another script for insertion into a database.
<input type='checkbox' name='list[]' id='product' value='Product'></input>
Here I am setting my variable with the POST of the checkboxes.
$checkboxes = $_POST['list'];
$_SESSION['list'] = $checkboxes;
How can I pass these checkbox selections into and out of a session variable for selecting elements on a previous page?
Change:
<input type='checkbox' name='list[]' id='product' value='Product'></input>
To
<input type='checkbox' name='list[0]' id='product' value='Product'></input>
<input type='checkbox' name='list[1]' id='product' value='Product'></input>
etc.
And use foreach:
Okay so. We know that in $_SESSION['list'] we have only checked ones!
foreach ($_SESSION['list'] as $key => $value)
{
echo '<input type="checkbox" name="list['$key']" value="'.$value.'" checked="checked >';
}
Just thought I share my code / solution for storing the html form checkbox value in a session. My search did not get me to cover all scenarios that I needed.
My scenario includes that I pass a default state for checked or not checked.
HTML Form Code:
<input type="hidden" name="product[]" value = 0>
<input name="product[]" type="checkbox" value = 1 <?php echo (($productvalue == 1) ? 'checked' : '')?>>
PHP Form processing code:
$productvalue = ((isset($_POST['product'])) ? array_sum((array)$_POST['product']) : ((isset($_SESSION['productsession'])) ? $_SESSION['productsession'] : 0));
$_SESSION['productsession'] = $productvalue;