我对PHP和网络编程相对较新。当我尝试从一个页面中选择一定数量的项目时,我遇到了一个问题,并在一个单独的PHP页面上发布了每个选择和在表格中选择的数量。从那里,我需要显示选择,选择的数量,价格和总数。如果有人能够帮助指导我正确的方向,我将非常感谢!我已经缩小了我的问题,以便当我选择每个选择的数量并单击“提交”时,它将不会处理并将我的所有选择添加到表中。提前感谢您的时间和精力!
网站03 shop.php
<?php
session_start();
include("website 03 library.php");
pagetop("WE WANT YOU TO SHOP");
echo "<span id = 'message' class = 'message'>Select How Many You Want of Each Product</span>";
$host_name = "localhost";
$user_name = "web";
$password = "programming";
$database = "assignments";
$connection = mysqli_connect($host_name, $user_name, $password, $database);
$sql = "select * from products";
$recordSet = mysqli_query($connection, $sql);
while ($record = mysqli_fetch_array ($recordSet, MYSQLI_ASSOC)) {
$pid = $record['pid'];
echo "<form action = 'website 03 cart.php' method = 'post'>";
echo "<h2 name = 'NAME$pid'>$record['name']</h2><br><br>";
echo "<img name = 'IMAGE$pid' style = 'height:280px; width:350px; ' src = '$record['url']'><br><br>";
echo "<span name = 'PRICE$pid'>Price of each $record['name'] is $record['price']. How many do you want? </span>";
echo "<select name = 'QUANTITY$pid'>";
echo "<option value = 0>0</option>";
echo "<option value = 1>1</option>";
echo "<option value = 2>2</option>";
echo "<option value = 3>3</option>";
echo "<option value = 4>4</option>";
echo "</select>";
echo "<input type = 'submit' class = 'button' name = 'SUBMIT$pid' value = 'SUBMIT'>";
echo "</form>";
};
mysqli_free_result($recordSet);
mysqli_close($connection);
?>
第二页(接收所选值)
网站03 cart.php
<?php
session_start();
include("website 03 library.php");
pagetop("SHOPPING CART");
$host_name = "localhost";
$user_name = "web";
$password = "programming";
$database = "assignments";
$connection = mysqli_connect($host_name, $user_name, $password, $database);
echo "<table border = 1>";
$sql = "select * from products";
$recordSet = mysqli_query($connection, $sql);
while ($record = mysqli_fetch_array($recordSet, MYSQLI_ASSOC)) {
$pid = $record['pid'];
$url = $record['url'];
$price = $record['price'];
$key = "QUANTITY$pid";
$quantity = $_POST['$key'];
$total = $quantity * $price;
if ($quantity > 1) {
echo "<tr>";
echo "<td><img style = 'height:110; width:150; ' src = $url></td>";
echo "<td>$quantity</td>";
echo "<td>$price</td>";
echo "<td>$total</td>";
echo "</tr>";
echo "<tr>";
echo "<td>TOTAL:</td>";
echo "<td>$total</td>";
echo "</tr>";
echo "<tr>";
echo "</tr>";
};
};
echo "</table>";
?>
我的目的是从03商店获取发布的数据并将其放在03购物车的表格中。有点像在线商店。例如,我会选择2个“时钟”和3个“自行车”,然后将数据按顺序发布到03购物车中的表格中:图像,数量,价格和总数(数量*价格)。我相信我的问题是“提交”按钮在循环内,所以它只会从一个部分发布。但是,我不知道如何使用“提交”按钮将一个帖子中的所有选项引用到网站03购物车。
答案 0 :(得分:0)
要将<form>
代码置于循环之外,请更改03 shop
while
循环,使<form>
代码位于循环之外。
另请注意,像$variables
这样复杂的$record['name']
无法嵌入双引号中,如果没有大括号,请执行以下操作:{$record['name']}
。
// print the form open
echo "<form action = 'website 03 cart.php' method = 'post'>";
// do the loop
while ($record = mysqli_fetch_array ($recordSet, MYSQLI_ASSOC)) {
$pid = $record['pid'];
// complex vars inside double quotes require special syntax
echo "<h2 name = 'NAME$pid'>{$record['name']}</h2><br><br>";
echo "<img name = 'IMAGE$pid' style = 'height:280px; width:350px; ' src = '{$record['url']}'><br><br>";
echo "<span name = 'PRICE$pid'>Price of each {$record['name']} is {$record['price']}. How many do you want? </span>";
echo "<select name = 'QUANTITY$pid'>";
echo "<option value = 0>0</option>";
echo "<option value = 1>1</option>";
echo "<option value = 2>2</option>";
echo "<option value = 3>3</option>";
echo "<option value = 4>4</option>";
echo "</select>";
echo "<input type = 'submit' class = 'button' name = 'SUBMIT' value = 'SUBMIT'>";
};
// now close the form
echo "</form>";