set_checkbox for multiple select checkbox - php

how to set checkbox value if form validation fails in codeigniter. I have multiple select checkbox whose values populated from other dropdown selection. I want if form validation fails it should give me selected values. In my case it disappers. Any help will be appreciated.
my code:
<tbody class="location_id">
<?
$checked='';
if(isset($data1['loc_id']))
{
if($data1['loc_id']!='')
{
$checked='checked';
}
}
if(isset($data1['loc_id'])){
?>
<tr><td><input type="checkbox" name="loc_id[]" value="<?php if(isset($data1['loc_id'])){ echo $data1['loc_id'];} ?>" <?php echo set_checkbox('loc_id[]', $data1['loc_id']); ?> <?php echo $checked;?>></td><td><?php if(isset($data1['loc_id'])){echo get_name('location_tbl','loc_id',$data1['loc_id'],'loc_name');}?></td></tr>
<?php
}
?>
</tbody>
hope you understand.
In my controller I have set
$this->form_validation->set_rules('loc_id', '', 'trim|xss_clean');
sorry for grammar.

<input type="checkbox" style= "position: initial;" name="hobby[]" value="Sport"<?php if(strpos($hobby, 'Sport') !== false) echo "checked='checked'"; ?> >Sport
model:-
$hobby =implode(",", $this->input->post('hobby'));

Related

How to validate set of checkboxes/ array of checkboxes?

after looking for ages on the internet i thought id ask here for help. I have a simple checkbox table here in which i click the checkbox and the result is posted back in PHP, but when i dont click any checkbox and submit it i get the following error.
Notice: Undefined index: idlights in C:\xampp\htdocs\lt4\checkbox\index.php on line 44
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\lt4\checkbox\index.php on line 44
How can i validate this table so that if nothing is checked it just says something like "Please check a checkbox" or something like this? i have tried
if (empty($camid)
{
echo "<p style='color: red'>Please check a checkbox </p>";
}
But that didn't work, Here is my code, Any help on this would be very much appreciated
<?php
include('conn.php');
$query=mysqli_query($conn,"select * from `camera`");
while($row=mysqli_fetch_array($query)){
?>
<tr>
<td><input type="checkbox" value="<?php echo $row['camid']; ?>" name="camid[]"></td>
<td><?php echo $row['cameras']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<br>
<input type="submit" name="submit" value="Submit">
</form>
</div>
<div>
<h2>You Booked:</h2>
<?php
if (isset($_POST['submit'])){
foreach ($_POST['camid'] as $id):
$sq=mysqli_query($conn,"select * from `camera` where camid='$id'");
$srow=mysqli_fetch_array($sq);
echo $srow['cameras']. "<br>";
endforeach;
}
?>
```
Do you use any framework? Try to add required in you checkbox
<input type="checkbox" value="<?php echo $row['camid']; ?>" name="camid[]" required>
Just use an additional if statement like below.
<?php
if (isset($_POST['submit'])){
if(isset($_POST['camid'])){
foreach ($_POST['camid'] as $id):
$sq=mysqli_query($conn,"select * from `camera` where camid='$id'");
$srow=mysqli_fetch_array($sq);
echo $srow['cameras']. "<br>";
endforeach;
}
}
?>

How to keep checkbox array checked after form submission

I have the following codes to show dynamic checkboxes.
while($result = mysqli_fetch_array($query)){
$oaName = $result['oaName'];
echo '<input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="'.$oaName.'" style="float:left;"'; ?> <?php if(isset($_POST['checkBoxArray'])) echo "checked='checked'"; ?> <?php echo '>'; ?>
}
I want to retain status of only those checkboxes that I checked as checked after form submission. But with the above codes, all checkboxes are showing checked after form submission. Does anyone know what am i doing wrong here?
Edit 1
checkBoxArray[] are checkboxes names which are getting from database
After a few trials, I am able to get it done by the following way. The key here is the use of in_array
while($result = mysqli_fetch_array($query)){
$oaName = $result['oaName'];
echo '<div class = "checkbox-group" required ><input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="'.$oaName.'" style="float:left;"'; ?> <?php if(in_array($oaName,$_POST['checkBoxArray'])) echo "checked='checked'"; ?> <?php echo '></div>
}

How to select checkbox in database mysqli table in PHP

**How to select checkbox in database mysqli
what should i write in checkbox value
please kindly help
i also implode function which convert array to string
did not find values for checkbox values to print. .....
Also check the output on PICTURE...
**
OUTPUT PIC
<?php
while($fetch=mysqli_fetch_array($select))
{
?>
<tr>
<td><?php echo $fetch["tableno"]?></td>
<td><?php echo $fetch["customerid"]?></td>
<td><?php echo $fetch["item"]?></td>
<td><?php echo $fetch["money"]?></td>
<td><button>Delete</button></td>
<td><a href="update.php?tableno=<?php echo $fetch["tableno"]; ?>"/><button>Update</button></td>
<td><form method="POST">
<input type="checkbox" name="chk[]" value="???">
<input type="submit" name="sub1" value="Button for Checkbox">
</td> </form>
</tr>
<?php
}?>
</table>
<p style="font-size:30px">The Customer has TO Selected these Items from Menu List
and therefore submitted to<br> Kitchener to Make the Food and
then waiter will serve the Food to Customer.</p>
</body>
</html>
<?php
if(isset($_POST['sub1']))
{
$chk=implode(',', $_POST['chk']);
echo "<br>Customer has selected the following checkbox Item to be Prepared and Eat the Food:<br/>";
echo "<br/>".$chk."<br><br/><br/><br/><br/>";
}
?>
Try this:
In you form, method to insert multiple checkboxes
<form method="POST" action="test2.php">
<input type="checkbox" name="chk[]" value="InputText1">InputText1<br>
<input type="checkbox" name="chk[]" value="InputText2">InputText2<br>
<input type="submit" name="sub1">
</form>
if(isset($_POST['sub1'])) // check the submitted form and print the values
{
echo "You selected the following checkbox:<br/>";
foreach ($_POST['chk'] as $value) {
echo $value."<br>";
}
One method is the value of a checkbox can be concatenated with all data and after submitting, check for selected check boxes with for each loop and print all the selected rows. Like below
<input type="checkbox" name="chk[]" value="<?php echo "<td>".$fetch["tableno"]."</td><td>".$fetch["customerid"]."</td><td>".$fetch["item"]."</td><td>".$fetch["money"]."</td>"; ?>">
And after posting use this :
if(isset($_POST['sub1'])) // check the submitted form and print the values
{
echo "Below Customers have placed orders:<br/>";
echo "<table>";
foreach ($_POST['chk'] as $value) {
echo "<tr>".$value."</tr>";
}
echo "</table>";
}

Retrieve POST checkbox data from form if they exist

Hello knowledgeable people. I am having trouble retrieving checkbox data from form. I have a site in which user can add checkboxes themselves, so I am writing them out like this:
<table style="padding:10px;">
<?php
$query_boolean = $DB->prepare("SELECT * FROM moduls WHERE type='boolean'") or die(mysql_error());
$query_boolean->execute();
while (($row = $query_boolean->fetch()) != false)
{
?>
<tr>
<td>
<?php echo $row->name ?>:
</td>
<td>
<?php
$s = "";
$s .= sprintf('<input type="checkbox" class="textbox" name="boolean_%s" value="yes">%s', $row->id, Yes);
$s .= sprintf('<input type="checkbox" class="textbox" name="boolean_%s" value="no">%s', $row->id, No);
echo $s;
?>
</td>
</tr>
<?php
}
?>
</table>
Now I have an advanced search in which I have to chech through every checkbox to see what has been selected (ether none, Yes, No, or both). How can I get the info from every checkbox in variables? Thank you so much!
To get POST data from checkboxes they must have attribute
checked="checked"
EDIT:
If you have 2 checkbox as this..
<input type="checkbox" checked="checked" class="textbox" name="boolean_yes" value="yes">
<input type="checkbox" class="textbox" name="boolean_no" value="no">
When you submit your form the checkbox with attribute checked will be sent as POST and the one without checked attribute will not be sent..
if(isset($_POST['search'])) {
$all_checked = array();
foreach($_POST as $key=>$value){
if(strpos($key, "boolean_") > -1){
$all_checked[$key] = $value;
}
}
var_dump($all_checked);
}
This way you will get inside $all_checked array all marked boxes.. All others checboxes are not marked!
if you want to get checkbox value then use checkbox name as array
<input type="checkbox" name="email1[]" value="">
an get it on another page by
<?php
$var2 = $_POST['email1'];
$v=implode(",",$var2);
echo $v;
?>
try it

Looping through an array of checkbox

I have a form with rows which are populated from a table. Each row has a "checkbox" which the user can check or not.
When the form is submitted I want to be able to read which checkbox have been selected and insert the result in to a data table.
My code so far
FORM:
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table
<?php do { ?>
<tr>
<td>input type="text" name="InspectRoomNo" value="<?php print $row_InspectItems['AuditItemNo']; ?>"></td>
<td>php echo $row_InspectItems['AuditItem']; ?>td>
<td>input name="check[]" type="checkbox" ></td>
</tr>
<?php } while ($row_InspectItems = mysql_fetch_assoc($InspectItems)); ?>
<input type="submit" value="Insert record">
</table>
The insert: fetchs $Items from table
while($row = mysql_fetch_assoc($Items))
{
$array[] = $row['AuditItem'];
}
foreach($array as $id) {
$AuditItemID = mysql_real_escape_string($id);
if(isset($_POST['check'])){
$Checked = mysql_real_escape_string($_POST['check'][$row]);
}
}
The problem I am having is the returned values for all the checkbox is true, even if a checkbox was not selected.
Can anyone help me sort this issue.
Many thanks.
Do it like this:
if(!empty($_POST['check'])) {
foreach($_POST['check'] as $check) {
echo $check;
}
}
You should put the item id inside the checkbox name:
<td><input name="check[<?= $row_InspectItems['AuditItem']; ?>]" type="checkbox" /></td>
Then, you can simply iterate over it:
foreach ($_POST['check'] as $id => $value) {
// do stuff with your database
}
I'm assuming than whomever runs this script is trusted, because it would be easy to forge the list of ids; make sure the current user has permissions to update those records.
What is happening, is that only selected checkboxes get sent to the server, so you will see that your $_POST['check'] array (this is an array!) is smaller than the number of items you have displayed on the screen.
You should add your ID's so that you know what checkboxes got checked and adapt your php processing code to handle an array instead of a single value.
You are also overwriting your InspectRoomNo every row, so you should use an array there as well.
The form side would look something like:
<td><input type="text" name="InspectRoomNo[<?php echo row_InspectItems['AuditItemNo']; ?>]" value="<?php print row_InspectItems['AuditItemNo']; ?>"></td>
<td><?php echo $row_InspectItems['AuditItem']; ?></td>
<td><input name="check[<?php echo row_InspectItems['AuditItemNo']; ?>]" type="checkbox" ></td>

Categories