doulu1968 2015-09-11 19:40
浏览 22

尝试使用Javascript Php和Ajax更新购物车

I am trying to select an item from an array to push into the shopping cart via an ajax call but my JS skills are a little weak and need some pointers;

Heres ht js

$(document).on('click', 'button', function() {
var element = $(this).closest('.items');
var image = element.find('img')[0].src;
var name = element.find('input').val();
var price=document.find('input').val();

$.ajax({
    type:'post',
    url:'store_items.php',
    data:{
      item_src:img_src,
      item_name:name,
      item_price:price
    },
    success:function(response) {
      document.getElementById("total_items").value=response;
    }
  });

}

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

}

This is the while loop to spit out the list of products

    while ($row = mysqli_fetch_array($r)) {

    echo '
    <div class="items" id="item">
    <a href="doc_view.php?prod_id='.$row["prod_id"].'"> '.$row["prod_name"]. '</a>';

    echo "
    <div class='product_display'>
    <p><img src='../admin/uploads/".$row['file_upload']."'></p>";    


   echo"
    <input type='button' value='Add To CART' id='cart_button' onclick='cart(\"item\");' "; 

    echo'
    <p>Price - '.$row["prod_price"]. '</p>
    <input type="hidden" class="item_name" value="'.$row["prod_name"]. '">
    <input type="hidden" class="item_price" value="'.$row["prod_price"]. '">';?>

    <?php echo'
    </div>
    </div>
    ';


    } 

I am struggling to get the products to show in the cart - I display it like so:

$(document).ready(function(){

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

});


<p id="cart_button" onclick="show_cart();">
  <input type="button" id="total_items" value="">
</p>

<div id="mycart">
</div>

<a href="../index.php">home</a>
<div id="item_div">
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)