PHP Basket quantity variable

时间:2016-04-25 09:07:23

标签: php mysql

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

2 个答案:

答案 0 :(得分:0)

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>";

答案 1 :(得分:0)

Please try this: I have updated the code:

echo "<table border=1>";
    echo "<tr>";
    echo "<th>Material Name</th>";
    echo "<th>Material Description</th>";
    echo "<th>Toxicity Level</th>";
    echo "</tr>";
    if(mysqli_num_rows($result)>0)
    {
      echo "<form action=request_material.php method=post>";
      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 "<input type=hidden name='materialcode[]' value=".$arraymaterials['materialCode'].">";
            echo "<td><input type=submit value='Request'></td>";

            echo "</tr>";

    } 
    echo "</form>";
   }  
    echo "</table>"; 

In request_material.php check value of $qty.It will be an array.for more details print_r($_POST) in request_material.php