Checkbox in a while loop - php

I am trying to create a function where users can check a message to delete. Obviously if more than one message is checked, they will all be deleted.
I have the following code
<form action="process.php" method="post">
<input type="hidden" name="deleteMessages" />
<?php
while($row=mysql_fetch_assoc($q))
{
if($row['to_viewed'] == 0)
{
?>
<tr>
<td><input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
</td>
<td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></b></td>
<td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></b></td>
<td><b><?php echo $row['created']; ?></b></td>
</tr>
<?php
}
else if($row['to_viewed'] == 1)
{
?>
<tr>
<td><input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
</td>
<td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></td>
<td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></td>
<td><?php echo $row['created']; ?></td>
</tr>
<?php
}
}
?>
<input type="submit" value="Delete All" />
</form>
I want to pass the checkbox through and if the value is 1, process it and delete it.
But how would I achieve this with mulitple messages no matter if there is one message or ten?
Thanks

In your form put:
<input type="checkbox" name="check_box_delete[]" value="<?php echo $row['id']; ?>" />
Then to process:
if(isset($_POST['check_box_delete']))
{
foreach($_POST['check_box_delete'] as $id)
{
// Delete $id
}
}

You can let php store mutliple values in an array by naming the input fields with an array designator suffix [].
For example, all checkboxes named checkbox[] will then be stored in $_POST['checkbox'][].
Note that this may not be applicable to checkboxes as their $_POST value only exists if they were checked.

<input type="checkbox"
name="mid_to_delete[]"
value="some message id"
id="mid_to_delete_some_message_id">
<label for="mid_to_delete_some_message_id">Delete "Some subject"</label>
and then
<?php
foreach ($_POST['mid_to_delete'] as $mid) {
delete_some_message_id($mid);
}
?>
Since only successful controls are submitted, and unchecked checkboxes are not successful, all the values you get will be ones that have been selected for deletion. (Obviously you still need to perform auth/authz to make sure that the messages being deleted are ones the user is allowed to delete)

Related

How can i keep data array from checkbox when datalist has many page

My problem is array can keep data only present page.
If user selected data page 1 & page 4 and push submit when he uses page 4
My array will keep data from page 4.
I want to keep data from the page that the user selects every page.
datalist table page
<td>
<center>
<input type="hidden" name="txtID[<?php echo $ven2["id"]; ?>]" id="txtID" value="<?php echo $ven2["id"]; ?>"><?php echo $ven2["id"]; ?>
</center>
</td>
<td>
<center>
<input type="hidden" name="txtACC[<?php echo $ven2["id"]; ?>]" id="txtACC" value="<?php echo $ven2["acc_name"]; ?>"><?php echo $ven2["acc_name"]; ?>
</center>
</td>
<td>
<input type="hidden" name="txtITM[<?php echo $ven2["id"]; ?>]" id="txtITM" value="<?php echo $ven2["item_name"]; ?>"><?php echo $ven2["item_name"]; ?>
</td>
<td>
<center>
<input type="checkbox" name="dataset[<?php echo $ven2["id"]; ?>]" id="dataset" value="<?php echo $i++; ?>">
</center>
</td> 
show data page
foreach($_POST["dataset"] as $key=>$value)
{
if($_POST["dataset"][$key] != "")
{
$DATA_ID = $_POST["dataset"][$key];
$DATA_ACC = $_POST["txtACC"][$key];
$DATA_ITM = $_POST["txtITM"][$key];
print "ACCOUNTCODE = ".$DATA_ACC."<br>";
print "ITEM = ".$DATA_ITM."<br>";
print "========================== <p>";
}
}
My web template
<script type="text/javascript" src="jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.dataTables.min.css">

GET single loop form data in hyperlink php

I am trying to update quantity as i fill the quantity field and click on update button. It works but it shows the form fields of each item in the loop and works for the last entry update only. Something like this.
/EcommerceClient/index.php?page=cart&action=update&id=3&name=%09%0D%0ACool+T-shirt&color=blue&size=XL&quantity=4&id=4&name=HBD+T-Shirt&color=yellow&size=XL&quantity=900
In the above link it should only get the information associated with id=3 only because i tried to update the quantity of id=3.
Here is my code. Any suggestions or help will be highly appreciated.
Code
if(isset($_GET['action']) && $_GET['action']=="update"){
$id= intval($_GET['id']);
$size = $_GET['size'];
$color = $_GET['color'];
$qty = $_GET['quantity'];
$index = $id." ".$color. " ".$size;
if( isset($_SESSION['cart'][$index]) && isset($_SESSION['cart'][$index]['color']) && $_SESSION['cart'][$index]['color'] == $color && isset($_SESSION['cart'][$index]['size']) && $_SESSION['cart'][$index]['size'] == $size){
$_SESSION['cart'][$index]['quantity']=$qty;
print_r($_SESSION['cart'][$index]);//It just shows me the last item array.
}
}
?>
<form class="product" method="get" action="index.php">
<table>
<input type="hidden" name="page" value="cart">
<input type="hidden" name="action" value="update">
<?php
if(isset($_SESSION['cart'])){
foreach($_SESSION['cart'] as $id => $value){
?>
<tr>
<input type="hidden" name="id" value="<?php echo $value['id'] ?>">
<input type="hidden" name="name" value="<?php echo $value['name'] ?>">
<input type="hidden" name="color" value="<?php echo $value['color'] ?>">
<input type="hidden" name="size" value="<?php echo $value['size'] ?>">
<td><?php echo $value['id'] ?></td>
<td><?php echo $value['name']?></td>
<td><?php echo $value['price']?> </td>
<td><?php echo $value['quantity']?><input type="text" name="quantity" value="<?php echo $value['quantity'] ?>"></td>
<td><?php echo $value['color'];?> </td>
<td><?php echo $value['size']; ?></td>
<td><?php echo "$" .$value['price']*$value['quantity']. ".00"; ?>
</tr>
<?php
}
}
?>
<tr><td><button type="submit">Update</button></td></tr>
</table>
</form>
You can update the quantity by not posting or getting all the cart product. Either you can use simple JS to update the field value or use SESSION variable array to update data as #Marc B mentioned.

How to capture different values for the items belong to the same group

I have a table to display Addon items from database.
The user must check the checkbox for the first two items namely: gps and babyseat.
So the deposit and priceperday are captured inside add_item[] of the checkbox.
But the third item which is Driver, there's no deposit or priceperday but price per hour.
So I gave out two radio buttons namely: one for per8thhour rate and the other one for per16thhour rate.
The problem is it captures both rate but I only want either one value of the radio button to be recorded.
Here's the table:
<table border="1" style="border-collapse: collapse;margin-top: 20px;">
<tr>
<th>AddOns</th>
<th>Deposit</th>
<th>PricePerDay</th>
<th>Price per 8th Hour</th>
<th>Price per 16th Hour</th>
<th></th>
</tr>
<?php
while($row_addon=mysql_fetch_array($result_addon))
{
$add_on_id=$row_addon['addOns_id'];
$add_on=$row_addon['addOns'];
$deposit=$row_addon['Deposit'];
$ppd=$row_addon['PricePerDay'];
$pp8=$row_addon['PricePer8thHour'];
$pp16=$row_addon['PricePer16thHour'];
$status=$row_addon['status'];
?>
<tr>
<td><input type="hidden" name="add_name" value=""><?php echo $add_on;?></td>
<td><input type="hidden" name="add_on"><?php echo $deposit;?></td>
<?php
if(isset($add_on)&&($add_on!=='Driver'))
{
?>
<td><input type="hidden" name="add_on"><?php echo $ppd;?></td>
<td><input type="hidden" name="add_on"><?php echo $pp8;?></td>
<td><input type="hidden" name="add_on"><?php echo $pp16;?></td>
<?php
}
else
{
?>
<td><input type="hidden" name="add_on"><?php echo $ppd;?></td>
<td><input type="radio" name="add_on"><?php echo $pp8;?></td>
<td><input type="radio" name="add_on"><?php echo $pp16;?></td>
<?php
}
?>
<td><input type="checkbox" name="add_item[]" value="<?php echo $add_on_id;?>"/></td>
</tr>
<?php
}
?>
<input type="hidden" name="submit_add">
<tr><td colspan="6"></td></tr>
</table>
I'm recording the value like this:
if(isset($_POST['submit_add']))
{
$add_item=$_POST['add_item'];
echo $add_item_1=implode(',',$add_item);
}
How do I make it possible to catch only the value selected by user for the item driver.
At the same time group them all under one name which is addon so that I can store them as one group in session later?
FULL FORM:
<form action="" method="POST">
<?php
//display pickup location
mysql_select_db($database_bumi_conn, $bumi_conn);
$q="SELECT * FROM tbl_pickup_location WHERE pickup_id='$location'";
$r=mysql_query($q);
$row_show= mysql_fetch_array($r);
?>
<input type="hidden" name="lo" value="<?php echo $location_name=$row_show['pickup_location'];?>">
<?php
echo 'Pickup Location :'.$location_name=$row_show['pickup_location'].'<br/>';
//display dropoff location
mysql_select_db($database_bumi_conn, $bumi_conn);
$q_1="SELECT * FROM tbl_dropoff WHERE dropoff_id='$d_location'";
$r_1=mysql_query($q_1);
$row_show_1= mysql_fetch_array($r_1);
?>
<input type="hidden" name="d_lo" value="<?php echo $location_name_1=$row_show_1['dropoff_location'];?>">
<?php
echo 'Return Location : '.$location_name_1=$row_show_1['dropoff_location'].'<br/>';
?>
<input type="hidden" name="val_1" value="<?php echo $date_value; ?>"/>
<?php
echo 'Pickup date : '.$date_value.'<br/>';
?>
<input type="hidden" name="val_2" value="<?php echo $date_value_2; ?>"/>
<?php
echo 'Return date : '.$date_value_2.'<br/>';
//$days=0;
$days=$diff->format("%a Days");
echo 'Total Rental for : '.$days.'<br/>';
//echo"hdssssssssssssfviodrhfvuhgudfhghdfhijswdjiahsdhsndjfhzsnhio";
$total_days=(int)$days;
?>
<input type="hidden" name="t_days" value="<?php echo $total_days; ?>"/>
<?php
include'calculation.php';
?>
<input type="hidden" name="sum" value="<?php echo $sum;?>">
<?php
mysql_select_db($database_bumi_conn, $bumi_conn);
$q_addon="SELECT * FROM tbl_addons";
$result_addon= mysql_query($q_addon)or die(mysql_error());
echo "<hr width=540>";
?>
<table border="1" style="border-collapse: collapse;margin-top: 20px;">
<tr><th>AddOns</th><th>Deposit</th><th>PricePerDay</th><th>Price per 8th Hour</th><th>Price per 16th Hour</th><th></th></tr>
<?php
while($row_addon=mysql_fetch_array($result_addon))
{
$add_on_id=$row_addon['addOns_id'];
$add_on=$row_addon['addOns'];
$deposit=$row_addon['Deposit'];
$ppd=$row_addon['PricePerDay'];
$pp8=$row_addon['PricePer8thHour'];
$pp16=$row_addon['PricePer16thHour'];
$status=$row_addon['status'];
?>
<tr>
<td><input type="hidden" name="add_name" value=""><?php echo $add_on;?></td>
<td><input type="hidden" name="add_item[]"><?php echo $deposit;?></td>
<td><input type="hidden" name="add_item[]"><?php echo $ppd;?></td>
<td><input type="radio" name="add_item[]" value="eight"><?php echo $pp8;?></td>
<td><input type="radio" name="add_item[]" value="six"><?php echo $pp16;?></td>
<td><input type="checkbox" name="add_item[]" value="<?php echo $add_on_id;?>"/></td>
</tr>
<?php
}
?>
<input type="hidden" name="submit_add">
<tr><td colspan="6"></td></tr>
</table>
<input type="image" src="submit.png" name="submit" value="submit" onclick="return confirm('Do you really want to proceed to checkout?');"href="checkout.php" style="padding-left:20px;" title="Add to cart"/>
</form>
To select one radio button at once, you have to give the all radio button's name same.
Otherwise you have to use javascript/jquery to deselect others when one is selected.
if you give all radio button name same, then when you post it will just post the value of the radio which is selected. You can get it by $_POST['radio_name'] .
Here it is $_POST['add_item'] .
If you are interested to use jquery, you can do that in this way
<script>
$(document).ready(function() {
$('input:radio').click(function(event) {
$('input:radio').not(this).attr('checked', false);
$(this).attr('checked', true);
});
});
</script>

Post values of single row from while loop using onclick

I am trying to pass the column values from a single row generated from a while loop. When the link is clicked I want certain values from that row to be passed as hidden fields to another page.
The code:
<form id="repeat" name="repeat" action="edit_user.php" method="post">
<?php
$counter = 0;
while($row = $result->fetch_array()) {
$color = ($counter & 1)? "#D7ECEC" : "#DEDEDE";
$counter++;
?>
<tr align="left" valign="middle" style="background: <?php print $color; ?>">
<td><a href="#" onclick="document.getElementById('repeat').submit();" >
<?php echo $row['firstname']; ?> <?php echo $row['surname']; ?></a></td>
<td><?php echo $row['userlogin']; ?></td>
<td><?php echo $row['accesslvl']; ?></td>
<td ><?php echo $row['chgpasswrd']; ?></td>
</tr>
<input type="hidden" name="id" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="lvl" value="<?php echo $row['accesslvl']; ?>" />
<?php } ?>
</table>
</form>
What is happening is that the hidden fields are passing values for the last row of the while loop. Is it possible to do this so the hidden values being passed are from the same row of the link that is being clicked?
I've tried a few things and nothing works and I've not been able to find anything that directly relates to this problem. Any help would be appreciated. Cheers
You need to split this into two pages really. The first page displays the user info and link for each unique user, and the second page provides the edit form for posting the form data as follows...
<table>
<?php
$counter = 0;
while($row = $result->fetch_array()) {
$color = ($counter & 1)? "#D7ECEC" : "#DEDEDE";
$counter++;
?>
<tr align="left" valign="middle" style="background: <?php print $color; ?>">
<td>
<a href="edit_user.php?userID=<?php echo $row['userID']; ?>&accesslvl=<?php echo $row['accesslvl']; ?>">
<?php echo $row['firstname']; ?> <?php echo $row['surname']; ?>
</a>
</td>
<td><?php echo $row['userlogin']; ?></td>
<td><?php echo $row['accesslvl']; ?></td>
<td ><?php echo $row['chgpasswrd']; ?></td>
</tr>
<?php } ?>
</table>
on the edit_user page you can populate the hidden fields with the information passed in by the url query string. Remember to clean the values to prevent sql injection or xss attacks. I've used htmlentities here as an example and you may want to enhance this, read the php documentation page on htmlentites for more information (ENT_QUOTES).
$userID = htmlentities($_GET['userID']);
$accesslvl = htmlentities($_GET['accesslvl']);
<form action="modify_user.php" method="post">
<input type="hidden" name="id" value="<?php echo (int)$userID; ?>" />
<input type="hidden" name="lvl" value="<?php echo $accesslvl; ?>" />
//rest of form html

how to post a looping value of radio button that have dynamics name?

i tried to develop a system that allowing users to answer a quiz that will be generated randomly from a database. the problems now is to mark the quiz. how can i post value of radio button because it has different name.
this is some of my code.
thanks.
$selq="SELECT * FROM question WHERE q_subject_kod = '$subkod' AND q_chapter= '$chap' order by RAND() limit $bil ";
$result=mysql_query($selq);?>
<form name="quiz" action="mark.php?bil=<?php echo $bil?>&chap=<?php echo $chap?>&action=1&subject=<?php echo $subkod?>" method="post">
<?php
##Cycle through randomly selected questions
if (mysql_num_rows($result)>=0) {
$qi = 1;
$e=0?>
<table name="q" border="1" cellpadding="2" width="100%">
<tr><td></td><td></td></tr>
<?php
while ($qry = mysql_fetch_array($result)) {
$idsol=$qry['id'];
$sol=$qry['q_quest'];
$jwpn=$qry['q_ranws'];?>
<tr><td><?php echo $qi?></td> <td><?php echo $sol?></td></tr>
<tr><td></td><td>
A<input type="radio" name="ans.<? echo $e?>" id="<? echo $idsol?>"value="<?php echo $qry['q_anws'];?>" /><?php echo $qry['q_anws'];?><br />
B <input type="radio" name="ans.<? echo $e?>" id="<? echo $idsol?>" value="<?php echo $qry['q_anws1'];?>" /><?php echo $qry['q_anws1'];?><br />
C <input type="radio" name="ans.<? echo $e?>" id="<? echo $idsol?>" value="<?php echo $qry['q_anws2'];?>" /><?php echo $qry['q_anws2'];?><br />
D <input type="radio" name="ans.<? echo $e?>" id="<? echo $idsol?>" value="<?php echo $qry['q_anws3'];?>" /><?php echo $qry['q_anws3'];?><br />
<input type="text" name="val.<? echo $idsol?>" value="<? echo $idsol?>">
</td>
</tr>
<?php
$qi++;
$e++;}}}?>>
Just use the Question, all Radio Buttons need the same name anyhow, and you don't nesecarly need an ID. So that would work just fine:
A<input type="radio" name="ans_<? echo $idsol?>" value="<?php echo $qry['q_anws'];?>" /><?php echo $qry['q_anws'];?><br />
B <input type="radio" name="ans_<? echo $idsol?>" value="<?php echo $qry['q_anws1'];?>" /><?php echo $qry['q_anws1'];?><br />
C <input type="radio" name="ans_<? echo $idsol ?>" value="<?php echo $qry['q_anws2'];?>" /><?php echo $qry['q_anws2'];?><br />
D <input type="radio" name="ans_ <? echo $idsol?>" value="<?php echo $qry['q_anws3'];?>" /><?php echo $qry['q_anws3'];?><br />
But just for the Matter of principle, try to store the Answers in a different Table then the Questions, this Way every Answer can have his own "id" and you are not limited to 4.

Categories