简体   繁体   English

JS添加到购物车

[英]JS adding to cart

Hi i am trying to let user to add selected item into carts table. 嗨,我试图让用户将选定的项目添加到购物车表。 I have tried alot of things but so far no results. 我已经尝试了很多东西,但到目前为止没有结果。 I am using postgresql 我正在使用postgresql

here is my html file 这是我的html文件

 <form method="post" action="/products/shopping_cart">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2">
                <div class="modal-body">
                    <h5 style="color: #18BC9C;" id="productName"> <strong>{{ this.productname }} </strong></h5>

                    <img src="/{{ this.photofile }}" class="img-responsive img-centered" alt="" style="width:400px;height:450px;">

                    <p> "{{ this.description }}" </p>
                    <hr>
                    <ul class="list-inline item-details">
                        <li id="quantity">Stock:
                            <strong> {{ this.quantity }} remaining
                            </strong>
                        </li>
                        <li id="price">Price:
                            <strong> ${{ this.price }} NZD
                            </strong>
                        </li>
                        <li>Shipping:
                            <strong> ${{ this.shippingvalue }} NZD
                            </strong>
                        </li>
                        <br>
                        <li id="category">Style:
                            <strong> {{ this.category }}
                            </strong>
                        </li>
                        <li id="gender">For:
                            <strong>  {{ this.gender }}
                            </strong>
                        </li>
                    </ul>
                    <button type="submit" class="btn btn-default" style="margin-right: 20px;">Add to cart</button>

                    <button type="button" class="btn btn-default" data-dismiss="modal">Back</button>

< form> <表格>

and here is my js 这是我的js

client.query(
        'INSERT into cartstable(price,description,productname,uniqueid,  category, gender, size,   quantity, shippingValue, dateadded) Values ((SELECT price FROM productstable WHERE price=($1)),$2, $3, $4, $5, $6, $7, $8, $9,$10)', 
         [ price,description,productname,uniqueid, category, gender,size,  quantity, shippingValue,  date]
        );

I don't know at all but you can do it to use ajax 我一点都不知道,但是您可以使用ajax

Example: 例:

Create a route: 创建一条路线:

Route::post('add-to-cart/{product}', CartController@addToCart);

In cart controller: 在购物车控制器中:

public function addToCart($product){
  //if you use data as a string
  $data = $product->split(";")

  \App\Cart::create([
    'product_id' => data[0],
    'quantity' => .....,
    'user_id' => .....,\
    ..........
  ]);
}

Ajax: 阿贾克斯:

$('#addToCart').click(function(){
//you can use javascript to get data from html

var data= "";
data += product_id + ";";
//or some thing you like to store in data
//You can use data as a array

  $.ajax({
     url: "{{ url('add-to-cart') }}" + '/' + data,
     type: 'POST',
     data: {
       "_token" : {{csrf_token()}},
       "data" : data
     },
    success: function(data){
      //Do some code to update cart on html
    }

  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM