2013-10-11 39 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">