How do i integrate a form with a table? - php

Here is my current TABLE:
print "<table cellpadding=12px>";
print "<th>Name</ th>";
print "<th>Price</ th>";
print "<th>Image</ th>";
if ($cat == 'Clothes'){
$query = "SELECT * FROM Products WHERE Type = 'Clothes' ORDER BY ProductPrice";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result))
{
echo " <tr>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "<td>" . '&pound'. $row['ProductPrice'] . "</td>";
echo "<td>" . '<img width=200px height=200px src="./ProductsPic/' . $row['ProductPic'] . '" />' . "</td>";
echo "<td> Add to Cart</td>";
echo "</tr>";
}
print "</table>";
However, when i click on the "add to cart" link, it only sends the ID of the product to the cart. I somehow need to send the price and name of that product (possibly hidden data?). So would it be easy to integrate a form into my table, or would there be another way i can add the name and price of the product into my cart array?

If the ID field is the unique key that represents the product, then you can simply fetch its other details from the database. Otherwise you can do the following:
You could add more parameters to the query string.
echo "<td> Add to Cart</td>";
You could create a form with hidden values, and convert the Add to Cart link to a submit button.
echo '
<form action="cart.php">
<input type="hidden" name="price" value="'.$row['ProductPrice'].'">
<input type="hidden" name="name" value="'.$row['ProductName'].'">
<input type="hidden" name="id" value="'.$row['ID'].'">
<button type="submit">Add to cart</button>
</form>
';

Related

PHP MySQL Get material and quantity for an order form

I need to create an order form that has a checkbox for each product in a table and have a quantity box next to each product.
I need to process the products that have been checked and update an orders table with the checked products and the quantity entered.
I don't know how to link the product checkbox to the quantity field.
$sql="select * from tproducts";
$result=mysqli_query($con, $sql) or die(mysqli_error($con));
echo "<table>";
echo "<tr>";
echo "<th>Product Names</th>";
echo "</tr>";
if(mysqli_num_rows($result)>0)
{
echo "<form action=welcome1.php method=post>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><input type="checkbox" value="' . $row['intProductID'] . '" name="materialcode[]">' . $row['strProductName'] . "</td>";
echo "<td>Enter Quantity </td>";
echo "<td><input type='text' name='qty[]' value='qty' size=5></td>";
echo "</tr>";
}
}
echo "</table>";
echo '<input type="submit" name="submit" value="submit" />';
echo "</form>";
echo "</html>";
welcome1.php
<?php
echo "materialcode<br>";
print_r($_POST['materialcode']);
echo "<br>";
echo "qty<br>";
print_r($_POST['qty']);
?>
PHP will keep the key name you supply in the fields html.
$productIndex = 0;
while ($row = mysqli_fetch_array($result) )
{
echo "<tr>";
echo '<td><input type="checkbox" value="' . $row['intProductID'] . '" name="materialcode[$productIndex]">' . $row['strProductName'] . "</td>";
echo "<td>Enter Quantity </td>";
echo "<td><input type='text' name='qty[$productIndex]' value='qty' size=5></td>";
echo "</tr>";
$productIndex++;
}
This example uses numeric indexes but you could probably use $row['intProductId'] as well.
In your php:
$_POST['materialCode'][0] will be the analog to $_POST['qty'][0] but only the checked checkboxes will be supplied. You might end up with a $_POST['materialCode'] array that has index 0, 5, and 7 if those were the only ones checked.
So use a foreach loop instead of a for loop.
foreach( $_POST['materialCode'] as $key => $value ){
$productId = $value;
$quantity = $_POST['qty'][$key];
}

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
}

Reading and Overwriting radio button values in mysql database

I'm describing the correcting functioning of the process.
First the user posts an order, and the status initially is set to NOT APPROVED, but his manager is able to see the order request and can change the status to APPROVED, and then it is forwarded to the IT guy and he can change status to UNDER PROCUREMENT.
I am using radio buttons for gm and IT guy for changing the status. However, i am unable to change the status. Here's the code for better understanding
echo'<th>Product</th><th>Type</th><th>Quantity</th><th>Order Status</th>';
while ($row = mysqli_fetch_array($result)){
$type=$row["type"];
$make=$row["make"];
$quantity=$row["quantity"];
echo "<tr>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['make'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo '<form method="post">';
//Radio buttons used here
echo '<td><input type="radio" name="status[' . $row['order_id'] . ']" value="not approved" checked >Not Approved<br>
<input type="radio" name="status[' . $row['order_id'] . ']" value="approved">Approved</td>';
echo '</form>';
$_SESSION['radio'] = $_POST['status[]'];
$qr = "UPDATE order_info SET status = '".$_SESSION['radio']."' WHERE username = '".$_SESSION['username']."'";
$rs = mysqli_query($con3,$qr) or die(mysqli_error($con3));
}
echo "</table>";
Also, I want the radio button to be checked by default according to the status of the order.
The problem is with your input names:
echo '<td><cb><input type="radio" name="status[' . $row['order_id'] . ']" value="not approved" checked >Not Approved<br>
<input type="radio" name="status[' . $row['order_id'] . ']" value="approved">Approved</cb></td>';
echo '</form>';
You're creating an array. You can see this with this example:
<form method ='post'>
<input type='radio' name='status["25"]' value='not approved'>Not Approved
<input type='radio' name='status[25]' value='approved'>Approved
<input type='submit' value='Submit'>
</form>
<pre>
<?
session_start();
print_r($_POST);
$_SESSION['radio'] = $_POST['status'];
print_r($_SESSION);
?>
</pre>
This may have been to keep the order_id, but your update query is not specifying an order_id and will update all rows for that username. If you're only updating one row at a time, you can pass the order id with a hidden field (if you even need it?):
echo '<input type="hidden" name="order_id" value="'.$row['order_id'].'">';
Which means you're probably missing an "and order_id =" qualifier in your update.

Delete button for each table row

I manage to succesfully read and display data from my database with the following code:
http://pastebin.com/rjZfBWZX
I also generate a delete button for each row of the table :) Clicking the delete button calls "obrisi.php" which is supposed to delete that row but I messed something up :S Here's obrisi.php code:
http://pastebin.com/mrFy1i7S
I'm getting a Parse error: syntax error, unexpected 'FROM' (T_STRING) :S
Let's try and do it with _GET instead of _POST.
In your main code you need to change line 39 (the input) from:
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
to:
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
In your obrisi.php change line 3 (the id setup) from:
$id = $_POST['id'];
to:
$id = $_GET['id'];
And finally as a nice addition, redirect back to the main page by adding the following line at the end of the obrisi.php file before the closing of the php tag.
header('location:index.php');
Where index.php the name of the main page you have.
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
some simple errors here. this would output (with $id = 1):
<td><input type='submit' id= '1' . ' value='Delete' ></td>
this line should be corrected to
echo '<td><input type="submit" id="' . $id . '" value="Delete" ></td>';
this is also going wrong.
echo "<form action="obrisi.php" method="post">";
should be like:
echo '<form action="obrisi.php" method="post">';
But the main problem is that there is no field id given in the post. The id of a html element is not sent on submit. it is basically to identify that element in the HTML structure.
And when using a submit button you will have to limit the scope of the form to that row and use a hidden input field, or use a link like thanpa suggests
to clarify: if you want to do it with a post (but i would sugget using the $_GET)
while ($row = mysqli_fetch_array($result) )
{
$id = $row['id'];
echo "<tr>";
echo '<form action="obrisi.php" method="post">';
echo "<td>" . $row['Ime'] . "</td>";
echo "<td>" . $row['Prezime'] . "</td>";
echo "<td>" . $row['Grad'] . "</td>";
echo "<td>" . $row['Drzava'] . "</td>";
echo "<td>" . $row['Obavijesti'] . "</td>";
echo "<td>" . $row['Tekst'] . "</td>";
echo "<td>"
echo '<td><input type="hidden" name="id" value="' . $id . '"/><input type="submit" value="Delete" ></td>';
echo "</form>
echo "</tr>";
}
and remove the echo's for the form from start and end of script.
as an additional note here, if this is going to be at some point being used in a live system you need to be checking $id in obrisi.php that it is actually an ID and not something nasty and unexpected like more sql, look up sql injection.

Trying to get PHP checkboxes to hold data from a MySQL table

Ive got to make a web-based checkout for an assignment and have come across a problem, ive imported a database and set up the data in a table with added check boxes alongside it. I need to take the reference number (stored first in the array) onto another page using sessions. From using var_dump i cant seem to get anything from the selected from the table.
Code:
Button code
<p><tr>
<input type="submit" name="Select" id="Select" value="Add Selected To Cart"/>
</tr></p>
Access database code(values changed for saftey)
<?php
Accesses database
$con=pg_connect("host=hostname port=portnumbers
dbname=name user=user password=password");
if (!$con)
{
die('Could not connect to database');
}
?>
Database display
//Creates table
echo "<table border='1'>\n<thead>\n<tr>\n";
echo "<th>Title</th>
<th>Platform</th>
<th>Description</th>
<th>Price</th>
<th>Buy</th>\n";
while($row = pg_fetch_array($res)){
echo"<tr>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo '<td><input type="checkbox" name="selected[]" value="' . $row['0'] . '" /></td>';
echo"</tr>";
}
echo"</table>";
?>
Well, I think you can as follow because you are trying to improve a shopping car:
//Use the `$_SESSION` var to hold the values
foreach ($_POST['selected'] as $item)
$_SESSION['cart'][$item] = $item;
As the item's id will be indexed, each time a submit is performed:
If the item exists, will be replaced
If the item does not exist, will be added
In order to remove items, you should use a "view cart" page and then show the items:
foreach ($_SESSION['cart'] as $item)
echo '' . $item . '';

Categories