在开始之前,我要感谢这个网站让 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>