使用多维会话的php订单表
例如,我在主页上有此代码。
<?php
session_start();
$_SESSION['order']=array();
?>
<form name="orderform" method="post" action="e.php">
Product Catalog
<table border="1">
<tr>
<td>Product</td>
<td>Price</td>
<td>Quantity</td>
</tr>
<?
for($i=0;$i<6;$i++){
echo '<tr>';
echo '<td><input type=hidden name="product" value="'.$i.'"> Product '.$i.'</td>';
$price=rand(1,10);
echo '<td><input type=hidden name="price" value="'.$price.'">$'.$price.'</td>';
echo '<td><input type=text name="quantity"></td>';
echo '<tr>';
}
?>
</table>
<br>
<input type="submit" name="submit" value="submit">
</form>
我有一个多维会话数组 $_SESSION['order'] 我试图保存 6 个产品的订单表格及其价格和数量,以便在 POST 方法执行后可以在下一页上检索它。
即在 e.php 文件上
<?php
session_start();
$_SESSION['order'][] = array('product'=>$_POST['product'],
'price'=>$_POST['price'],
'quantity'=>$_POST['quantity']);
var_dump($_SESSION['order']);
if(count($_SESSION['order'])>0){
foreach($_SESSION['order'] as $order){
echo "<p>Product = ".$order['product']."</p>";
echo "<p>Price = ".$order['price']."</p>";
echo "<p>Quantity = ".$quantity['quantity']."</p>";
}
}
?>
但我在 e.php 上得到的结果是我只获得订单页面的最后一项,而不是其他前五个。我在这里做错了什么吗?你有什么想法?
For eg I have this code on the main page.
<?php
session_start();
$_SESSION['order']=array();
?>
<form name="orderform" method="post" action="e.php">
Product Catalog
<table border="1">
<tr>
<td>Product</td>
<td>Price</td>
<td>Quantity</td>
</tr>
<?
for($i=0;$i<6;$i++){
echo '<tr>';
echo '<td><input type=hidden name="product" value="'.$i.'"> Product '.$i.'</td>';
$price=rand(1,10);
echo '<td><input type=hidden name="price" value="'.$price.'">
I have a multidimensional Session array, $_SESSION['order'] and I'm trying to save this order form of 6 product items, along with its price and quantity, so it can be retrieved on the following page after POST method is actioned.
ie on e.php file
<?php
session_start();
$_SESSION['order'][] = array('product'=>$_POST['product'],
'price'=>$_POST['price'],
'quantity'=>$_POST['quantity']);
var_dump($_SESSION['order']);
if(count($_SESSION['order'])>0){
foreach($_SESSION['order'] as $order){
echo "<p>Product = ".$order['product']."</p>";
echo "<p>Price = ".$order['price']."</p>";
echo "<p>Quantity = ".$quantity['quantity']."</p>";
}
}
?>
But the result I'm getting on e.php is I only get the last item of the order page, but not the other previous five. Did I do something wrong here? What are your thoughts?
.$price.'</td>';
echo '<td><input type=text name="quantity"></td>';
echo '<tr>';
}
?>
</table>
<br>
<input type="submit" name="submit" value="submit">
</form>
I have a multidimensional Session array, $_SESSION['order'] and I'm trying to save this order form of 6 product items, along with its price and quantity, so it can be retrieved on the following page after POST method is actioned.
ie on e.php file
But the result I'm getting on e.php is I only get the last item of the order page, but not the other previous five. Did I do something wrong here? What are your thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用相同的
name
属性_POST
处理多个字段,因此$_POST
变量将仅包含您所指定的最终唯一名称发布。您可以为每个输入创建唯一的名称,也可以将字段作为数组发布。唯一的名称将如下所示:并像这样取出它:
.$price.'</td>'; echo '<td><input type=text name="quantity'.$i.'"></td>'; echo '<tr>'; } ?>然后您需要循环遍历 post 数组并将其添加到会话数组中:
您还可以将其作为数组发送:
并像这样取出它:
You're
_POST
ing multiple fields with the samename
property, so the$_POST
variable will contain only the final unique names that you're posting up. You can either create unique names for each input, or you can post up the fields as an array. Unique names would look like this:And get it out like this:
.$price.'</td>'; echo '<td><input type=text name="quantity'.$i.'"></td>'; echo '<tr>'; } ?>And then you'd need to loop through the post array and add it to session array:
You can also send it up as an array:
And get it out like this:
问题是您有多个同名的表单字段。您需要这样的东西:
并且
(对于生产,您还应该检查 POST 变量是否确实存在并且数组大小正确。)
The problem is you have multiple form fields with the same name. You'd need something like this:
and
(For production you should also check the POST variables if they are really present and arrays in correct size.)