checkbox array with corresponding input value - php

I fetch data from mysql in php. List all items with checkbox and quantity (rqty) as input value where user can enter quantity. But I'm not able to relate checkbox with corresponding input value (rqty) in an array. If I select first checkbox and enter quantity it works fine but if select any other checkbox and enter quantity, it's not getting updated in database. My code is as below:
<?php
If (isset($_REQUEST['submit1'])!='') {
foreach($_POST['itm'] as $key=>$itm) {
$odate=date("Y-m-d H:i:s");
$rqty=$_POST['rqty'][$key];
$itm=$_POST['itm'][$key];
$sql1="insert into table (itemtype,itemname,qty ,retdate,reason) select itemtype, itemname, '$rqty', '$odate', 'Return' from stock where itemname='$itm';
mysql_query($sql1);
}
}
?>
<p>
<form action="" method="post" name="form1">
<?php
$query3 = "SELECT id, category, itemtype, itemname, qty, status FROM stock where location='godown'";
$comments3 = mysql_query($query3);
$qty = mysql_num_rows($comments3);
echo "Consumable Items: " . $qty;
print "<table class='blue'>
<thead>
<tr>
<td width=100>Return</td>
<td width=100>Category</td>
<td width=100>Item Type</td>
<td width=100>Item Name</td>
<td width=100>Available Qty.</td>
<td width=100>Return Qty.</td>
<td width=100>Status</td>
</tr>
</thead>";
while($row1 = mysql_fetch_assoc($comments3))
{
$it=$row1['itemname'];
print "<tbody>";
print "<tr>";
print "<td><input type='checkbox' name='itm[]' value='$it'></td>";
print "<td>" . $row1['category'] . "</td>";
print "<td>" . $row1['itemtype'] . "</td>";
print "<td>" . $row1['itemname'] . "</td>";
print "<td>" . $row1['qty'] . "</td>";
print "<td><input type='text' name='rqty[]' placeholder='Enter Qty' /></td>";
print "<td>" . $row1['status'] . "</td>";
print "</tr>";
print "</tbody>";
}
print "</table>";
?>
<input type='submit' name='submit1' value='Return' />
In inspect I can see that all rqty[] are getting posted with blank data. But in case of itm, only itm which is selected is getting posted which is correct.
enter image description here

Since I did not receive any solution for this, I changed the approach. Now handling each checkbox separately and not using any array. Added submit button in form list and posting required values to other php page where it's processed further.

That's because of your loop:
foreach($_POST['itm'] as $key=>$itm) {...
and input "itm" is checkbox: <input type='checkbox' name='itm[]' value='$it'></td>
For instance, if you have 5 checkboxes in the form-table but only 2 of them was checked - then you'll have only 2 items in array $_POST['itm'], not 5.
To fix this issue you need to loop through $_POST['rqty'] instead of $_POST['itm']

Related

PHP While loop in Table in Form

I'm trying to create a Table where you can 'Sign Off' items populating it. Currently the table looks to be populated fine, however when you go to 'Sign Off' an item - it will only act on the latest mysql row ['directory'] as well as the latest text field accompanying it.
When I say 'Sign Off' I mean to insert someone's initials into the mysql table row associated with that item. I first of course need to get the POST submission handled correctly.
That probably wasn't articulated perfectly, so please take a look at my current code;
echo "<form action='signoff.php' method='post' id='signoffform' style='display:inline;'>";
echo "<p3><table id='myTable3' class='tablesorter'>
<thead>
<tr>
<th>To Delete</th>
<th>Directory</th>
<th>Space Used</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='text' name='signoff' id='signoff' form='signoffform' /><button>Sign Off</button> Signed Off By:" . $row['signoff'] . "</td>";
echo "<td><input type='text' name='dir' id='dir' form='signoffform' value='" . $row['directory'] . "' readonly /></td>";
echo "<td>" . $row['used'] . "</td>";
echo "</tr>";
}
echo "</table></p3>";
echo "</form>";
signoff.php
<?php
echo $_POST["signoff"];
echo "<br />";
echo $_POST["dir"];
?>
Please feel free to request more info
... when you go to 'Sign Off' an item - it will only act on the latest mysql row ['directory'] as well as the latest text field accompanying it.
The problem is because of your name attributes, name='signoff' and name=dir. When you loop through the result set, the name attribute value gets overwritten in each iteration. Hence whatever button you click, you'll always get the last row values.
So the solution would be like this:
Change your while loop in the following way,
// your code
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td><input type='text' name='signoff[".$row['directory']."]' id='signoff' form='signoffform' /><button>Sign Off</button> Signed Off By:" . $row['signoff'] . "</td>";
echo "<td><input type='text' name='dir[".$row['directory']."]' id='dir' form='signoffform' value='" . $row['directory'] . "' readonly /></td>";
echo "<td>" . $row['used'] . "</td>";
echo "</tr>";
}
// your code
And on signoff.php page, process your form in the following way,
foreach($_POST['dir'] as $dir){
if(!empty($_POST['signoff'][$dir])){
$signoff = $_POST['signoff'][$dir];
// That how you can get $dir and the corresponing $signoff value
}
}
Sidenote: If you want to see the entire $_POST array structure, do var_dump($_POST); on signoff.php page.

How to access values of checkboxes in php mysql

I have a table with pizza name, pizza type and price. I used a loop to print all the items in a table.
<form method="Cart.php" method="post">
<table border="1" cellpadding="10">
<tr>
<th>Pizza name</th>
<th>Pizza type</th>
<th>Price</th>
</tr>
<?php
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['subcat_name'] . "</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['price']. "</td>";
echo "<td><input type=\"checkbox\" name=\"checkbox\" value=\"\" id=\"checkbox\"></td>";
echo "</tr>";
}
echo "</table>";
?>
<input type="submit" name="addToCart" id="addToCart"/>
</form>
Now I want to access the pizza names and prices as I click the submit button.
You can access them by first checking if it exists...
if(isset($_POST['checkbox']){
$checkbox_value = $_POST['checkbox'];
}else{
$checkbox_value = ""; // set a default value here
}
I should add that checkboxes that are NOT "checked" will not pass along in a POST, so you need to explicitly check if it has been "checked" by calling
if(isset($_POST['checkbox'])){}
Which at that point you can decide to set a value yourself or use the value you set in the form.
Here is another way, Hope this helps.
if(isset($_POST['addToCart'])){
$check_list = $_POST['checkbox'];
foreach ($check_list as $checked=>$value) {
//Here you got all checked values in "$checked"
//Eg: to move checked values to array
array_push($pizzaOrdered, $value);
}
}
Try this just use the name of the check box $_POST['checkbox']

How to get the ID of all the checked boxes on displayed data base QUICKFIX?

I have displayed a table of my data from the data base with check boxes to the left. I want to find a way to link the check boxes to the question number (ID). when I hit submit I want the selected id's to be echoed. pretty much I want someone to be able to select the questions they want and then display them.
<?php
$con=mysqli_connect("####","####","#####","###");
$result = mysqli_query($con,"SELECT * FROM q_and_a ");
echo "<table border='1'>
<tr>
<th>Add</th>
<th>#</th>
<th>Question</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>Answer</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><input type="checkbox" name="questions[]" value="$id"></td>';
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['question'] . "</td>";
echo "<td>" . $row['choiceA'] . "</td>";
echo "<td>" . $row['choiceB'] . "</td>";
echo "<td>" . $row['choiceC'] . "</td>";
echo "<td>" . $row['choiceD'] . "</td>";
echo "<td>" . $row['answer'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
submit button
<form method="POST" action="makeTest.php">
<input type="submit" name="make" value="Make Test">
</form>
Make some edits to your code then it will work.
1. Change your query by adding fields not * (to ensure performance and display order)
$result = mysqli_query($con,"SELECT id,question,choiceA,choiceB,choiceC,choiceD,answer FROM q_and_a ");
then before while block open form tag(HTML)
<?php
//above codes will be there as you show before
echo '<form method="POST" action="makeTest.php">';
while($row = mysqli_fetch_array($result)){
{ $id=$row['id']; // initialize your id here, so as to pass it in checkbox too
// then continue with your code
}
?>
<input type="submit" name="make" value="Make Test">
</form>
in maketest.php yo can hande ckeckbox using foreach, see below
foreach($_POST['questions'] as $questions){
//do your job
}

Using $_GET with a submit button

I have a table with 3 columns like so in index.php:
ID | StName | Inspect
_________________________________
1 Wisconsin INSPECT
2 Alabama INSPECT
3 Nebraska INSPECT
The right most column is a submit button which takes the user to a page called inspect.php. I want to send the corresponding ID column value to inspect.php when a user clicks the INSPECT button so that I can use that value inside inspect.php, how can I do this?
Currently I have:
//index.php
echo "<table border = 1>
<tr>
<td>ID</td>
<td>StName</td>
</tr>";
// go into a loop to create more rows -- omitted
echo "<tr>";
echo "<td>" . $resultArr[0] . "</td>";
echo "<td>" . $resultArr[1] . "</td>";
echo "<td> <form action='inspect.php' method='get'>
<input type='Submit' value='Inspect'>
</form>
</td>";
echo "</tr>";
echo "</table>";
$resultArr[0] contains the ID value I want to send to inspect.php
So add an input field inside the form with a name attribute which will be an index key name on inspect.php page
<form action='inspect.php' method='get'>
<input type='hidden' name="id" value='<?php echo $resultArr[0]; ?>'>
<input type='submit' value='Inspect'>
</form>
A better and simple way to go for this is without a submit, just use a hyperlink like
Inspect
And later process this id on inspect.php page
I think I got what you wanted to work. I am using different data in the array, but you should be able to change to your needs anyway.
On index.php:
$resultArr = array("dave","fff","erere");
echo "<table border = 1>
<tr>
<td>ID</td>
<td>StName</td>
</tr>";
// go into a loop to create more rows -- omitted
echo "<tr>";
echo "<td>" . $resultArr[0] . "</td>";
echo "<td>" . $resultArr[1] . "</td>";
echo "<td> <form action='inspect.php' method='get'>
<input type='hidden' name='id' value=' $resultArr[0] '>
<input type='submit' name='inspect' value='Inspect'> </td>";
echo "</tr>";
echo "</table>";
And on inspect.php:
if( $_GET['inspect']){
$name = $_GET['id'];
echo $name;
}
This outputs the value of $resultArr[0], which I think you wanted.

How to create submit form in a row and relate input to that row?

Hey Guys, I have a question for you.
Imagine that I wanted to be able to keep track of how many miles I've ran every week, so that I could
compare it to the goals I've set for each week. So i've created this table by the use of mysql_fetch_row.
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><form><input method='post' type='number'></form></td>";
echo "</tr>";
}
echo "</table>";
This piece of code resultet in a table with 10 weeks with 10 goals - and a column for the actual number of miles. This column should include 10 input forms where the actual number of miles can be submitted. But how do I relate the input from the submit form to the row in which the submit form is positioned?
The primary key is the week - so this would be the one to relate to.
Hope you understand what my problem is:)
To do this you would use a hidden input field.
When you echo each row, and the form in that row, you would simply add an extra line:
`<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';
In full, your code would be:
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>
<form>
<input method='post' type='number'>
<input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
</form>
</td>";
echo "</tr>";
}
echo "</table>";
I think there should be some modifications that has to be done in loop.
echo "<td><form method='post'><input type='number' value='".$rows['col_name']."'><input type='submit' ></form></td>";
This code adds a submit button to each row. But, this shouldn't be what I think.
It should be rather this way,
echo "<form method='post'> ";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><input type='number' value='".$rows['col_name']."'></td>";
echo "</tr>";
}
echo "<input type='submit' ></form>";
Or make the input field look like this.
'<input type="text" name="row['.$row['id_column'].'][miles]" />';
It will return you an array when you post it.
foreach($_POST['row'] as $key => $value){
// $key is your primary key
// $value['miles'] is your input value
}

Categories