  | 
              
	              
	                
  
    
	 
        **สอบถาม** เรื่อง การใช้งาน Javascript Alert ในหน้าที่ถูก Ajax เรียกครับ     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                คือผมทำ หน้าสั่งซื้อสินค้าเอาไว้ครับ แล้วใช้ ajax ช่วยในการบันทึกรายละเอียดสินค้า  
ปัญหา คือผมสั่ง Javascript Alert ในหน้าที่ ajex ดึงมา แล้วไม่ทำงาน ไม่ทราบว่าพอจะมีวิธีใด ที่ทำให้ มัน alert ได้ไหมครับ 
โค๊ดอยู่ด้านล่าง ผมตัดส่วนที่ ไม่จำเป็นออกไปแล้ว น่ะครับ 
 
นี้คือโค๊ดหนาที่สั่งดึง ajax ครับ 
Code (PHP) 
<?php
session_start();
include("../Connect_DB.php");
?>
<head>
<title>DASADA</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="styles.css" />
<link href="../buttonCss.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
function AddOrderPart(PartID,Amount)
{
	//Ajax sale
	HttPRequest = false;
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari,...
	 HttPRequest = new XMLHttpRequest();
	 if (HttPRequest.overrideMimeType) 
	 {
		HttPRequest.overrideMimeType('text/html');
	 }
	} 
	else if (window.ActiveXObject) { // IE
	 try {
		HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
	} 
	
	if (!HttPRequest) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
	}
	
	var url = 'Admin_order_parts_detail.php';
	var pmeters = "Action=ADD&PartID=" + PartID+"&Amount=" + Amount;
	//alert(pmeters)
	HttPRequest.open('POST',url,true);
	
	HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	HttPRequest.setRequestHeader("Content-length", pmeters.length);
	HttPRequest.setRequestHeader("Connection", "close");
	HttPRequest.send(pmeters);
	
	
	HttPRequest.onreadystatechange = function()
	{
	 if(HttPRequest.readyState == 3)  // Loading Request
	  {
	  	document.getElementById("addpart").innerHTML = "Now is Loading...";
	  }
	 if(HttPRequest.readyState == 4) // Return Request
	  {	
	  	document.getElementById('selpart').value = '0';
		document.getElementById('selamount').value = '0';	  
		document.getElementById('addpart').innerHTML = HttPRequest.responseText;
	  }				
	}
}
</script>
</head>
<body onLoad="AddOrderPart('','')">
<div id="wrapper">
  <div id="header"><h1>DASADA</a></h1>
  <h3>ระบบการจัดการงานซ่อมบำรุงน้ำพุ</h3>
<div class="hder"><font size="+1">คุณ <?php echo $_SESSION['NameEmp']; ?></font><br>
    <font size="+1">สถานะ <?php echo $_SESSION['logintype']; ?>  </font>
    <input type="button" value="Logout" id="button" class="small awesome" onClick="window.location = '../logout.php'" style="cursor:pointer">
  </div>
   </div>
  <div id="navigation">
  
  <link rel="stylesheet" href="menu.css" type="text/css" />
<div id="foxmenucontainer">
  <div id="foxmenu">
    <ul>
       <li><a href="H_Admin.php" ><span>หน้าหลัก</span></a></li>
      <li><a href="Admin_show_maintenance.php"><span>การซ่อมบำรุง</span></a></li>
      <li><a href="Admin_repair.php" ><span>แจ้งซ่อม</span></a></li>
      <li><a href="Admin_confirm.php"><span>อนุมัติ</span></a></li>
      <li><a href="Admin_parts.php" class="current"><span>อะไหล่</span></a></li>
      <li><a href="Admin_Statistic.php"><span>สถิติ</span></a></li>
       <li><a href="Admin_H_Report.php "><span>รายงาน</span></a></li>
      <li><a href="Admin_Map.php"><span>แผนที่</span></a></li>
      <li><a href="Admin_employee "><span>ข้อมูลพนักงาน</span></a></li>
    
    </ul>
  </div>
</div>
  </div>
  
  <div id="content"><div id="line"></div>
  <table>
	<tr>
      <td>อะไหล่ที่สั่ง</td>
      <td>
      <select name="selpart" id="selpart">
      <option value="0">กรุณาเลือก</option>
      <?php
	 	$selpart_sql = "select * from tblpart";
		$query_part = mysql_query($selpart_sql);
		while($part = mysql_fetch_array($query_part))
		{
		?>
        <option value="<?php echo $part['PartID']; ?>"><?php echo $part['Name']; ?></option>
        <?php  
		}
	  ?>
        </select>
        </td>
      <td>จำนวน</td>
      <td>
      <select name="selamount" id="selamount">
      <option value="0" selected="selected">กรุณาเลือก</option>
      <?php
	  for($i=1;$i<=15;$i++)
	  {
		?>
        <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
        <?php  
	  }
	  ?>
      </select></td>
      <td><input type="button" value="สั่งซื้อ" onClick="chkOrder();" ></td>
  	</tr>
  </table>
  <span id=addpart>
  
  </span>
  </div>
  <div id="footer"><center>Dasada The Flower Es'senses Resort <p>ดาษดา เดอะ ฟลาวเวอร์ เอสเซนซ์ รีสอร์ท-ปราจีนบุรี</center></div>
 </div>
</body>
</html>
 
 
นี้ค่อหน้าที่ดึงมา  
 
Code (PHP) 
if(isset($_POST['Action']) && $_POST['Action'] == 'ADD')
{
	$OrderTotal = 0;
	$PartID = $_POST['PartID'];
	$QtyP = $_POST['Amount'];
	//////////////////////////หาว่าสั่งเพิ่มไหม///////////////////////////////////
	$sel_OrderPart = "select * from tblorderpartdetail where PartID = '$PartID' && OrderID = '$GenOPID'"; 
	$query_OrderPart = mysql_query($sel_OrderPart);
	$numrow_OrderPart = mysql_num_rows($query_OrderPart);
	$orderpart = mysql_fetch_array($query_OrderPart);
	$oldQty = $orderpart['Qty'];
	////////////////////////////////////////////////////////////////
	$sel_part = "select * from tblpart where PartID = '$PartID'";
	$query_part = mysql_query($sel_part);
	$resultPart = mysql_fetch_array($query_part);
	$PartPrice = $resultPart['Price'];
	$PartStock = $resultPart['Stock'];
	//////////////////////////////////////////////////////////////
	if($numrow_OrderPart > 0)//หาว่าเคยสั่งไหม
	{
		$newQty = $oldQty+$QtyP;
		$StockLimit = $PartStock + $newQty;
		if($StockLimit < 15)
		{
			$OrderPrice = $PartPrice*$newQty;
			$Update_OrderPart = "update tblorderpartdetail set Qty = $newQty,Total = $OrderPrice
			where PartID = '$PartID' && OrderID = '$GenOPID'";
			mysql_query($Update_OrderPart);
		}
		else
		{
			echo "<script language='javascript' type='text/javascript'>alert('ของที่สั่งเต็มสต๊อกแล้ว ไม่สามารถสั่งเพิ่มได้'); </script>";//ตรงนี้ คือส่วนที่ต้องการให้มัน alert
		}
	}
	else
	{
		$StockLimit = $PartStock + $QtyP;
		if($StockLimit < 15)
		{
			$OrderPrice = $PartPrice*$QtyP;
			$insert_OrderPart = "insert into tblorderpartdetail(PartID,OrderID,Price,Qty,Total) 
			value('$PartID','$GenOPID',$PartPrice,$QtyP,$OrderPrice)";
			mysql_query($insert_OrderPart);
		}
		else
		{
			echo "<script language='javascript' type='text/javascript'>alert('ของที่สั่งเต็มสต๊อกแล้ว ไม่สามารถสั่งเพิ่มได้'); </script>";//ตรงนี้ คือส่วนที่ต้องการให้มัน alert
		}
	}
	
}	
$sel_OrderDetail = "select * from tblorderpartdetail where OrderID = '$GenOPID' order by PartID";
$query_OrderDetail = mysql_query($sel_OrderDetail);
$Numrow_OrderDetail = mysql_num_rows($query_OrderDetail);
?>
<!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>
<form id="frmsaveorder" name="frmsaveorder" method="post" action="../SaveAll.php?Action=SaveOrder">
<input name="txtorderID" type="hidden" id="txtorderID" value="<?php echo $GenOPID; ?>" />
<div align="right">รหัสใบสั่งซื้อ : <?php echo $GenOPID; ?><br>วันที่สั่งซื้อ : <?php echo showdaterevthai(date("Y-m-d")); ?></div>
  <table width="100%" border="1" cellspacing="0" cellpadding="0">
    <tr>
      <td colspan="6" align="center" bgcolor="#999999">ตารางอะไหล่ที่ต้องการสั่งซื้อ</td>
    </tr>
    <tr>
      <td width="2%" align="center" bgcolor="#CCCCCC">ลบ</td>
      <td width="15%" align="center" bgcolor="#CCCCCC">รหัสอะไหล่</td>
      <td width="38%" align="center" bgcolor="#CCCCCC">ชื่ออะไหล่</td>
      <td width="13%" align="center" bgcolor="#CCCCCC">จำนวนที่สั่งซื้อ</td>
      <td width="15%" align="center" bgcolor="#CCCCCC">ราคา</td>
      <td width="17%" align="center" bgcolor="#CCCCCC"><p>รวม</p></td>
    </tr>
      <?php
	   $granttotal = 0;
	  while($order = mysql_fetch_array($query_OrderDetail))
	  {
		$sel_part1 = "select * from tblpart where PartID = '".$order['PartID']."'";
		$query_part1 = mysql_query($sel_part1);
		$resultPart1 = mysql_fetch_array($query_part1);
		$PartName = $resultPart1['Name'];
		$Total = $order['Price'] * $order['Qty'];
		$granttotal += $Total;
		$grantQty += $order['Qty'];
	  ?>
    <tr>
      <td align="center"><input type="button" value="ลบ" onclick="DelOrderPart('<?php echo $order['ID']; ?>')" /></td>
      <td align="center"><?php echo $order['PartID']; ?></td>
      <td align="left"><?php echo $PartName; ?></td>
      <td align="center"><?php echo $order['Qty']; ?></td>
      <td align="right"><?php echo number_format($order['Price'],0); ?></td>
      <td align="right"><?php echo number_format($Total,0); ?></td>
    </tr>
    <?php
	  }
	?>
    <tr>
      <td colspan="5" align="right">รวมทั้งหมด</td>
      <td align="right"><?php echo number_format($granttotal,0); ?></td>
    </tr>
    <tr>
      <td colspan="6" align="center"><input name="txtgranttotal" type="hidden" id="txtgranttotal" value="<?php echo $granttotal; ?>" />
      <input type="button" name="button" id="button" onclick="check_saveorder()" value="สั่งซื้อ"/></td>
    </tr>
  </table>
  <input name="checkorder" type="text" id="checkorder" style="visibility:hidden" />
</form>
</body>
</html>
 
 
  Tag : PHP, HTML/CSS, JavaScript, Ajax               
                        | 
           
          
            | 
			
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2016-09-26 14:53:07 | 
                      By :
                          nutzaaclub | 
                      View :
                          1196 | 
                      Reply :
                          3 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ลองให้มัน Return กลับไปครับ เช่น 
 
ส่วนนี้คือหน้าที่ดึงมา  
Code (PHP) 
	//////////////////////////////////////////////////////////////
	if($numrow_OrderPart > 0)//หาว่าเคยสั่งไหม
	{
		$newQty = $oldQty+$QtyP;
		$StockLimit = $PartStock + $newQty;
		if($StockLimit < 15)
		{
			$OrderPrice = $PartPrice*$newQty;
			$Update_OrderPart = "update tblorderpartdetail set Qty = $newQty,Total = $OrderPrice
			where PartID = '$PartID' && OrderID = '$GenOPID'";
			mysql_query($Update_OrderPart);
		}
		else
		{
			echo "alert";//ตรงนี้ คือส่วนที่ต้องการให้มัน alert
                        exit();
		}
	}
	else
	{
		$StockLimit = $PartStock + $QtyP;
		if($StockLimit < 15)
		{
			$OrderPrice = $PartPrice*$QtyP;
			$insert_OrderPart = "insert into tblorderpartdetail(PartID,OrderID,Price,Qty,Total) 
			value('$PartID','$GenOPID',$PartPrice,$QtyP,$OrderPrice)";
			mysql_query($insert_OrderPart);
		}
		else
		{
			echo "alert";//ตรงนี้ คือส่วนที่ต้องการให้มัน alert
                        exit();
		}
	}
 
 
โค๊ดหนาที่สั่งดึง ajax ครับ 
Code (PHP) 
	HttPRequest.onreadystatechange = function()
	{
	 if(HttPRequest.readyState == 3)  // Loading Request
	  {
	  	document.getElementById("addpart").innerHTML = "Now is Loading...";
	  }
	 if(HttPRequest.readyState == 4) // Return Request
	  {	
                var response = HttPRequest.responseText;
                if(response.Trim()=="alert"){
                     alert('ของที่สั่งเต็มสต๊อกแล้ว ไม่สามารถสั่งเพิ่มได้');
                 }
                 else{
	  	      document.getElementById('selpart').value = '0';
		      document.getElementById('selamount').value = '0';	  
		      document.getElementById('addpart').innerHTML = response;
                 }
	  }				
	}
                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2016-09-26 16:43:56 2016-09-26 16:44:50 2016-09-26 16:45:39 2016-09-26 16:46:09              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2016-09-26 16:41:22 | 
                        By :
                            popfanta | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 jQuery น่าจะดีกว่านะครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2016-09-27 11:25:00 | 
                        By :
                            Krungsri | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |