JS加入购物车

时间:2017-07-05 06:46:00

标签: javascript postgresql laravel

您好我想让用户将所选项目添加到购物车表格中。我已经尝试了很多东西,但到目前为止还没有结果。我正在使用postgresql

这是我的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>

&LT;形式&GT;

这是我的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]
        );

1 个答案:

答案 0 :(得分:0)

我根本不知道你可以使用ajax

示例:

创建路线:

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

在购物车控制器中:

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
    }

  });
});
相关问题