2016-11-30 56 views
0

我想要做的是檢查文本框輸入數量是否大於數據庫中的可用數量。應該顯示ADD按鈕的警報onclick()檢查輸入數量是否大於數據庫中的可用數量

ADD按鈕

<button type="button" name="btnSave" id="btnSave" onclick="submitdata(); resetform(); checkQty();">ADD</button> 

checkQty()功能

function checkQty() { 
    //Grab current forms input field values. 
    var txtQuantity = document.getElementById("txtQuantity").value; 
    var listItemName = document.getElementById("listItemName").value; 

    //Connect to database and verify Quantity Ordered isnt greater than Quantity In Stock. 
    $.ajax({ 
     type: "POST", 
     url: "/pms/includes/functions/qty_check.php", 
     data: 'listItemName=' + listItemName + '&txtQuantity=' + txtQuantity, 
     }).responseText;    
} 

qty_check.php

<?php 

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

//Start the Session 
if(!isset($_SESSION)) 
{ 
    session_start(); 
} 

include_once("../../config.php"); 
require __DIR__."../../dbutil.php"; 

if(!empty($_POST['txtQuantity'])){$qty = $_POST['txtQuantity'];} 
if(!empty($_POST['listItemName'])){$item = $_POST['listItemName'];} 

$results = mysqli_query($connection, "SELECT * FROM purchase_items WHERE item_id= ".$_GET['listItemName']""); 

$row = mysqli_fetch_assoc($results); 
{ 
    $tb_qty=$row["avail_qty"]; 
} 

if($tb_qty < $qty){ ?> 

<script type="text/javascript"> 
    alert("Quantity exceeds the stock limit"); 
</script> 

<?php 
} 
?> 

我嘗試了很多,但我無法解決這個問題。感謝任何幫助。

+0

究竟是什麼錯誤?是你的JavaScript功能嗎? – GraveyardQueen

+0

@GraveyardQueen它不顯示任何錯誤,但警報不按預期顯示。 – EKBG

+0

你從查詢中得到結果嗎? – GraveyardQueen

回答

1

你不應該直接從ajax調用打印html。你應該回應一些你可以在前端解析得到的信息。使用

echo json_encode(['key' => 'value']) 

這裏是你的代碼,稍作修改。我在ajax查詢中添加了dataType,並在ajax請求完成時調用了done函數。

function checkQty() { 
    //Grab current forms input field values. 
    var txtQuantity = document.getElementById("txtQuantity").value; 
    var listItemName = document.getElementById("listItemName").value; 

    //Connect to database and verify Quantity Ordered isnt greater than Quantity In Stock. 
    $.ajax({ 
    type: "POST", 
    url: "/pms/includes/functions/qty_check.php", 
    dataType: 'json', 
    data: { 
     listItemName: listItemName, 
     txtQuantity: txtQuantity 
    } 
    }).done(function(response){ 
    alert('check your console!') 
    console.log('this is the response', response.available); 
    }) 
} 

qty_check.php

<?php 

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

//Start the Session 
if(!isset($_SESSION)) 
{ 
    session_start(); 
} 

include_once("../../config.php"); 
require __DIR__."../../dbutil.php"; 

if(!empty($_POST['txtQuantity'])){$qty = $_POST['txtQuantity'];} 
if(!empty($_POST['listItemName'])){$item = $_POST['listItemName'];} 

$results = mysqli_query($connection, "SELECT * FROM purchase_items WHERE item_id= ".$_GET['listItemName']""); 

$row = mysqli_fetch_assoc($results); 
{ 
    $tb_qty=$row["avail_qty"]; 
} 
// echo out some json to send to the front end 
echo json_encode(['available' => $tb_qty < $qty]); 
?> 
+0

對不起朋友,仍然沒有顯示,也沒有錯誤。我是否需要包含json庫類的東西,如果是的話,該怎麼做?我沒有json的經驗。 – EKBG

+1

如果沒有錯誤,請檢查console.log的控制檯值。你不需要任何庫來使用json在php或javascript中。 – synthet1c

+0

我檢查瀏覽器控制檯是否有錯誤。沒有錯誤。否則,他們的任何方式來檢查你所陳述的? – EKBG