Ajax购物车不显示商品

时间:2016-04-15 10:01:18

标签: php jquery ajax

我试图制作一个非常简单的ajax购物车。到目前为止,我有购物车,基于ajax功能,我看到当我点击Add to Cart按钮时,购物车中的商品数量增加了。

现在的问题是显示购物车按钮。当我试图滑动推车时,它不会滑动。我认为问题出在PHP部分,但我在数据库中循环以获取所有ID并显示结果。至少该功能不起作用,这就是为什么我认为问题在那里。

这是PHP部分:

// counting items in cart and showing on the page - work
if(isset($_POST['total_cart_items']))
{
   echo count($_SESSION['itemid']);
   exit();
}

// post item into cart - work
if(isset($_POST['item_id']))
{

    $_SESSION['itemid'][]=$_POST['item_id'];
    echo count($_SESSION['itemid']);
    exit();
}

// this part is the problem as doesn't show the cart
if(isset($_POST['showcart']))
{
    for($i=0;$i<count($_SESSION['itemid']);$i++)
    {

        $sql = "SELECT upload_lesson_plan, upload_worksheet, upload_materials FROM document_upload where upload_id = ?"; 
        $result = $pdo->prepare($sql);
        $result->execute(array($_SESSION['itemid']));                 

        foreach ( $result as $row ){

            echo '<div class="cart_items" style="text-align:center;">
                    <a href=""><p>'.$row["itemid"][$i].'</p></a>
                  </div>';
        }
    }
    exit(); 
} 

这是ajax / js部分

$(document).ready(function(){

  $.ajax({
    type:'post',
    url:'includes/store_items.php',
    data:{
      total_cart_items:"totalitems"
    },
    success:function(response) {
      document.getElementById("total_items").value=response;
    }
  });

});

function cart(itemid)
{
    var ele=document.getElementById(itemid);    
    $.ajax({
    type:'post',
    url:'includes/store_items.php',
    data:{
      item_id:itemid        
    },
    success:function(response) {
      document.getElementById("total_items").value=response;
    }
  });

}

function show_cart()
{
  $.ajax({
  type:'post',
  url:'includes/store_items.php',
  data:{
    showcart:"cart"
  },
  success:function(response) {
    document.getElementById("mycart").innerHTML=response;
    $("#mycart").slideToggle();
  }
 });
}

上一个函数function show_cart()是问题所在。此外,如果需要HTML,但这也有效。

<p id="cart_button" style="text-align:center;" onclick="show_cart();">
    <img src="img/cart_icon.png">
    <input type="button" id="total_items" value="">
</p>

<div id="mycart" style="text-align:center;"></div>  

更新:

    for($i=0;$i<count($_SESSION['itemid']);$i++)
    {

        $sql = "SELECT * FROM document_upload where upload_id = ?"; 
        $result = $pdo->prepare($sql);
        $result->execute(array($_SESSION['itemid'])); 
        foreach ( $result as $row ):?>
          <div class="cart_items" style="text-align:center;">
               <a href=""><p><?=$row["itemid"][$i]?></p></a>
          </div>
       <?php endforeach; ?>
    }

2 个答案:

答案 0 :(得分:1)

$result是和对象包含您用来准备查询,绑定参数,执行的查询,参数......但是他包含结果......

您有不同的方法来获得结果:

$row = $result->fetch(PDO::FETCH_ASSOC); //load ONE row as array(colName => colValue)... First call: returns the first row. Second call, the second one... When there is no more rows, returns FALSE
$rows = $result->fetchAll(); //the one we use: a big array with all rows returned . We 'll do like this one...

另外,我有更好的方法来设置请求参数(对我来说更好,因为通过这种方式你可以命名你的参数):

$result->bindParam(':id', $id, PDO::PARAM_INT);

你需要这样做:

<?php
//...
// counting items in cart and showing on the page - work
if(isset($_POST['total_cart_items']))
{
   echo count($_SESSION['itemid']);
   exit();
}

// post item into cart - work
if(isset($_POST['item_id']))
{    
    $_SESSION['itemid'][]=$_POST['item_id'];
    echo count($_SESSION['itemid']);
    exit();
}

// this part is the problem as doesn't show the cart
if(! isset($_POST['showcart'])) exit; //problem
foreach ($_SESSION['itemid'] as $i):

        $sql = "SELECT * FROM document_upload where upload_id = :id"; 
        $result = $pdo->prepare($sql);
        $result->bindParam(":id", $i, PDO::PARAM_INT);
        $result->execute();                 


        $resArray = $result->fetchAll(); //return the array of results

         foreach ( $resArray as $row ):?>
             <div class="cart_items" style="text-align:center;">
                  <a href=""><p><?=$row["upload_title"]?> - <?=$row["upload_description"]?></p></a>
             </div>
        <?php endforeach;
 endforeach; ?>

更好,因为使用这种结构,你可以直接操作HTML ...

但是从SQL部分:你想只选择一些项目?不要像那样使用“for”循环!请使用以下请求:

SELECT * FROM document_upload where upload_id IN (id1, id2, id3)...

没有更多,更好的表现......

答案 1 :(得分:1)

试试这个;)

问题在于您使用将ID传递给查询的方式:

$itemsInCart = count($_SESSION['itemid']);
for($i = 0; $i < $itemsInCart; $i++){
    $sql = "SELECT upload_lesson_plan, upload_worksheet, upload_materials FROM document_upload where upload_id = ?"; 
    $result = $pdo->prepare($sql);

    /* you forgot to get current item from array using index $i */
    $result->execute(array($_SESSION['itemid'][$i]));       

    /* fetch all records */
    $records = $result->fetchAll();
    foreach ( $records as $row ){
        /* one more thing you have not selected itemid column in query so you can't get it right now you passed 3 columns;  */
        echo '<div class="cart_items" style="text-align:center;">
                <a href=""><p>' . $row["upload_lesson_plan"] . '</p></a>
              </div>';
    }
}

使用foreach循环:

foreach($_SESSION['itemid'] as $currentItemId){
    $sql = "SELECT upload_lesson_plan, upload_worksheet, upload_materials FROM document_upload where upload_id = ?"; 
    $result = $pdo->prepare($sql);

    /* you forgot to get current item from array using index $i */
    $result->execute(array($currentItemId));       

    /* fetch all records */
    $records = $result->fetchAll();
    foreach ( $records as $row ){
        /* one more thing you have not selected itemid column in query so you can't get it right now you passed 3 columns;  */
        echo '<div class="cart_items" style="text-align:center;">
                <a href=""><p>' . $row["upload_lesson_plan"] . '</p></a>
              </div>';
    }
}
相关问题