karir karir - 7 months ago 10
SQL Question

PHP Basket quantity variable

I am trying to create a php page where the materials from database are populated. Users should be able to enter the quantity next to the item they wish to order and I have created a qty text field for this

<?php
session_start();
include("db.php");
$pagename="Order Material";
echo "<html>";

echo "<title>".$pagename."</title>";
echo "<h2>".$pagename."</h2>";

include ("detectlogin.php");

echo "<link rel=stylesheet type=text/css href=mystylesheet.css>";



$sql="select * from material";

$result=mysqli_query($con, $sql) or die(mysqli_error($con));

echo "<table border=1>";
echo "<tr>";
echo "<th>Material Name</th>";
echo "<th>Material Description</th>";
echo "<th>Toxicity Level</th>";
echo "</tr>";

while ($arraymaterials=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$arraymaterials['materialName']."</td>";
echo "<td>".$arraymaterials['materialDescrip']."</td>";
echo "<td>".$arraymaterials['materialToxicity']."</td>";
echo "<td>Enter Quantity</td>";
echo "<td><input type=text name=qty value=qty size=5></td>";
echo "<form action=request_material.php method=post>";
echo "<input type=hidden name=materialcode value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
echo "</tr>";

}
echo "</table>";

?>


However, I cannot successfully post the value of qty on to the next page even though I have $qty=$_POST['qty']; on my request_material.php. Do you know why this value entered in the qty field cannot be posted onto the request_material.php page? Do I need a session variable?

thanks

Answer

Because input tag name="qty" is outside the form tag

echo "<td><input type=text name=qty value=qty size=5></td>";// outside form tag
echo "<form action=request_material.php method=post>";
echo "<input type=hidden name=materialcode value=" . $arraymaterials['materialCode'] . ">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";

You need to add it inside your form tag as

echo "<form action=request_material.php method=post>";
         echo "<td><input type=text name=qty value=qty size=5></td>";// add inside it 
        echo "<input type=hidden name=materialcode value=".$arraymaterials['materialCode'].">";
        echo "<td><input type=submit value='Request'></td>";
        echo "</form>";