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


?>

index.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

フォーム内の 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 に答える
0

フォームは、データの転送を処理するコンテナーのようなものです。action 属性は、データの送信先を示します。

ここにいる

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

これは、フォーム データが login.php ページに送信されることを意味します。あなたが望むものではないと思います。

次に、ターゲット ページ (アクション属性で定義) で、フォームのメソッド属性に応じて、$_GETまたは配列でパラメーター値を取得できます。$_POST

提供されたコードにテキストボックスがありません。のようなものを追加すると

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

を使用して、ターゲット ページのテキスト ボックスの値を取得できます。

$_POST['my_textbox']

お役に立てれば。

ところで、私はあなたのコードにいくつかの間違いがあることに気づきました:

index.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 に答える