Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 循环数组,一个按钮中有2个字段_Javascript_Php_Jquery_Arrays_Forms - Fatal编程技术网

Javascript 循环数组,一个按钮中有2个字段

Javascript 循环数组,一个按钮中有2个字段,javascript,php,jquery,arrays,forms,Javascript,Php,Jquery,Arrays,Forms,我已经搜索了一些答案,找到了一个,但没有真正说的代码,但逻辑,这是很好的。但我还没有做对,我已经尝试了很多次 所以它是购物车,我把我的项目放入一个会话变量(数组)。在我的购物车页面中,我每行有2个表单(编辑数量和删除项目)。我想做的是把整个按钮变成一个 因此,当更改任何数量字段(或选中“删除项目”框)时,如果我单击一个提交按钮,它将更新所有内容,而不是每个操作一个表单 ***这是我的原始代码:(***编辑请参见文章的最后3个表单) 我尝试(其中之一)做的是将CartoOutput循环变成一个表单

我已经搜索了一些答案,找到了一个,但没有真正说的代码,但逻辑,这是很好的。但我还没有做对,我已经尝试了很多次

所以它是购物车,我把我的项目放入一个会话变量(数组)。在我的购物车页面中,我每行有2个表单(编辑数量和删除项目)。我想做的是把整个按钮变成一个

因此,当更改任何数量字段(或选中“删除项目”框)时,如果我单击一个提交按钮,它将更新所有内容,而不是每个操作一个表单

***这是我的原始代码:(***编辑请参见文章的最后3个表单)

我尝试(其中之一)做的是将CartoOutput循环变成一个表单,有一个提交按钮。。。然后,每个字段都将是一个数组。我认为可以这样做,但我做不到,我将字段更改为“quantity[]”和“item_to_adjust[]”以获得数组输入,并编辑了php操作,添加了另一个for循环

以下是我的代码:

foreach ($_SESSION["cart_array"] as $each_item) { 
          $i++;
          while (list($key, $value) = each($each_item)) {
                  if ($key == "item_id" && $value == $item_to_adjust) {
                      foreach($cart_items_new as $item_id => $quantity) {
                      // That item is in cart already so let's adjust its quantity using array_splice()
                      array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
                      }
                  } // close if condition
          } // close while loop
} // close foreach loop
是的,它不工作,它提交,但什么也不做,请原谅我的混乱

编辑: (当用户添加新购物车项目时)

if(isset($\u POST['pid'])){
$pid=$_POST['pid'];
$wasFound=false;
$i=0;
//如果未设置购物车会话变量或购物车数组为空
如果(!isset($_会话[“cart_数组])|计数($_会话[“cart_数组])<1){
//如果购物车为空或未设置,则运行
$\会话[“购物车数组”]=数组(0=>数组(“项目id=>pid,“数量”=>1));
}否则{
//如果购物车中至少有一个项目,则运行
foreach($\会话[“购物车数组”]作为$each项目){
$i++;
while(列表($key,$value)=每个($each_项)){
如果($key==“item_id”&&$value==$pid){
//该商品已经在购物车中,所以让我们使用array_splice()调整其数量
数组拼接($会话[“购物车数组”],$i-1,1,数组(数组(“项目id=>$pid,“数量”=>$each_item['quantity']+1));
$wasFound=true;
}//如果条件为,则关闭
}//关闭while循环
}//关闭foreach循环
如果($wasFound==false){
数组推送($\会话[“购物车数组”],数组(“项目id=>pid,“数量”=>1));
}
}
标题(“位置:cart.php”);
退出();
}
(查看和渲染购物车,我只包括编辑和删除部分)

if(!isset($_SESSION[“cart_array”])| | count($_SESSION[“cart_array”])<1){
$cartOutput=“您的购物车是空的
”; }否则{ foreach($\会话[“购物车数组”]作为$each项目){ $item_id=$each_item['item_id']; $sql=mysql\u query(“从产品中选择*,其中id='$item\u id'限制1”); while($row=mysql\u fetch\u数组($sql)){ $product_name=$row[“product_name”]; $price=$row[“price”]; $details=$row[“details”]; $quantity=$row['quantity']; } //编辑 $cartOutput.=' '; //删除 $cartOutput.=' ';
提交后,会发生以下情况:

        if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
    // execute some code
    $item_to_adjust = $_POST['item_to_adjust'];
    $quantity = $_POST['quantity'];
    $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
    if ($quantity >= 100) { $quantity = 99; }
    if ($quantity < 1) { $quantity = 1; }
    if ($quantity == "") { $quantity = 1; }
    $i = 0;
    foreach ($_SESSION["cart_array"] as $each_item) { 
              $i++;
              while (list($key, $value) = each($each_item)) {
                  if ($key == "item_id" && $value == $item_to_adjust) {
                      // That item is in cart already so let's adjust its quantity using array_splice()
                      array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
                  } // close if condition
              } // close while loop
    } // close foreach loop
}
if(设置($\u POST['item\u to\u adjust'])和&$\u POST['item\u to\u adjust'])!=“”){
//执行一些代码
$item_to_adjust=$_POST['item_to_adjust'];
$quantity=$_POST['quantity'];
$quantity=preg_replace(“#[^0-9]#i”,“$quantity);//过滤除数字以外的所有内容
如果($quantity>=100){$quantity=99;}
如果($quantity<1){$quantity=1;}
如果($quantity==“”){$quantity=1;}
$i=0;
foreach($\会话[“购物车数组”]作为$each项目){
$i++;
while(列表($key,$value)=每个($each_项)){
如果($key==“item\u id”&&$value==$item\u to\u adjust){
//该商品已经在购物车中,所以让我们使用array_splice()调整其数量
数组拼接($会话[“购物车数组”],$i-1,1,数组(数组(“项目id=>$item\u to_adjust”,“数量”=>$quantity));
}//如果条件为,则关闭
}//关闭while循环
}//关闭foreach循环
}
会话的结构 将项目存储在按项目id索引的会话中

$_SESSION['cart_array'][ $itemId ] = $quantity;
您可以这样保存项目,这使得访问购物车中的项目更加麻烦

$_SESSION['cart_array'][0] [ 'item_id' ] = $itemId;
$_SESSION['cart_array'][0] [ 'quantity' ] = $quantity;
建立你的形式 使您的购物车成为一个表单。 对于每个条目,按如下方式创建一个输入字段

print '<form action="cart.php" method="post">';
foreach ($_SESSION['cart_array'] as $itemId => $quantity) {
  // query product information from database here

  // build form row with one input field for each entry in the cart
  print '<tr>';
  print '<td> add product information here </td>'
  print '<td><input name="quantity['.$itemId.']" value="'.$quantity.'"></td>';
  print '</tr>';
}
print '</form>';
向表单中添加删除按钮
如果其余的工作正常,您可以添加删除按钮。这些按钮必须通过javascript将相应输入字段的值设置为0,然后提交表单。

这只是一个想法:创建一个新的提交表单。使用jQuery捕获表单提交,并停止它做任何事情(默认设置)。接下来使用.submit()使函数提交所有其他表单我不能让它工作,我也调整了名称,使其符合我的代码,但提交后没有错误,但什么也没有发生。请问您是否可以调整名称,使其符合我的代码。如果您需要,我可以发布我的代码的其他信息。您可以在提交后发布会话转储和post数组吗?对不起,我已经编辑了我的answer但是忘了在评论中回复,谢谢。嗯,你好?有人帮忙吗?我编辑了我的答案以使我的观点更清楚。希望它能让你更容易理解这个方法。
$_SESSION['cart_array'][0] [ 'item_id' ] = $itemId;
$_SESSION['cart_array'][0] [ 'quantity' ] = $quantity;
print '<form action="cart.php" method="post">';
foreach ($_SESSION['cart_array'] as $itemId => $quantity) {
  // query product information from database here

  // build form row with one input field for each entry in the cart
  print '<tr>';
  print '<td> add product information here </td>'
  print '<td><input name="quantity['.$itemId.']" value="'.$quantity.'"></td>';
  print '</tr>';
}
print '</form>';
// check if cart already exists. if not, init it.
if (!isset(_SESSION['cart_array']) {
   $_SESSION['cart_array'] = array();
// otherwise save itemIds and their quantities
} else {
  foreach ($_POST['quantity'] as $itemId => $itemQuantity) {
    // if the given quantity is 0 remove the item from the cart
    if ($itemQuantity == 0) {
      unset($_SESSION['cart_array'][ $itemId ])
    // otherwise save or update the quantity for the given item
    } else {
      $_SESSION['cart_array'][ $itemId ] ['quantity'] = $itemQuantity; 
    }
  }
}