按“提交”后用户的PHP代码?

时间:2013-10-11 23:14:02

标签: php html forms

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

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

2 个答案:

答案 0 :(得分:1)

我不确定这是否能解决您的问题,但我不相信method =“$ _ post”是正确的。它应该是

method="post"

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

答案 1 :(得分:0)

你需要改变这个

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

到这个

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

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