0

我有一个难题,我正在尝试为我的代码找到解决方案。我如何做到这一点,以便当用户在 index.php 的文本框中输入给定数量时,它将将该输入数量传输到 invoice.php。我已经尝试过使用 post 方法,但它似乎不起作用:/ 一如既往,任何帮助或建议将不胜感激!我希望这很简单:(这是我的代码:

此代码包含我的产品数组,还包括我的文本框。

//The following arrays contain our products and their information, one product per array.

$hulkhamburger = array('Product' => 'Hulk Hamburger', 'Description' => '...', 'Price' => '$1.00', 'Quantity' => "<input type='text' name='quantity1'>");
$atomichotdog = array('Product' => 'Atomic Hot Dog', 'Description' => '...', 'Price' => '$2.00', 'Quantity' => "<input type='text' name='quantity2'>");
$friedchicken = array('Product' => 'Fantastic 4 Fried Chicken', 'Description' => '...', 'Price' => '$3.00', 'Quantity' => "<input type='text' name='quantity3'>");
$psyonicpizza = array('Product' => 'Psyonic Pizza', 'Description' => '...', 'Price' => '$4.00', 'Quantity' => "<input type='text' name='quantity4'>");
$marvelmeatloaf = array('Product' => 'Marvel Meatloaf', 'Description' => '...', 'Price' => '$5.00', 'Quantity' => "<input type='text' name='quantity5'>");

//The following array takes our previous five arrays and puts them into one array for easier coding and reading.

$allfood = array ($hulkhamburger, $atomichotdog, $friedchicken, $psyonicpizza, $marvelmeatloaf);


?>

索引.php

<html>
    <style>
            body{
            background-image: url('URL HERE');
            font-family: "Helvetica";
            font-size:15px;
            }

            h1{
            color:black;
            text-align:center;
            }

            p{
            font-size:15px;
            }
        </style>
        <h1>
        STORE TITLE HERE
        </h1>
    <body>       
        <form action="login.php" method="post">

            <?php
            //Include products info.inc (Which holds all our product arrays and info)
            //Credit: Tracy & Mark (THank you!)
            include 'products_info.inc';

            /*The following code centers my table on the page, makes the table background white,
             makes the table 50% of the browser window, gives it a border of 1 px,
             gives a padding of 2 px between the cell border and content, and gives 1 px of spacing between cells.
             */

            echo "<table align=center bgcolor='FFFFFF' width=50% border=1 cellpadding=1
            cellspacing=2>";

            //Credit: Tracy & Mark (THank you!)
            echo '<th>Product</th> <th>Description</th> <th>Price</th> <th>Quantity</th>';

            //The following code loops through the whole table body and then prints each row.
            for($i=0; $i<count($allfood); $i++)
                {
                //Credit: Tracy & Mark (THank you!)
                echo "<tr align=center>";
                echo "<td>{$allfood[$i]['Product']}</td>";
                echo "<td>{$allfood[$i]['Description']}</td>";
                echo "<td>{$allfood[$i]['Price']}</td>";
                echo "<td>{$allfood[$i]['Quantity']}</td>";
                echo "</tr>";
                }

            //This code ends the table.
            echo "</table>";
            echo "<br>";
            ?>
            <br><center><input type='submit' name='purchase' value='Purchase'></center>
        </form>
    </body>
</html>

这是我的 invoice.php

<html>
    <style>
        body{
            background-image: url('URL HERE');
            font-family: "Helvetica";
            font-size:15px;
            }

            h1{
            color:black;
            text-align:center;
            }

            p{
            font-size:15px;
            }
        </style>
        <h1>
        Invoice
        </h1>
</html>

<?php
//Include products info.inc (Which holds all our product arrays and info)
//Credit: Tracy & Mark (Thank you!)
include 'products_info.inc';

//Display the invoice & 'WELCOME USER. THANK YOU FOR USING THIS DAMN THING msg'

/*The following code centers my invoice table on the page, makes the table background white,
makes the table 50% of the browser window, gives it a border of 1 px,
gives a padding of 2 px between the cell border and content, and gives 1 px of spacing between cells.
*/
echo "<table align=center bgcolor='FFFFFF' width=50% border=1 cellpadding=1cellspacing=2>";
echo "<tr>";
echo "<td align=center><b>Product</b></td>";      
echo "<td align=center><b>Quantity</b></td>";
echo "<td align=center><b>Price</></td>";
echo "<td align=center><b>Extended Price</b></td>";
echo "</tr>";


 for($i=0; $i<count($allfood); $i++)
 {
     //Credit: Tracy & Mark (Thank you!)
     $qty= @$_POST['Quantity']['$i'];

    // This calculates the price if the user orders more than 1 item.
     $extendedprice = $qty*$allfood[$i]['Price'];

     echo "<tr>";
     echo "<td align=center>{$allfood[$i]['Product']}</td>";
     echo "<td align=center>$extendedprice</td>";
     echo "<td align=center>{$allfood[$i]['Price']}</td>";
     echo "</tr>";          
 }
// The goal here was to make it so that if the user selected a certain quantity from index.php, it would carry over and display on the invoice.php
if ($qty = 0)
{
    echo "please choose 1";
}
 elseif ($qty > 0) 
{
    echo $qty;
}


/*echo "<tr>";
echo "<td align=center>Subtotal</b></td>";      
echo "<td align=center></td>";      
echo "<td align=center></td>";   

echo "<tr>";
echo "<td align=center>Tax at 5.75%</td>";      
echo "<td align=center></td>";      
echo "<td align=center></td>";   

echo "<tr>";
echo "<td align=center><b>Grand total</b></td>";      
echo "<td align=center></td>";      
echo "<td align=center></td>";   
*/
echo "</table>";

?>
<br>
<center>
    <form action="index.php" method="post">
    <input type="submit" name="home" value="Back to homepage">
    </form>
</center>
4

3 回答 3

0

表单就像一个处理数据传输的容器。action 属性告诉你的数据将被发送到哪里。

给你

<form action="login.php" method="post">

这意味着表单数据将被发送到 login.php 页面。不是我想的你想要的。

然后,在目标页面(由 action 属性定义)中,您可以根据表单的方法属性获取参数值$_GET$_POST数组。

我在您给我们的代码中没有看到任何文本框。如果你添加类似的东西

<input type="text" name="my_textbox"/>

您将能够通过使用获取目标页面中文本框的值

$_POST['my_textbox']

希望这可以帮助。

顺便说一句,我注意到您的代码中有一些错误:

索引.php:

  1. 样式应该在<head>...</head>标记中
  2. 你不应该有像你的<h1>外部的html 输出<body>...</body>
  3. 表头 ( <th>) 应该在<tr>原样<td>中。

发票.php:

  1. $allfood 未声明
  2. $qty= @$_POST['Quantity']['$i'](@删除错误)更好,测试你的密钥是否存在(if isset($_POST['Quantity']) && isset($_POST['Quantity'][$i]) $qty = $_POST['Quantity'][$i];
  3. if ($qty = 0)应该是if ($qty == 0)=是做作运算符,==是比较运算符
  4. html 也是一样,所有输出都应该在<body>...</body>标记中,样式应该在 html 标头(<head>...</head>)中
于 2013-11-08T09:49:41.330 回答
0

就像在 index.php 里面的表格一样,你已经创建了表格。没有文本框、隐藏字段等表单元素。表单不会发布表值,因此您不会在 invoice.php 中获得任何已发布的值。

于 2013-11-08T09:45:22.687 回答
0

在 index.php 中,您的表单操作是“login.php”,而不是 invoice.php,因此它不会将任何内容传递给后者。

此外,在 index.php 中,没有名为“quantity”的文本框,因此 $_POST['quantity] 将一无所有。

于 2013-11-08T09:46:40.753 回答