How to validate set of checkboxes/ array of checkboxes? - php

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;
}
}
?>

Related

Using HTML buttons in PHP loops

I have a html table displayed using foreach loop in php. And I do even have buttons to be clicked in multiple rows. And the code goes like this:
<form method="post">
<table>
<tr> <th> Item </th> <th>Click to select</th></tr>
<?php
$query="select items from items_table";
$result=$con->query($query); //$con is connection variable already initialized
$row=mysqli_fetch_assoc($result);
foreach ($row as $index) //loop
{
?>
<tr>
<td><?php echo $index['items']; ?> </td>
<td><input type="button" value="select"> </td> //button here
</tr>
<?php } ?>
</table>
</form>
Now how can I get to know which button was pressed?
I have read some web pages, which says we need to be using AJAX, and I'm a newbie with no knowledge of how it works.. Please help me out!
I tried to have button inside a loop and expected that the buttons works correctly directly. But it gives wrong output.
If I got what u want I'd say you should handle your button or input
Some ways available
$index['items']
Is Not Correct where u used
<input value=<?php echo $row['id'] ?>
<input name=foo[<?php echo $row['id'] ?>] value=<?php echo $row['id'] ?> >
<input name='foo[]' value=<?php echo $row['id'] ?> >
Then handle them :
<?php
foreach($_REQUEST['foo'] as $name =>
$value){
echo $name ."posted and its value
is:". $value;
}
?>
OR
<?php
echo 'value of foo[1] Is '.$_REQUEST['foo[1]'] ;
?>
You can use FOR EXAMPLE $row['name'] Or any field name u have in your table intead of $row['id'] that I gave

How to return values entered in a dynamic table textboxes if there is an error on form submit

Coud somebody please help me. I have a textbox name "txtremise" found in a dynamic table where user enter a discount %. I am trying to return the values entered in each textbox if there is an error during the form submit.
Lets say, there are 3 rows with 3 textboxes in the table. A user enter 5, 10, 15 respectively in each textbox. Let's say there was an error upon submitting the form, i need the values 5, 10, 15 back in their respective textboxes. Can somebody please assist?
Here is how i generate the textboxes:
<?php
while($iArticles < count($listeArticlePourUnDossier))
{
?>
<tr>
<td><?php echo ($listeArticlePourUnDossier[$iArticles]['ARTICLE_NOM']); ?></td>
<td>
<input type="text" name="txtremise[]" id="txtremise_<?php echo $iArticles; ?>" class="remise" value="<?php echo $_POST['txtremise'];" />
</td>
</tr>
<?php
$iArticles++;
}
?>
Thank you for helping.
<?php
$i=0;
while($iArticles < count($listeArticlePourUnDossier))
{
?>
<tr>
<td><?php echo ($listeArticlePourUnDossier[$iArticles]['ARTICLE_NOM']); ?></td>
<td>
<input type="text" value="<?php if(isset($_POST['txtremise'][$i])){ echo $_POST['txtremise_'][$iArticles];}?>" name="txtremise[]" id="txtremise_<?php echo $iArticles; ?>" class="remise" value="<?php echo $_POST['txtremise'];" />
</td>
</tr>
<?php
$i++;
$iArticles++;
}
?>
You still have that value in either $_POST or $_GET so check if the key is isset then echo the value. So all you need is to add value in your input

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>";
}

set_checkbox for multiple select checkbox

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'));

How to get the name/id of multiple buttons

I have gotten the id numbers of users from my database, and I want to make a button for each user. My code makes a table that shows all the IDs and creates a button for each one. I'm having trouble figuring out how to get the name of those buttons for use in other code. The error I am getting is "undefined variable" (in the 3rd line), which I am most likely getting because I am going at getting the button names wrong.
Basically, the $_POST in the third line is wrong (among perhaps other things). My question is how would one get the name (or id?) of the buttons I have made: how should I fix the $_POST or should I use something else entirely?
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST[$n])) header("location:" . $n . ".php");
}
?>
<div id="mod_user">
<table id='mod_table'>
<th class='ttop'>#</th>
<th class='ttop'>Page</th>
<?php
$result = $db->prepare("SELECT * FROM User");
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
$n=$row["UserID"];
?>
<form action="" method="post">
<tr>
<td class='tben'><?php echo $n; ?></td>
<td class='tben'><button type='submit' name=<?php echo $n; ?> >Go here</button></td>
<br />
</tr>
</form>
<?php
} ?>
</table>
</div>
You can try like this:
<td class='tben'><button type='submit' name="usernames[<?php echo $n ?>]" >Go here</button></td>
So you can get button name from $_POST["usernames"] array as below
foreach($_POST["usernames"] as $username => $btn_value)
echo "$username => $btn_name";

Categories