Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > รบกวนชวนดูโค้ดให้หน่อยครับ ต้องการเก็บค่าในarray โดย ajax แต่มันไม่ยอมทำงาน ปล.ฝึกใช้ ajax ฮะ



 

รบกวนชวนดูโค้ดให้หน่อยครับ ต้องการเก็บค่าในarray โดย ajax แต่มันไม่ยอมทำงาน ปล.ฝึกใช้ ajax ฮะ

 



Topic : 127679



โพสกระทู้ ( 6 )
บทความ ( 0 )



สถานะออฟไลน์




โค้ดที่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">฿&nbsp;<?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







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2017-05-21 04:30:53 By : ret4567 View : 735 Reply : 1
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ยากหน่อยนะครับ สำหรับการเรียนรู้เขียนตาม Script แล้วเขียน Code ยาวขนาดนี้ครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-05-23 06:26:57 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : รบกวนชวนดูโค้ดให้หน่อยครับ ต้องการเก็บค่าในarray โดย ajax แต่มันไม่ยอมทำงาน ปล.ฝึกใช้ ajax ฮะ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่