如何从我的index.php到invoice.php显示变量?

时间:2013-10-12 20:44:24

标签: php html arrays forms loops

我试图制作一个页面,以便当用户选择任何漫画书的数量并按下submit时,它会将它们带到发票页面,向他们显示他们选择的漫画,数量和价格。

我尝试使用$_POST[]从我的发票中提及,但由于我是php的新手,我不知道我是否正确使用它。一如既往,任何非常感谢帮助。

这是我的代码(index.php

<!DOCTYPE html>
 <html>
     <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <title></title>
     </head>
     <body>
         <form action= "invoice.php" method="post">

 <?php /*INSERT NAME HERE This code will allow the user to purchase whatever quantity of certain comic books they want.

 */
       $productImage = array('spiderman1.jpg', 'spiderman2.jpg', 'spiderman3.jpg',  'spiderman4.jpg', 'hulk1.jpg'); // I'm stil lhaving a hard time with multidimensional arrays.
        //the product image array gave me a hard time, in the sense that I'm still having a hard time trying to output an image that saves onto my desktop locally.  So I chose the option of grabbing the links fromt the web.  Don't worry though, I won't do this for my Assignment #1.  UPDATE: Problem has been fixed!

       $description = array('Amazing Spiderman #1', 'Amazing Spiderman #15', 'Amazing Spiderman #52', 'Amazing Spiderman #107', 'Hulk #181');  
       $price = array('400', '350', '150', '300', '90');      
       /* For this code, credit goes to Kristen, she showed me how to loop the quantity. */

       for($number = 0; $number < count($allitems); $number++)
       {
           $quantity="<select name='Quantity$number'> 
                   <option value='0'>0</option>
                   <option value='1'>1</option>
                   <option value='2'>2</option>
                   <option value='3'>3</option>
                   </select>";
       }       
       $quantity="<select name='Quantity$number'>
                   <option value='0'>0</option>
                   <option value='1'>1</option>
                   <option value='2'>2</option>
                   <option value='3'>3</option>
                   </select>";       
       echo '<table border="1" align="center" cellpadding="10">'; // I'm very horrible with tables, I finally figured out how to center the text within the table.
     echo "<tr align='center'>
             <td><b>Product</b></td>
             <td><b>Description</b></td>
             <td><b>Price (each)</b></td>
             <td><b>Quantity</b></td>
           </tr>";


     foreach ($productImage as $key=>$display) // I used $key to represent the value to display my images via $display.  If you erase the $display code it would look very nice, but output a bunch of errors.
     { 
         echo "<tr align='center'>";
             echo '<td>';
             echo "<img src='".$productImage[$key]."' width='200' height='300' align='center'>";
             echo '</td>';            
             echo '<td>';
             echo $description[$key];// Description loop from the foreach()
             echo '</td>';            
             echo '<td>';
              printf('$%', $price); // for some reason if I wanted the price to be $400.00 with '$%.2f', it would print as 1.00400.  So I just kept it as whole numbers swapmeet style.
             echo $price[$key]; // This loops the price  via the foreach() function.
             echo '</td>';            
             echo '<td>';
             echo $quantity;
             echo '</td>';
       }      
             echo '<tr>';
             echo '<td>';
             echo '<td>';
             echo '<td>';
             echo '<td>';
             echo 'Add to cart '; // letting the user know what to do after they picked the quantity
             echo '<input type="submit" value="submit" name="submit">'; // my submit button
             echo '</td>';
             echo '</td>';
             echo '</td>';
             echo '</td>';
             echo '</tr>';
      echo '</table>';
     //The images under the description are linked to the websites, if the images are not shown it is due to the image being removed or renamed on the linked site (a disclaimer I borrowed from the assignment webpage.)
 ?>    
         </form>

     </body>
 </html>

这是我的invoice.php

的代码
<?php
  // a lot of trial and error and I finally got a code working so that everytime I pressed submit on the index.php page, it will direct me to the invoice.php page     

$name = $_POST['name'];
$quantity = $_POST['Quantity$number'];
$detail = $_POST['descriptoin'];
$price = $_POST['price'];

print '<table width="790" height="31" border="1" cellpadding="1"> 
                          <td align="center" width="43%"><b>Product</b></td>
                          <td width="11%"><b>Quantity</b></td>
                          <td align="center" width="13%"><b>Price</b></td>
                          <td align="center" width="54%"><b>Extended Price</b></td>
                        </tr>'; 
                        print '<tr> 
                          <td width="43%">'.$_POST[description].'</td>
                          <td align="center" width="11%">'.$quantity.'</td>
                          <td width="13%">'.$price.'</td>
                          <td width="54%">$&nbsp'.$cTotal.'</td>
                        </tr>';
                        print '<tr>
                          <td width="43%"></td>
                          <td align="center" width="11%">'.$_POST[qty].'</td>
                          <td width="13%">'.$aPrice.'</td>
                          <td width="54%">$&nbsp'.$aTotal.'</td>
                        </tr>';
                        print '<tr>
                          <td width="43%"></td>
                          <td align="center" width="11%">'.$_POST[qty].'</td>
                          <td width="13%">'.$pPrice.'</td>
                          <td width="54%">$&nbsp'.$pTotal.'</td>
                        </tr>';
                        print '<tr>
                          <td width="43%"><br>
                          </td>
                          <td align="center" width="11%">'.$_POST[qty].'</td>
                          <td width="13%">'.$sPrice.'</td>
                          <td width="54%">$&nbsp'.$sTotal.'</td>
                        </tr>';
                        print '<tr>
                          <td width="43%"><br>
                          </td>
                          <td align="center" width="11%">'.$_POST[qty].'</td>
                          <td width="13%">'.$tPrice.'</td>
                          <td width="54%">$&nbsp'.$tTotal.'</td>
                        </tr>';
                        print  '<tr>
                          <td colspan="4" width="100%">&nbsp;</td>
                        </tr>
                        <tr>
                          <td colspan="3" width="67%">Sub-total</td> 
                          <td width="54%">$&nbsp'.$subTotal.'</td> 
                        </tr>
                        <tr>
                          <td colspan="3" width="67%"> <font
                              face="arial"> Tax @ 5.75%</font></td>
                          <td width="54%">$&nbsp;'.$tax.'</td>
                        </tr>
                        <tr>
                          <td colspan="3" width="67%"> <font
                              face="arial"> Shipping </font></td>
                          <td width="54%">FREE</td>
                        </tr>
                        <tr>
                          <td colspan="3" width="67%"><b>Total</b></td>
                          <td width="54%"><b>$&nbsp;'.$Total.'</b></td>
                        </tr>';

?>

0 个答案:

没有答案