In codeigniter, how will i know which checkboxes are checked? - php

I have a dynamic number of checkbox in my view,like this:
<?php foreach($documents as $row): ?>
<input type="checkbox" name="options[]" value="<?php echo $row->docu_title?>"><?php echo $row->docu_title?><?php endforeach; ?>
And I set the rule for this group of checkboxes to be required in my controller:
$this->form_validation->set_rules('options[]','options', 'required');
How will i know which checkboxes are checked? so whenever there are errors on the other fields i can still show the user the checkboxes that has been checked already. like this:
<input style="" type="text" class="form-control" name="ClientName" id="ClientName" value="<?php echo set_value('ClientName'); ?>">

You could use form helper 's set_checkbox() function.
This permits you to display a checkbox in the state it was submitted. The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE). Example:
<input style="" type="checkbox" class="form-control" name="ClientName" id="ClientName"
value="<?php echo set_value('ClientName'); ?>" <?php echo set_checkbox('ClientName', '1'); ?> />
For reference visit CodeIgniter User Guide Version 2.2.0

should be something like this
<?php foreach($documents as $row): ?>
<input type="checkbox" name="option[]" value="<?php echo $row->docu_title?>" <?php echo set_checkbox('option[]', $row->docu_title); ?>>
<?php echo $row->docu_title?>
<?php endforeach; ?>

Related

maintain checkbox which were checked after submit

I know there's "PHP keep checkbox checked after submitting form" on here, but that thread does not solve my problem, because I have multiple checkbox, what I need is when you check a checkbox, this stay checked after submit.
At the moment with this code nothing happens, I tried another way but when I check "id7" checkbox, all the checkbox get checked.
I have to know which checkbox was checked by the id that I give it, but I do not know how.
while ($fila = mysql_fetch_array($rs)) {
echo utf8_encode("
<tr>
<td>
".$fila['title']."
</td>
");?>
<td>
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist[]']) && is_array($_POST['checklist[]']) && in_array('$fila', $_POST['checklist[]'])) echo 'checked="checked"'; ?> />
</td>
<?php
}
}
?>
First, the value of checkbox is $fila['id'] so when you are checking, use $fila['id'] instead of $fila. Also, when PHP receive array input fields with [] in their names the [] will be removed so that the correct POST variable is $_POST['checklist'].
Try changing this line:
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist[]']) && is_array($_POST['checklist[]']) && in_array('$fila', $_POST['checklist[]'])) echo 'checked="checked"'; ?> />
to
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist']) && is_array($_POST['checklist']) && in_array($fila['id'], $_POST['checklist'])) echo 'checked="checked"'; ?> />
"name" is like a variable name - by using checklist[] you're defining an array as the variable.
Why not just name each checkbox properly? When the form is submitted, use the contents of $_POST to set each variable into the user's $_SESSION.
If the page is refreshed, use the values from $_SESSION to determine if the checkbox should be ticked. Something like (untested):
<input type="checkbox" name="vehicle" value="Bike"
<?php if (isset($_SESSION['bike_checked']) echo 'checked'; ?>> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car"
<?php if (isset($_SESSION['car_checked']) echo 'checked'; ?>> I have a car<br>
<input type="submit" value="Submit">

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 Show if hides update fields, but they get overwritten with an empty string

This is one of those ones that probably falls between PHP and SQL stuff.
Basically I have a page where some update fields are displayed depending on who is logged, for example:
<?php if ($row_Users['UserID']=="101"){ ?>
<input <?php if (!(strcmp($row_lodges['101_finalist'],"Yes"))) {echo "checked=\"checked\"";} ?> type="checkbox" name="101_finalist" value="Yes"/>
<input type="text" class="rankfield" name="101_rank" value="<?php echo($row_lodges['101_rank']); ?>" />
<?php } ?>
<?php if ($row_Users['UserID']=="102"){ ?>
<input <?php if (!(strcmp($row_lodges['102_finalist'],"Yes"))) {echo "checked=\"checked\"";} ?> type="checkbox" name="102_finalist" value="Yes"/>
<input type="text" class="rankfield" name="102_rank" value="<?php echo($row_lodges['102_rank']); ?>" />
<?php } ?>
The issue I have is that if User101 is logged in and updates fields 101_finalist and 101_rank, it overwrites 102_finalist and 102_rank with an empty string.
Is it possible to prevent each user from seeing the other fields for other users, and prevent the existing values for those other users not be overwritten?
Hope that makes sense!
Thank you.
You'll want to do something like this. It's basically what #taco is saying:
<?php
// Set's the default user logged in
$_def = ($row_Users['UserID'] == "101")? '101': '102'; ?>
<input <?php if (!(strcmp($row_lodges[$_def.'_finalist'],"Yes"))) { ?>checked="checked"<?php } ?> type="checkbox" name="<?php echo $_def; ?>_finalist" value="Yes"/>
<input type="text" class="rankfield" name="<?php echo $_def; ?>_rank" value="<?php echo ($_def == '101')? $row_lodges['101_rank']:$row_lodges['JA_rank']; ?>" />
You can do something like
<?php
$userid = $row_Users['UserID'];
$finalist = $userid."_finalist";
$rank = $userid."_rank";
?>
<input <?php if (!(strcmp($row_lodges[$finalist],"Yes"))) {echo "checked=\"checked\"";} ?> type="checkbox" name="101_finalist" value="Yes"/>
<input type="text" class="rankfield" name="101_rank" value="<?php echo($row_lodges[$rank]); ?>" />

How to use GET to retrieve check box selected value in php

I am using Mysql PDO query to fetch the required result and it is saved in and array $newfamily. Now with the help of this array I am implementing check-box with the given code-
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<input type='checkbox'<?php echo $name;?>"><?php echo $name;?></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
Now in specific_family.php how can I retrieve al the selected check-box values in an array ?
Also when I use back button of browser I can see previously selected values as ticked. How can I remove this ?
Please guide.
The checkbox should have:
A label
The checkbox needs:
A name attribute
A value attribute
It must not have:
An end tag (unless you are using XHTML)
So:
<label>
<input type="checkbox"
name="new_families[]"
value="<?php echo htmlspecialchars($name); ?>">
<?php echo htmlspecialchars($name); ?>
</label>
The values of all the checked checkboxes will then appear in the array $_GET['new_families'] when the form is submitted.
If you add the name attribute to your input thus:
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<label for="<?php echo $name?>"><?php echo $name?></label>
<input type='checkbox' name="<?php echo $name;?>" id="<?php echo $name;?>"></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
then your checkboxes will show up by name in your $_GET array.
Hope that helps :)

how to pass value of checked checkbox in php

Hi i am generating checkbox list by using a for each loop, I want to pass the value of checked checkbox into database.
foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list" value="1" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
when i checked the checkbox i want to pass the label name into the database can someone help.
Check this code. You need to take array as checkbox name i.e. list[]. Once form is submitted you can use it's value to insert in to database.
<?
if(isset($_POST['submit'])) // once form is submitted build your logic
{
$list = $_POST['list'];
foreach($list as $value)
{
echo "<br />Checked: $value";
// use $value to insert into database
$sql = "insert into...";
mysql_query($sql);
}
}
?>
<form name="frm" method="post">
<table>
<? foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list[]" value="<?php echo $list->nList ?>" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
<tr>
<td><input name="submit" value="Submit" type="submit" /></td></tr></table>
</form>
Ok, you've got a few problems there..
Firstly, please avoid opening php tags and closing them constantly. Just echo back the HTML. Going in and out of php so often will cause performance issues.
So, first off, you need to set up a form on here, your form needs to point to a second page (or self-process using an isset function, but let's go with the former solution for clarity). The second page is what's going to dump things into the DB for you based on what the user ticked.
So, here's the HTML for your form;
<form action="database.php" method="POST">
<div>
<label for="checkbox1">This is the first option:</label>
<input type="checkbox" id="checkbox1" name="checkbox1" />
</div>
<div>
<label for="checkbox2">This is the second option:</label>
<input type="checkbox" id="checkbox2" name="checkbox2" />
</div>
<div>
<input type="submit" />
</div>
</form>
Again, I'd recommend echo'ing this back in your foreach loop - a short function could accomplish this for you.
So, once someone hits the "Submit" button, they'll be taken to your second page (database.php as we've called it), and their options will be stored in the POST array. For good practice, always use POST rather than GET when dealing with DB entries.
So on this page, we'd check what checkboxes were selected, and then update the db as required;
if (isset($_POST['checkbox1']))
{
// user ticked checkbox1
$sql = "UPDATE table
SET 'checkbox1' = 1
WHERE 'user' = 'username';"
mysqli_query($sql);
}
Note that's just a rough solution - you haven't told me exactly how you're doing this, so I'm assuming your DB is tied to user's votes etc. You can also update checkbox1 by incrementing the value.. etc etc.
Hope this helps.
Eoghan
In the name you should use [] (because it's an array). So in this case it'd be name="list[]". Then when you submit it (either post or get) you just get the checked data on the list[] array. I'm not sure if I understood your question. If I didn't; please share a little more information.
I think this is what you're looking for:
foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list[<?php echo $list->nList ?>]" value="1" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
Then in PHP, you would do foreach($_POST['list'] as $listval) to loop over it again. Substitute $_POST for $_GET if you are using GET in your form.
Edit: I just noticed where you did echo $list->nList that this variable is either not going to change in your loop or it's a typo. Assuming it's a typo, it should be echo $lst->nList instead. If it's not a typo, then you want to just remove it and have name="list[]" in your HTML

Categories