Insert group only if checkbox is checked - php

I am a little stuck with this. I am trying to insert groups of items only if that groups checkbox is clicked. I have tested the SQL insert without the checkboxes, and the SQL works perfectly and inserts the data. But when I added the checkboxes and if/else statement, it started to give me troubles.
For example:
-If I select group 1 and not group 2 - the SQL works correctly only inserting group 1 data
-If I select group 2 and not group 1 - The insert still inserts group 1 data
-If I select group 1 and group 2 - The SQL inserts two rows of group 1 data
-If I do not select any groups, then I get "Warning: Invalid argument supplied for foreach() in /home..." - which I will sort out later.
I have marked "Group 1" and "Group 2". I need the code to insert only the groups if checkbox is clicked, and ignore the non clicked checkbox groups. Any help would be great. Thanks.
I have been testing with the following code:
Form:
<form id="Form" method="post" action="/random.php" name="Form">
<input id="itemCurrency" type="hidden" value="2" name="itemCurrency"></input>
<div class="row">
<div class="col-lg-4">
<div class="itemHolder">
<img class="itemImg thumbnail" src="http://images.com/i/images.jpg"></img>
<div class="itemName">swim Shorts Wi...</div><
<div class="itemPrice">$33.33</div>
<div class="itemHref"><a target="_blank" href="http://random.com/product.aspx?id=40"> Visit Item Page </a></div>
</div>
</div>
//GROUP 1
<input id="itemCheckBox[]" type="checkbox" value="checked" name="itemCheckBox[]"></input>
<input id="itemName[]" type="hidden" value="some random text" name="itemName[]"></input>
<input id="itemHref[]" type="hidden" value="http://random.com/product.aspx?id=2140" name="itemHref[]"></input>
<input id="itemImg[]" type="hidden" value="http://images.com/i/image1s.jpg" name="itemImg[]"></input>
<input id="itemPrice[]" type="hidden" value="$37.33" name="itemPrice[]"></input>
//END GROUP 1
<div class="col-lg-4">
<div class="itemHolder">
<img class="itemImg thumbnail" src="http://images.com/i/image_xl.jpg"></img>
<div class="itemName">Bomber Jacket With Fur</div>
<div class="itemPrice"> $88.88</div>
<div class="itemHref"><a target="_blank" href="http://random.com/Bomber-Jacket-With-fur/..">Visit Item Page</a></div>
</div>
</div>
//GROUP 2
<input id="itemCheckBox[]" type="checkbox" value="checked" name="itemCheckBox[]"></input>
<input id="itemName[]" type="hidden" value="Bomber Jacket With Fur" name="itemName[]"></input>
<input id="itemHref[]" type="hidden" value="http://random.com/Bomber-Jacket-With-fur/.." name="itemHref[]"></input>
<input id="itemImg[]" type="hidden" value="http://images.com/i/image_xl.jpg" name="itemImg[]"></input>
<input id="itemPrice[]" type="hidden" value="$88.88" name="itemPrice[]"></input>
//END GROUP 2
<input id="site" type="hidden" value="1" name="site"></input>
<input id="submit_form" type="submit" value="yes" name="submit_form"></input>
</form>
PHP
if($submitForm == "yes" && $site == "1") {
foreach($_POST['itemCheckBox'] as $key => $val)
{
$fields[] = array(
'itemCheckBox'=>$_POST['itemCheckBox'] [$key],
'itemName' =>$_POST['itemName'] [$key],
'itemHref' =>$_POST['itemHref'] [$key],
'itemImg' =>$_POST['itemImg'] [$key],
'itemPrice' =>$_POST['itemPrice'][$key] );
//Remove non numaric characters from price(leave commas).
$itemPrice = preg_replace('/[^0-9,.]/s', '', $itemPrice);
//Data fields not in form
$userId = "username";
$userIp = $_SERVER['REMOTE_ADDR'];
$itemType = "ADD LATER";
$itemOutFit = "ADD LATER";
$site = "1";
$itemSale = "1";
//Only insert if checkbox is clicked --NOT WORKING, ????
if (isset($_POST['itemCheckBox'])){
echo "CHECKED";
echo "<br>";
/*** INSERT data ***/
$sql = "INSERT INTO items (userId,itemHref,itemImg,itemPrice,itemName,userIp,itemSite,itemType,itemCurrency,itemOutFit,itemSale) VALUES (:userId,:itemHref,:itemImg,:itemPrice,:itemName,:userIp,:itemSite,:itemType,:itemCurrency,:itemOutFit,:itemSale)";
$q = $conn->prepare($sql);
$q->execute(array(':userId'=>$userId,
':itemHref'=>$itemHref[$key],
':itemImg'=>$itemImg[$key],
':itemPrice'=>$itemPrice[$key],
':itemName'=>$itemName[$key],
':userIp'=>$userIp,
':itemSite'=>$itemSite,
':itemType'=>$itemType,
':itemCurrency'=>$itemCurrency,
':itemOutFit'=>$itemOutFit,
':itemSale'=>$itemSale));
} else {
echo "NOT CHECKED";
echo "<br>";
}
/*
echo $conn->errorCode();
echo "<br>"; ('SET CHARACTER SET utf8')
echo $conn->errorInfo();
echo "<br>";
//die(print_r($q->errorInfo(), true));
*/
}
UPDATE Tried to strip php down to basic code, still no luck. Does the same thing....anyone have an idea how I can fix this.
foreach($_POST['itemCheckBox'] as $key => $val)
{
$i = 0;
$itemCheckBox = $_POST['itemCheckBox'];
$itemName = $_POST['itemName'];
$itemHref = $_POST['itemHref'];
$itemImg = $_POST['itemImg'];
$itemPrice = $_POST['itemPrice'];
echo $i;
echo "<br>";
$i = $i +1;
if (isset($_POST['itemCheckBox'])){
echo "<br>";
echo "CHECKED";
echo "<br>";
echo "<br>";
echo $itemCheckBox[$i];
echo "<br>";
echo $itemName[$i];
echo "<br>";
echo $itemHref[$i];
echo "<br>";
echo $itemImg[$i];
echo "<br>";
echo $itemPrice[$i];
} else {
echo "NOT CHECKED";
echo "<br>";
$i = $i +1;
}
}

A couple things I spots that may be contributing to the issue:
In your PHP you have a line that has this:
//Remove non numaric characters from price(leave commas).
$itemPrice = preg_replace('/[^0-9,.]/s', '', $itemPrice);
I don't see where $itemPrice is defined though, should it be:
$itemPrice = preg_replace('/[^0-9,.]/s', '', $fields['itemPrice']);
Your issue of getting the "Warning: Invalid argument supplied for foreach() in /home..." can be solved by checking to make sure it exists and is an array beforelooping. You can use !empty() and is_array() to check.
if(!empty($_POST['itemCheckBox']) and is_array($_POST['itemCheckBox'])){}
Side Issue:
Your IDs for the inputs are all the same, "itemCheckBox[]", "itemName[]", etc.. are the same ID used for all of them. This may cause problems in the future if you want to use javascript or css properly.
Edit:
In your revised example the issue is that each time it cycles into the loop it is setting $i=0 because you declare it at the top of the foreach. If you go back to using the $key as the index it should work. See below:
//GROUP 1 checkbox
<input id="itemCheckBox[]" type="checkbox" value="0" name="itemCheckBox[]"></input>
//GROUP 2 checkbox
<input id="itemCheckBox[]" type="checkbox" value="1" name="itemCheckBox[]"></input>
I may also advise that you adjust your HTML items to match the Key you are passing:
<input id="itemName[1]" type="hidden" value="Bomber Jacket With Fur" name="itemName[]"></input>
<input id="itemHref[1]" type="hidden" value="http://random.com/Bomber-Jacket-With-fur/.." name="itemHref[]"></input>
<input id="itemImg[1]" type="hidden" value="http://images.com/i/image_xl.jpg" name="itemImg[]"></input>
<input id="itemPrice[1]" type="hidden" value="$88.88" name="itemPrice[]"></input>
foreach($_POST['itemCheckBox'] as $key => $val) {
$itemCheckBox = $_POST['itemCheckBox'];
$itemName = $_POST['itemName'];
$itemHref = $_POST['itemHref'];
$itemImg = $_POST['itemImg'];
$itemPrice = $_POST['itemPrice'];
if (isset($_POST['itemCheckBox'])){
echo "<br>";
echo "CHECKED";
echo $itemCheckBox[$key];
echo "<br>";
echo "<br>";
echo $itemCheckBox[$itemCheckBox[$key]];
echo "<br>";
echo $itemName[$itemCheckBox[$key]];
echo "<br>";
echo $itemHref[$itemCheckBox[$key]];
echo "<br>";
echo $itemImg[$itemCheckBox[$key]];
echo "<br>";
echo $itemPrice[$itemCheckBox[$key]];
} else {
echo "NOT CHECKED";
echo "<br>";
}
}

Related

Getting radio button value and storing in php

this is radio button code
now what if radio button does not have a constant name how would i store the data in database because to store the data in database we will need a name of form attribute
$sql1="select * from questions where email='". $_SESSION['email'] ."'";
$row=mysqli_query($conn,$sql1);
while ($result = mysqli_fetch_array($row))
{
?>
<h2 id="question_<?php echo $result['qid'];?>"><?php echo $result['qid'].".".$result['question'];?></h2>
<input type="radio" value="<?php echo $result['answer1'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer1'];?>
<input type="radio" value="<?php echo $result['answer2'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer2'];?>
<input type="radio" value="<?php echo $result['answer3'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer3'];?>
<input type="radio" value="<?php echo $result['answer4'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer4'];?>
if we know the name of radio button we can access it using
<input type="radio" value="<?php echo $result['answer4'];?>" name="name"><?php echo $result['answer4'];?>
$name=$_POST['name'];
but in the above code name of radio button is not fixed.questions is the table which consists of qid and questions with multiple options that is answer1,answer2 etc
i want to store the option select by user into database.for which i need to know the name of radio button
how should i use post in this case
$name=$_POST['what should go in here'];
You can get the radio button value along with question ID as:
Basic Example:
<?php
$array = array(1,2); // your Question ID array
?>
Your Form:
<form method="post" action="">
<?php
foreach ($array as $key => $qid) {
?>
<input type="radio" value="1" name="radio[<?=$qid?>]">
Answer 1
<input type="radio" value="2" name="radio[<?=$qid?>]">
Answer 2
<input type="radio" value="3" name="radio[<?=$qid?>]">
Answer 3
<input type="radio" value="4" name="radio[<?=$qid?>]">
Answer 4
<?php
echo "<br/>";
}
?>
<input type="submit" name="submit">
</form>
In PHP:
<?php
if(isset($_POST['submit'])) {
$query = array();
foreach ($_POST['radio'] as $key => $value) {
$query[] = "('$value','$key')";
}
$sql = "INSERT INTO table (answer,questionID) VALUES ";
$sql .= implode(",", $query);
echo $sql;
}
?>
In this example query look like:
INSERT INTO table (answer,questionID) VALUES ('2','1'),('3','2')
Few Suggestions:
- Your code is open for SQL Injection, you must need to prevent your code with SQL Attack and this reference will help you to understand: How can I prevent SQL injection in PHP?
- Make sure your column name and table name not having any conflict, currently, you are using same name for both.
Update with Your Code:
<?php
while ($result = mysqli_fetch_array($row))
{
?>
<h2 id="question_<?php echo $result['qid'];?>"><?php echo $result['qid'].".".$result['question'];?></h2>
<input type="radio" value="<?php echo $result['answer1'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer1'];?>
<input type="radio" value="<?php echo $result['answer2'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer2'];?>
<input type="radio" value="<?php echo $result['answer3'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer3'];?>
<input type="radio" value="<?php echo $result['answer4'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer4'];?>
<?
}
?>
In PHP:
<?php
if(isset($_POST['submit'])) {
$query = array();
foreach ($_POST['radio'] as $key => $value) {
$query[] = "('$value','$key')";
}
$sql = "INSERT INTO table (answer,questionID) VALUES ";
$sql .= implode(",", $query);
echo $sql; // run this query in mysqli_query()
}
?>
Few More Instructions:
- Change the table name as per your table name
- Change the column name according to your column.
- Use INSERT query at once, no need to use it inside the loop.
Create an array at the end of while loop like this :
while ($result = mysqli_fetch_array($row))
{
?>
<h2 id="question_<?php echo $result['qid'];?>"><?php echo $result['qid'].".".$result['question'];?></h2>
<input type="radio" value="<?php echo $result['answer1'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer1'];?>
<input type="radio" value="<?php echo $result['answer2'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer2'];?>
<input type="radio" value="<?php echo $result['answer3'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer3'];?>
<input type="radio" value="<?php echo $result['answer4'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer4'];?>
<? $names[] = $result['qid'];
}
if(isset($_POST['submit'])) {
for ($i=0; $i<count($names); $i++) {
if(isset($_POST[$names[$i]]) {
$rate = $_POST[$names[$i]];
$sql ="INSERT INTO answer (answer, qid) VALUES (".$rate.", ".$names[$i] .")";
mysqli_query($con, $sql);
}
}
}
?>

PHP- how to get values from checked checkboxes and corresponding textboxes

I want to get three values separated by a comma when the form is submitted. The value from the checkbox, textbox 1 and textbox 2.
This is my code that retrieves values from mysql database and generates checkboxes and two corresponding textboxes.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
$query = "SELECT * FROM subject";
$data = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($data)){
$s = $row['sub'];
echo $s;
?>
<input type="checkbox" name="req_sub[]" value= "<?php echo $s; ?>" />
<input type="text" name="<?php echo $s; ?>" placeholder="Total Number" />
<input type="text" name="<?php echo $s; ?>" placeholder="Pass Number" />
<?php
}
?>
<input type="submit" name="submit" value="Add">
</form>
Suppose the user checks the first three boxes , and enters the values like in this picture -
when I click add, I get the values --
Physics
Math
Chemistry
by using the code below:
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['req_sub'])) {
foreach ($_POST['req_sub'] as $selected) {
echo $selected."</br>";
}
}
}
?>
but how do I get the values like this-
Physics,40,30
Math,40,30
Chemistry,30,25
I want this output in a variable so that I can store it in my database table.
I have spent several hours behind this in last few days. Please help me with this one.
you need to assign unique names to the <input type="text" ... /> so you can recieve its values in the PHP.
Second, you need to write PHP code, that's concatenating those values.
For example, your HTML code might be:
<input type="checkbox" name="req_sub[]" value= "<?php echo $s; ?>" />
<input type="text" name="total[<?php echo $s; ?>]" placeholder="Total Number" />
<input type="text" name="pass[<?php echo $s; ?>]" placeholder="Pass Number" />
and your PHP code might be:
if (isset($_POST['submit'])) {
if (!empty($_POST['req_sub'])) {
foreach ($_POST['req_sub'] as $selected) {
$total = $_POST['total'][$selected];
$pass = $_POST['pass'][$selected];
$var = $selected . ',' . $total . ',' . $pass;
echo $var . '<br />';
}
}
}

Checkboxes are checking out

I've got a page with checkboxes generated with the database. When we press the checkbox and submit it, it is working fine and it is updating in the database. But when I try to uncheck "1" checkbox it is checking out all checkboxes which are selected.
Query:
if(isset($_POST['submit'])){
foreach ($_POST['untrain'] as $room_id => $user_id) {
// This query needs protection from SQL Injection!
$user_id;
$untrainQuery = "UPDATE room_users SET trained = '1' WHERE user_id = $user_id AND room_id = $room_id";
$db->update($untrainQuery);
}
}
if(isset($_POST['submit'])){
foreach ($_POST['amk'] as $room_id => $user_id) {
// This query needs protection from SQL Injection!
$user_id;
$untrainedQuery = "UPDATE room_users SET trained = '0' WHERE user_id = $user_id AND room_id = $room_id";
$db->update($untrainedQuery);
}
}
Checkboxes:
<?php
if($room->trained == 1)
{ ?>
<input type='hidden' value="<?php echo $room->user_id; ?>" name="amk[<?php echo $room->room_id; ?>]">
<input type='checkbox' value="<?php echo $room->user_id; ?>" name="trained[<?php echo $room->room_id; ?>]" checked>
<?php echo "Y"; }
else{ ?>
<input type='checkbox' value="<?php echo $room->user_id; ?>" name="untrain[<?php echo $room->room_id; ?>]">
<?php echo "N";
}?>
</td>
<Td><?php
if($room->active == 1) {
?> <input type='checkbox' name="<?php echo $room->room_id; ?>" checked>
<?php echo "Active"; }
else { ?>
<input type='checkbox' name="<?php echo $room->room_id; ?>"
<?php echo "Inactive"; } ?>
I used the trick with the "hidden" input before the checkbox, but the only problem is that it is not working. When I click on it, it resets all checkboxes to 0.
I think you are missing how the combo checkbox + hidden input does work.
So here you go freely inspired by this answer:
<input id="foo" name="foo" type="checkbox" value="1" />
<input name="foo" type="hidden" value="0" />
Looks like you do know, if you use the trick, that, if the checkbox is unchecked, it will not be present in the post. So to trick the form, we will always add an hidden field. And if the checkbox is checked, then the fact that it will be included in the post is going to override the value of the hidden input.
So for your specific problem :
<td>
<input type="checkbox" value="1" name="trained[<?php echo $room->room_id; ?>_<?php echo $room->user_id; ?>]" <?php echo ($room->trained == 1) ? ' checked' : '' ?> /> Trained
<input type="hidden" value="0" name="trained[<?php echo $room->room_id; ?>_<?php echo $room->user_id; ?>]"/>
</td>
Please note the use of the ternary operator on this part of the code <?php echo ($room->trained == 1) ? ' checked' : '' ?> which I may use a lot when writing html template.
Please also note the trick on the name trained[<?php echo $room->room_id; ?>_<?php echo $room->user_id; ?>] which is needed because we cannot set the user_id as value of the input.
Then for the processing part :
if ( isset ( $_POST['submit'] ) ) {
foreach ( $_POST['trained'] as $ids => $value ) {
// This query needs protection from SQL Injection!
// ^ good point, on which I would suggest you using PDO and prepared statement :)
list($room_id,$user_id) = explode('_',$ids);
// ^ now need to explode the name on the underscore to get both user_id and room_id cf the trick above
$untrainQuery = "UPDATE room_users SET trained = '$value' WHERE user_id = $user_id AND room_id = $room_id";
$db->update ( $untrainQuery );
}
}
Wash, rinse, repeat for every checkbox you need and you should be good to go.

Is it possible to have multiple "values" posted from a HTML form (checkboxes)?

I am developing an ordering page in PHP for some products. The user needs to be able to select multiple types of products, so checkboxes will be necessary in the HTML form.
I've established an array for the checkboxes via the "name" attribute.
My problem is, I want to display what the user has selected on a confirmation page, so I'd like the "value" of those checkboxes to ultimately return a product name AND a product price. As far as I can tell I'm going to be stuck with one value only, so I've attempted to get around this:
<?php
//variables for the array and the count
$parray = $_POST['product'];
$pcount = count($parray);
//arrays for each product's information; i've only listed one for simplicity
$br = array("Big Red", 575);
/*The idea for this was for each product ordered to add a line giving the product information and display it. When the $key variable is assigned, it is stored simply as a string, and not an array (yielding [0] as the first letter of the string and [1] as the second), which I expected, but my general idea is to somehow use $key to reference the above product information array, so that it can be displayed here*/
if (!empty($parray))
{
for ($i=0; $i < $pcount; $i++)
{
$key = $parray[i];
echo "<tr><td height='40'></td><td>" . $key[0] . "</td><td>" . $key[1] . "</td></tr>";
}
}
?>
Is there anyway to make my $key variable actually act as if it is the array's name it is set to?
If not, is there any good way to do this?
So in your HTML table, you'll need to render each checkbox like this:
<input type="checkbox" name="selectedIDs[]" value="$key" />
where $key is the item number.
Then, in your PHP, you'll have a $_POST["selectedIDs"] variable, which is an array of all the item numbers that were checked.
Assuming you've got a product list array like this:
$products = array( 1 => array("Big Red", 575), 2 => array("Spearmint", 525));
You can then print a list of the selected products using a loop like this:
for($i = 0; $i < count($_POST["selectedIDs"]); $i++) {
$key = $_POST["selectedIDs"][$i];
$product = $products[$key];
echo "<tr><td height='40'></td><td>" . $product[0] . "</td><td>" . $product[1] . "</td></tr>";
}
The only real difference between this and what you wrote is that my $products array is two-dimensional, and my $key is used to grab the relevant product from the $products array.
You can give the value of the options the value of the ID of the corresponding item. Then you'll receive an array of ID's, which you can then again map to the $br array, where you look up the name and price of an item by its ID. You could use the array key as ID, so:
<!-- Form: -->
<input type="checkbox" name="product[]" value="1" />
<input type="checkbox" name="product[]" value="..." />
<input type="checkbox" name="product[]" value="15" />
[...]
<?php
$product = array();
$product[1] = array('name' => "Big Red", 'price' => 575);
$product[...] = array('name' => "...", 'price' => ...);
$product[15] = array('name' => "Small Green", 'price' => 475);
foreach ($_POST['product'] as $pId)
{
echo $products[$pId]['name'] . " costs " . $products[$pId]['price'];
}
?>
If of course your array originates from a database, you can use the table's ID's as values for the checkboxes. You could then implode the $_POST['product'] (of course after escaping it) with a comma, and use it in a SELECT ... WHERE ID IN ($productIds) query.
If i'm understanding you correctly, you'll have checkboxes something like this:
<input type="checkbox" name="product_id[]" value="1">Product #1<br/>
<input type="checkbox" name="product_id[]" value="2">Product #2<br/>
<input type="checkbox" name="product_id[]" value="3">Product #3<br/>
That should post whatever is checked as an array to your server.
gen_paper1.php
<div style="overflow: auto; height: 500px; border: 1px solid;">
<form action="" method="post">
<fieldset style="font-size: 15px; font-weight: bolder;">
<h4><u>Your Existing Header's are listed below</u> </h4>
<hr / >
<?php
$xhdr = mysql_query("SELECT * from s_user_header ORDER BY 'ASC'");
while($rheader = mysql_fetch_array($xhdr)){
$h = $rheader['h_content'];
$hid = $rheader['h_id'];?>
<script>
$(document).ready(function(){
//$('input[type=checkbox]').val('<?php echo $hid ; ?>').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
//$('#c').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
$('').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
});
</script>
<table>
<td><input type="checkbox" id="c" name="u_hdr[]" value="<?php echo $hid; ?>"></td>
<td style="font-weight: bolder"><?php echo $h."<br />"; ?></td>
</table>
<?php } ?>
</fieldset>
</div>
gen_paper2.....3.....
i need all this values in further page, so have taken hidden variables
<input type="hidden" name="hstdid" value="<?php echo $stdid; ?>">
<input type="hidden" name="hsubid" value="<?php echo $subid; ?>">
<input type="hidden" name="hdifid" value="<?php echo $difid; ?>">
<input type="hidden" name="hmarks" value="<?php echo $t_marks; ?>">
<input type="hidden" name="h_hid[]" value="<?php echo $hh; ?>">
gen_paper4.php
$getstdid = $_POST['hstdid3'] ;
$getsubid = $_POST['hsubid3'] ;
$getdifid = $_POST['hdifid3'] ;
$gethmarks = $_POST['htmarks'] ;
$hdr= $_POST['h_hdrid'] ;
$h = implode($hdr);
<table>
<?php
$h1 = explode(",",$h);
count($h1) . "<br />";
for($i=0;$i<count($h1);$i++){
$h1[$i];
$xheader = mysql_query("SELECT * from s_user_header WHERE h_id = ".$h1[$i]);
while($row = mysql_fetch_array($xheader)){ ?>
<tr>
<td><b>
<?php echo $i + 1 . ".&nbsp&nbsp&nbsp"; ?>
</b></td><td>
<?php echo $header = $row['h_content'] . "<br />";
?></td>
</tr>
<?php
}
}
//echo $header; ?>
</table>

Update multiple rows with radio button check

I am getting the value for the single tsid for each record, however the checked radio button value is not returned in the array, I just get 0? Any help is appreciated.
PHP:
// Set the timesheets to set status approved/rejected
// find out how many records there are to update
$size = count($_POST['tsid']);
// start a loop in order to update each record
$i = 0;
while ($i < $size) {
// define each variable
$tsid = intval($_POST['tsid'][$i]);
$personnelid = intval($_POST['personnel'][$i]);
print "TSID: " . $tsid . "<br>";
print "TSuser: " . $personnelid . "<br>";
if ($tsid > 0 && $personnelid > 0) {
// do the update and print out some info just to provide some visual feedback
$query = "Update timesheets set status='1' where id= '$tsid' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
}
++$i;
}
mysql_close();
HTML:
<input type="hidden" name="tsid[]" value="<?PHP echo $row['id']; ?>">
<li data-role="fieldcontain">
<a href="tsapprove.php?id=<?PHP echo $row['id']; ?>"><p><?PHP echo $row['personnel']; ?></p>
<p><?PHP echo $row['name']; ?></p>
<p class="ui-li-aside"><strong><?PHP echo $row['totalhrs']; ?> H</strong></p>
<fieldset data-role="controlgroup" data-type="horizontal">
<input type="radio" name="personnel[]" id="1" value="<?PHP echo $row['personnel']; ?>" />
<label for="1">Approve</label>
<input type="radio" name="personnel[]" id="2" value="<?PHP echo $row['personnel']; ?>" />
<label for="2">Reject</label>
</fieldset>
</a>
View Details
</li>
Hopefully you can piece this together:
<?
$size = count($_POST['tsid']);
echo "<pre>";print_r($_POST);echo "</pre>";
$i = 0;
while ($i < $size) {
$tsid = $_POST['tsid'][$i];
$pid = $_POST['personnel_'.$i];
print "TSID: " . $tsid . "<br />";
print "TSuser: " . $pid . "<br />";
$i++;
}
?>
<form method="post">
<!--tsid[0] - $i = 0-->
<input type="hidden" name="tsid[]" value="1" />
<input type="radio" name="personnel_0" value="111" />
<input type="radio" name="personnel_0" value="222" />
<!--tsid[1] - $i = 1-->
<input type="hidden" name="tsid[]" value="2" />
<input type="radio" name="personnel_1" value="333" />
<input type="radio" name="personnel_1" value="444" />
<input type="submit" />
</form>
Basically, the way you are returning personnel doesnt really work. So I simplified this a bit, and basically set the radios for personnel differently. Instead of an array, its setting the actual name with the TSID value.
I hard coded 1 and 2, but you would replace with your $row['id'], as well has the number after "personnel_" would be 0..1..2 etc
Hopefully this helps
edit: few issues with code

Categories