这个代码应该在我点击保存按钮后保存数据,但它没有保存,任何想法发生了什么?
脚本名称为:prueba.php
<?php
session_start();
$save=$_POST['save'];
这部分是计算两个输入的加法的函数
function calculate_price($cart)
{
$price = 0.0;
if(is_array($cart))
{
foreach($cart as $isbn => $qty)
{
$price +=$qty;
}
}
return $price;
}
这是,如果没有点击按钮保存
if(!$save)
{
$cart=array("jonathan" => 30, "andrea" => 40);
$_SESSION["cart"]=$cart;
}
这是我们点击“保存”按钮
的情况 if($save)
{
foreach($_SESSION["cart"] as $isbn => $qty)
{
echo " ISBN : ".$isbn;
if($qty=="0")
{ echo "borraste";
unset($_SESSION["cart"][$isbn]);}
else
$_SESSION["cart"][$isbn] =$qty;
}
}
这是我用来输入值的表单,因此点击“保存”后可以更改
foreach ($_SESSION["cart"] as $isbn => $qty)
{
?>
<table>
<form action ="prueba.php" method ="post">
<tr>
<td>Value of <?php echo $isbn ?></td>
<td><input type = "text" name ="<?php $isbn ?>" value ="<?php echo $_SESSION["cart"] [$isbn] ?>" size ="3"></td>
</tr>
<?php
}
?>
<tr><td><input type="submit" name="save" value="Save"></td></tr>
</form></table>
<?php
这只是为了检查会话是否存在并且数组上有值
if($_SESSION['cart']&&array_count_values($_SESSION['cart']))
{
$_SESSION["total_price"] = calculate_price($_SESSION["cart"]);
echo "The total amount is : ".$_SESSION["total_price"];
}
?>
我非常感谢您帮助我查看并查看此解决方案,我想错误可能在表单上? ,谢谢你的回答
答案 0 :(得分:0)
问题是你没有得到你的帖子值。更好的解决方案是创建分组数组名称,以便更容易管理。例如:qty[<?php echo $isbn; ?>]
。这是一个working sample:
// initial setup
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array("jonathan" => 30, "andrea" => 40);
}
if(isset($_POST['save'])) { // is button is saved
$qty = $_POST['qty']; // get the grouping array
foreach($qty as $isbn => $value) { // loop it
if(isset($_SESSION['cart'][$isbn])) { // so now you can use they keys as isbn
if($value == 0) { // if zero then unset
unset($_SESSION['cart'][$isbn]);
continue;
}
$_SESSION['cart'][$isbn] = $value; // if not then set qty
}
}
}
?>
<form method="POST" action="">
<table border="0">
<?php foreach($_SESSION['cart'] as $isbn => $value): ?>
<tr>
<td>Value of <?php echo $isbn; ?></td>
<td>
<input style="text-align: center;" type="text" name="qty[<?php echo $isbn; ?>]" value="<?php echo $value; ?>" size="3" />
</td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="2" align="right">
<input type="submit" name="save" value="Save" />
</td>
</tr>
</table>
</form>
<div class="total">
<!-- total -->
<p>The total is: <?php echo array_sum($_SESSION['cart']); ?></p>
</div>
答案 1 :(得分:0)
您的代码中的错误很少 首先,您在循环中打印表单操作,导致无效的HTML代码 其次,在会话中存储数据时,您正在使用cookie中已存在的旧值 我修改了代码并对其进行了测试
<?php
session_start();
$save=$_POST['save'];
function calculate_price($cart)
{
$price = 0.0;
if(is_array($cart))
{
foreach($cart as $isbn => $qty)
{
echo $qty . "<br>";
$price +=$qty;
}
}
return $price;
}
if(!$save)
{
$cart=array("jonathan" => 30, "andrea" => 40);
$_SESSION["cart"]=$cart;
}
if($save)
{
$cart=array("jonathan" => $_POST['jonathan'], "andrea" => $_POST['andrea']);
foreach($cart as $isbn => $qty)
{
echo " ISBN : ".$isbn;
if($qty=="0")
{ echo "borraste";
unset($_SESSION["cart"][$isbn]);}
else
{
//$cart=array($isbn => $qty);
$_SESSION["cart"][$isbn] = $qty;
echo $qty;
}
}
}
?>
<form action ="prueba.php" method ="post">
<table>
<?php
foreach ($_SESSION["cart"] as $isbn => $qty)
{
?>
<tr>
<td>Value of <?php echo $isbn ?></td>
<td><input type = "text" name ="<?php echo $isbn ?>" value ="<?php echo $_SESSION["cart"] [$isbn] ?>" size ="3"></td>
</tr>
<?php
}
?>
<tr><td><input type="submit" name="save" value="Save"></td></tr>
</table>
</form>
<?php
if($_SESSION['cart'] && array_count_values($_SESSION['cart']))
{
$_SESSION["total_price"] = calculate_price($_SESSION["cart"]);
echo "The total amount is : ".$_SESSION["total_price"];
}
?>