2013-10-11 44 views
0

我开始之前,我想感谢这个网站让PHP更容易理解。所以我是PHP新手,这是我第一次处理表单。在下面的代码中,我的目标是让用户选择一个数量,然后点击提交,它将打开一张发票表,说明他们在多少和总额中选择了多少。我一直在做很多YouTube和阅读,但仍然无法掌握如何使我的页面做得令人兴奋。请记住,我并不是想让它看起来,只是很容易理解。这里是我的代码:)按“提交”后用户的PHP代码?

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title></title> 
    </head> 
    <body> 
       <form action= "<= $_server['PHP_SELF'] ?>" 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. 
     $description = array('Amazing Spiderman #1', 'Amazing Spiderman #15', 'Amazing Spiderman #52', 'Amazing Spiderman #107', 'Hulk #181'); 
     $price = array('400', '350', '150', '300', '90');  

     //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!  

    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 '<select name="service_type"><br>'; // my quantity select box, since these are very rare comics 
      echo '<option>0</option>'; // started off with 0 because if I gave this first value a 1, then all default comic quantity would be one. Users will be mad :(
      echo '<option>1</option>'; 
      echo '<option>2</option>'; 
      echo '<option>3</option>'; 
      echo '</td>'; 

     } 
      echo '<tr>'; 
      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" send="invoice.php">'; // my submit button 
      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.) 

if (array_key_exists('submit', $_POST)) // what I want to do is, if the user clicks submit, it will bring them to their invoice page. 
{ 
    echo "This is your total" . $_POST['submit']; 
} 


    ?> 


     </form> 


    </body> 
</html> 
+0

首先,您需要更改这个'<形式行动=“<= $ _server ['PHP_SELF']?>“method =”$ _ POST“>”this'

' –

+0

它工作正常!不知道它是那么简单。谢谢 – Romel

+0

不客气。很高兴为你工作。 –

回答

1

我不知道是否能解决你的问题,但我不相信,方法=“$ _职位”是正确的。它应该是

method="post" 

然后,当您需要从表单中获取数据时,请引用$ _POST数组。希望有所帮助。

+0

这样做很有道理,谢谢! – Romel

0

您需要更改这个

<form action= "<= $_server['PHP_SELF'] ?>" method="$_POST"> 

这个

<form action= "" method="post"> 

<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method="post">