如果值存在则显示模式,如果不存在则显示错误消息

时间:2018-09-13 06:37:06

标签: jquery ajax pdo

切换并不是确切的术语,但是,我想先检查值是否存在,然后显示我的模态,如果不存在,则模态一定不能显示,而只是显示一条PHP错误消息。

现在发生的是,如果该值不存在,将显示模式,但是当我刷新当前页面时,将显示错误消息。 这是我的Ajax jQuery。

 $(function(){
  $(document).on('click', '.cScanBtn', function(e){
    e.preventDefault();
    var inputQr = $('#scanQr').val();
    //console.log(inputQr);
    $('#inputBarcode').val(inputQr);
    location.reload();

      $.ajax({
        type: 'POST',
        url: 'display_item.php',
        data: {
          inputQr:inputQr
        },
        success: function(result){
        //console.log(result);
        }
      });  
      $('#viewItem').modal('show');
  }); 
});

这是我的Ajax文件

<?php 

include 'includes/session.php';

$qr_code = $_POST['inputQr'];

$conn = $pdo->open();

    $checkQr = $conn->prepare("SELECT *, COUNT(*) AS checkrows FROM product WHERE qr_code=:qr_code");

    $checkQr->execute(['qr_code'=>$qr_code]);

    $qr = $checkQr->fetch();

    if($qr['checkrows'] <= 0){  

        $_SESSION['error'] = 'Barcode doesn\'t exist! Kindly review your input!';

    }   

$pdo->close();

?>

1 个答案:

答案 0 :(得分:0)

我认为您必须改变自己的逻辑,

while ((line = Readed.ReadLine()) != null)
{
    if (line.Contains("data") )
    {
             // DO WHAT? 
    }
}

现在,在PHP中更改代码,

 $(function(){
  $(document).on('click', '.cScanBtn', function(e){
    e.preventDefault();
    var inputQr = $('#scanQr').val();
    //console.log(inputQr);
    $('#inputBarcode').val(inputQr);
    //location.reload(); // not required.
      $.ajax({
        type: 'POST',
        url: 'display_item.php',
        dataType:'json', // <--- we will get json response
        data: {
          inputQr:inputQr
        },
        success: function(result){
            // get the json response, and if the error status then show message in alert
            (result.status == 'error' && alert(result.message))
             || $('#viewItem').modal('show'); // or just show the modal
        }
      });  
  }); 
});
相关问题