How to access values of checkboxes in php mysql - php

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']

Related

checkbox array with corresponding input value

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']

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.

echo checkbox that when checked sets complete to ID in database

I have a table that I am echoing out and now I added a check box to the end of it.
I would like when that box is checked. It can be checked on multiple items and then the save button is clicked. It would go to the post page and set complete in the database to "Yes" for all the IDs that were checked.
Problem is I do not understand how the checkbox will know which ID is which once it goes to the post page.
<?php
$conduct = $_SESSION['username'];
$query = mysqli_query($con, "SELECT * FROM newworders WHERE train = 'Yes' AND conductor = '$conduct' AND complete = ' '");
echo "<table id='tb' border='1'>
<tr class='head'>
<th>First Name</th>
<th>Last Name</th>
</tr>";
while($row = mysqli_fetch_array($query)) {
echo "<tr>";
echo "<td>" . '' . $row['first'] . '<br />' . "</td>";
echo "<td>" . '' . $row['last'] . '<br />' . "</td>";
echo "<td>"."<input type='checkbox' value='Yes' name='complete'" . "</br>". "</td>";
}
<div class='new'>
<form action="savepending.php" method='POST'>
<input type='submit' name ='save'/>
</form>
</div>
Then the post page.
<?php
if(isset($_POST['save']))
{
$query = mysqli_query($con, "UPDATE newworders SET complete = 'Yes' WHERE ")
}
?>
I have no idea what to put in the WHERE part. I just dont understand how it will tell which ID is which.
Make your checkboxes an arrray, indexed by newworders primary key. Then you can just iterate through complete. PHP arrays are associative, so you will only have as many elements as you have checkboxes.

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
}

PHP GET problem - no form $_GET data is present

I have a simple PHP form which displays inputs with values from a mysql DB and sends the form results to another page which updates a db table based on the GET results:
echo "<table>";
echo "<tr>";
echo " <th>Project No</th>
<th>Customer Name</th>
<th>Description</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><input value=" . $row['project_no'] . "></input></td>";
echo "<td><input value='" . $row['cust_name'] . "'></input></td>";
echo "<td><input value='" . $row['description'] . "'></input></td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' value='Update' />";
echo "</form>";
In updateprojects.php when I do:
echo $_GET['project_no'].$_GET['cust_name'].$_GET['description'];
I don't see any values. Why is this?
In the input field you need to specify the parameter name with the "name" attribute.
echo "<tr>";
echo "<td><input name=\"project_no\" value=" . $row['project_no'] . "></input></td>";
echo "<td><input name=\"cust_name\" value='" . $row['cust_name'] . "'></input></td>";
echo "<td><input name=\"description\" value='" . $row['description'] . "'></input></td>";
echo "</tr>";
You are forgetting the input names:
<td><input name='project_no' value=" . $row['project_no'] . "></input></td>
If you don't do this php doesn't know what you mean by 'project_no'. Each input needs a name.
It is because you do not assign names to the input tags:
echo "<td><input name=\"project_no\" value=\"" . $row['project_no'] . \"></input></td>
The above should work.
Your inputs have no name attribute
Since you are creating form inputs with a loop this should work for you (you can't have 2 input fields with same name), just grab an id of row/record from the database too:
echo "<form method='get' action='updateprojects.php'>";
echo "<table>";
echo "<tr>";
echo " <th>Project No</th>
<th>Customer Name</th>
<th>Description</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo '<td><input value="' . $row['project_no'] .'" name="form['.$row['id'].']['project_no']"/></td>';
echo '<td><input value="' . $row['cust_name'] .'" name="form['.$row['id'].']['cust_name']"/></td>';
echo '<td><input value="' . $row['description'] .'" name="form['.$row['id'].']['description']"/></td>';
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="submit" value="Update" />';
echo "</form>";
if(isset($_GET['submit'])){
foreach($_GET['form'] as $id=>$column){
//update your database where id=$id. This is just testing
echo 'Row '.$id .' =>'. $column['project_no'].'-'.$column['cust_name'].'-'.$column['description'];
}
}
You need to add the name="" attribute to your inputs.
Something that has helped me in the past, is to use the Net tab in Firebug and enable the Persist option to see what the browser is actually sending back. In one case, the form had duplicate entries for some for fields, but the second entry wasn't sending the correct values.
Please inform the type of submit, or hidden text. And / or verify that you are returning data from sql query.
It appears to me that you need to have a name="project_no" in you HTML input.

Categories