|
|
|
รบกวนชวนดูโค้ดให้หน่อยครับ ต้องการเก็บค่าในarray โดย ajax แต่มันไม่ยอมทำงาน ปล.ฝึกใช้ ajax ฮะ |
|
|
|
|
|
|
|
โค้ดที่1คือผมตั้งใจจะให้มันเก็บค่าใน array แล้วไปแสดงในตระกร้าสินค้าหนะครับ ใช้ alert เออเร่อแล้ว แต่พึ่งลองใช้ ajax เลยไม่ค่อยรู้ว่าต้องแก้ตรงไหน
Code (PHP)
<?php
session_start();
$dbcon= mysqli_connect("localhost","root","024211234","earth");
?>
<html>
<head>
<title>qweqweqweqwe</title>
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<br />
<div class="container" style="width:800px;">
<h3 align="center">MUUASDLJSK ASLDASJNCZ APDJ EE APSODJX C C AJSDKLAS</H3>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#products">Product</li>
<li> <a data-toggle ="tab" href="#cart"> Cart <span class="badge"><?php if(isset($_SESSION["shopping_cart"])){echo count($_SESSION["shopping_cart"]);} else {echo '0';}?></span> </a></li>
</ul>
<div class="tab-content">
<div id="products" class="tab-pane fde in active">
<?php
$sql="SELECT * FROM stock ORDER BY idst ASC";
$result=mysqli_query($dbcon,$sql);
while($row= mysqli_fetch_array($result))
{
?>
<div class="col-md-4">
<div class="thumbnail">
<div style="border:1px solid:#333; background-color:whit; border-radius:5px;padding:1px;margin:1px" >
<img src="img/<?php echo $row['pic']?> " width="220px" height="186px" ><br>
<h4 class="text-info"><?php echo $row["name"];?></h4>
<h4 class="text-danger">฿ <?php echo number_format($row["price"],2);?>THB</h4>
<input type="hidden" name="hidden_name" id="name<?php echo $row["idst"];?>" value="<?php echo $row["name"];?>"/>
<input type="hidden" name="hidden_price" id="price<?php echo $row["idst"];?>" value="<?php echo $row["price"];?>"/>
<input type="text" name="quanlity" id="quanlity<?php echo $row["idst"];?>"class="form-control" value="1"/>
<input type="button" name="add_to_cart" id="<?php echo $row["idst"];?>" class="btn btn-success form-control add_to_cart" value="Add to Cart"/>
</div>
</div>
</div>
<?php
}
?>
<div id="cart" class="tab-pane fade">
<div class="table-responsive" id="order_table">
<table class="table table-bordered">
<tr>
<th width ="40%">Product Name </th>
<th width ="10%">Quanlity </th>
<th width ="20%">price</th>
<th width ="15%">Total </th>
<th width ="5%">Action</th>
</tr>
<?php
if(!empty($_SESSION["shopping_cart"]))
{
$total = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
?>
<tr>
<td><?php echo $values["product_name"]; ?></td>
<td><?php echo $values["product_quanlity"];?></td>
<td align="right">$ <?php echo $values["product_price"]; ?></td>
<td align="right">$ <?php echo number_format($values["product_quanlity"] * $values["product_price"],2);?></td>
<td><button name="delete" class="btn btn-dange btn-xs delete" id="<?php echo $values["product_id"]; ?>">REMOVEEEE</button>
</tr>
<?php
$total = $total + ($values["product_quanlity"] * $values["product_price"]);
}
?>
<tr>
<td colspan="3" align="right"> Total </td>
<td align ="right"> $ <?php echo number_format($total,2);?></td>
<td></td>
</tr>
</table>
<?php
}
?>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(data){
$('.add_to_cart').click(function(){
var product_id = $(this).attr("idst");
var product_name= $('#name'+product_id).val();
var product_price = $ ('#price' + product_id).val();
var product_quanlity = $('#quanlity' + product_id).val();
var action ="add";
if(product_quanlity > 0)
{
$.ajax({
url:"action.php",
method:"POST",
dataType:"json",
data :{
product_id:product_id,
product_name:product_name,
product_price:product_price,
product_quanlity:product_quanlity,
action:action
},
success:function(data)
{
$('#order_table').html(data.order_table);
$('.badge').text(data.cart_item);
alert("prasdapsdka");
}
});
}
else
{
alert("req")
}
});
});
</script>
action.php
Code (PHP)
<?php
session_start();
$dbcon = mysqli_connect("localhost","root","024211234","earth");
if(isset($_POST["product_id"]))
{
$order_table ='';
$message= '';
if($_POST["action"] == "add")
{
if(isset($_SESSION["shopping_cart"]))
{
$is_available = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($_SESSION["shopping_cart"][$keys]['product_id'] == $_POST["product_id"])
{
$is_available++;
$_SESSION ["shopping_cart"][$keys]['product_quanlity'] = $_SESSION["shopping_cart"][$keys]['product_quanlity'] + $_POST["product_quanlity"];
}
}
if($is_available < 1 )
{
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quanlity' => $_POST["product_quanlity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
}
else
{
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quanlity' => $_POST["product_quanlity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
$order_table .='
<table class="table table-bordered">
<tr>
<th width ="40%">Product Name </th>
<th width ="10%">Quanlity </th>
<th width ="20%">price</th>
<th width ="15%">Total </th>
<th width ="5%">Action</th>
</tr>
';
if(!empty($_SESSION["shopping_cart"]))
{
$total = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
$order_table .='
<tr>
<td>'.$values["product_name"].'</td>
<td>'.$values["product_quanlity"].'</td>
<td align="right">$'.$values["product_price"].'</td>
<td align="right">$'.number_format($values["product_quanlity"]* $values["product_price"], 2).'</td>
<td><>button name="delete" class="delete" id="'.$values[product_id].'">REMOVE</button></td>
</tr>
';
$total = total +($values["product_quanlity"] * $values["product_price"]);
}
$order_table.='
<tr>
<td colspan="3" align="right"> Total </td>
<td align ="right"> $ '.number_format($total,2).'</td>
<td></td>
</tr>
';
}
$order_table .= '</table>';
$output = array (
'order_table' => $order_table,
'cart_item' => count($_SESSION["shopping_cart"])
);
echo json_encode($output);
}
?>
ปล.ผมฝึกเขียนตามคลิปเอาครับ ผิดๆถูกเลยไม่รู้จะแก้ตรงไหน รบกวนทีครับ ขอบคุณครับ
Tag : PHP
|
|
|
|
|
|
Date :
2017-05-21 04:30:53 |
By :
ret4567 |
View :
797 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ยากหน่อยนะครับ สำหรับการเรียนรู้เขียนตาม Script แล้วเขียน Code ยาวขนาดนี้ครับ
|
|
|
|
|
Date :
2017-05-23 06:26:57 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|