|  |  | 
          
            |  
 หน้าสั่งซื้อ เมื่อใส่เลขสมาชิกแล้ว เพิ่มสินค้าเข้าไปได้เลยโดยใส่รหัสสินค้า โดยไม่รีเฟรชหน้า
 Code (PHP)
 
 <?
include "include/config.php";
$idname=$_POST['idname'];
if($idname>0){
$sql="SELECT * from customer WHERE cus_id='$idname'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser1.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="ajax2.php">
  <table width="762" border="0">
    <tr>
      <td width="300">รหัสลูกค้า:
        <label for="idname"></label>
      <input type="text" name="idname" id="idname" /> <input type="submit" name="submit" id="submit" value="ค้นหา"/></td>
      <td width="169">ชื่อ:<input name='name' type='text' value='<?=$row['cus_name'];?>'>
      </td>
      <td width="279">นามสกุล:<input name='lastname' type='text' value='<?=$row['cus_lastname'];?>'>
        </td>
    </tr>
  </table>
</form>
<br>
<form>
รหัสสินค้า:
<input name="users" onChange="showUser(this.value)" id="users">
<br>
<br>
<div id="txtHint"><b></b></div>
</body>
</html>
 นี่เป็นหน้าที่เรียกจากฟังก์ชั่น ajax
 Code (PHP)
 
 <?
include "include/config.php";
$q=$_GET["q"];
if($q==""){
	echo '<script>alert("กรุณากรอกข้อมูล");</script>';;
}else{
	$sql="SELECT * FROM product WHERE pro_id='".$q."'";
	$result = mysql_query($sql);
		if($row=mysql_fetch_array($result)){
			$proid=$row['pro_id'];
 			$proname=$row['pro_name'];
			$proqty=$row['pro_qty'];?><br><?
			$proprice=$row['pro_price'];
			$propoint=$row['pro_point'];
				$sql1="INSERT INTO `basket` ( `id` , `bas_id` ,`bas_name`, `bas_qty` , `bas_price` , `bas_point` )
				VALUES (
				'0', '$proid','$proname','1', '$proprice', '$propoint'
				)";
				if($result1=mysql_query($sql1)){
					$newqty=$proqty-1;
					$sql6="UPDATE product SET pro_qty='$newqty' WHERE product.pro_id='$proid'";
					/*$result6=mysql_query($sql6);*/
					}
		}else{
			$sql2="SELECT * FROM service WHERE ser_id='".$q."' ";
			$result2=mysql_query($sql2);
				if($row2=mysql_fetch_array($result2)){
				$serid=$row2['ser_id'];
				$sername=$row2['ser_name'];
				$serprice=$row2['ser_price'];
				$serpoint=$row2['ser_point'];
					$sql4="INSERT INTO `basket` ( `id` , `bas_id` ,`bas_name`, `bas_qty` , `bas_price` , `bas_point` )
						VALUES (
						'0', '$serid','$sername','1', '$serprice', '$serpoint'
						)";
						 $row4=mysql_query($sql4);
				}else{
					echo '<script>alert("ไม่สามารถเพิ่มสินค้า/บริการได้");</script>';;
					}
		}
}
	
$sql5="SELECT * FROM basket";
$result5=mysql_query($sql5);
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p><? echo "<table border='0'>
<tr>
<th align='center' bgcolor='#FFCCCC' width='90'>รายการที่</th>
<th align='center' bgcolor='#FFCCCC' width='90'>รหัสสินค้า</th>
<th align='center' bgcolor='#FFCCCC' width='450'>ชื่อสินค้า</th>
<th align='center' bgcolor='#FFCCCC' width='90'>จำนวน</th>
<th align='center' bgcolor='#FFCCCC' width='90'>ราคา</th>
<th align='center' bgcolor='#FFCCCC' width='90'>แต้ม</th>
<th align='center' bgcolor='#FFCCCC' width='90'>รวม</th>
<th align='center' bgcolor='#FFCCCC' width='90'>ลบ</th>
</tr>";
$total=0;
$sumtotal=0;
$totalpoint=0;
$sumtotalpoint=0;
$count =1;
while($row5 = mysql_fetch_array($result5))
  {
$total=$row5['bas_qty']*$row5['bas_price'];
$totalpoint=$row5['bas_qty']*$row5['bas_point'];
$sumtotal=$sumtotal+$total;
$sumtotalpoint=$sumtotalpoint+$totalpoint;
  echo "<tr>";
  echo "<td align='center'>" . $count . "</td>";
  echo "<td align='center'>" . $row5['bas_id'] . "</td>";
  echo "<td align='center'>" . $row5['bas_name'] . "</td>";
  echo "<td align='center'>".$row5['bas_qty']."</td>";
  echo "<td align='center'>" . $row5['bas_price'] . "</td>";
  echo "<td align='center'>" ?>
  <?=number_format($totalpoint,2);?><? "</td>";
  echo "<td align='center'>"?><?=number_format($total,2);?><? "</td>";
  echo "<form>";
  echo "<td align='center'><input type='submit' name='submit' id='submit' value='ลบ'></td></form>";
  
		
  echo "</tr>";
  $count++;
  }
echo "</table>";
?><br> 
  รวมทั้งหมด<?=number_format($sumtotal,2);?>
  <br>รวมแต้ม<?=number_format($sumtotalpoint,2);?> <?
?>
</body>
</html>
 ขอความกรุณาด้วยนะค่ะ พอดีติดตรงส่วนนี้จริงๆค่ะ รบกวนผู้รู้ด้วยค่ะ
 
 
 
 Tag : PHP, JavaScript, Ajax
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2012-09-13 23:18:42 | By :
                          nookeybear | View :
                          2296 | Reply :
                          1 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |