【问题标题】:Show modal if value exists and show error message if not如果值存在则显示模态,如果不存在则显示错误消息
【发布时间】:2019-02-17 20:18:33
【问题描述】:

Toggle 不完全是术语,但是,我想先检查值是否存在,然后显示我的模态,如果不显示,则不能显示模态,只是一个 PHP 错误消息。

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

 $(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();

?>

【问题讨论】:

    标签: jquery ajax pdo


    【解决方案1】:

    我认为你必须改变你的逻辑,

     $(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
            }
          });  
      }); 
    });
    

    现在,在 PHP 中更改您的代码,例如,

    <?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();
        $response = array('status'=>'success','message'=>'Barcode exists'); // be positive here
        if($qr['checkrows'] <= 0){
            $msg = 'Barcode doesn\'t exist! Kindly review your input!';
            $_SESSION['error'] = $msg;
            $response = array('status'=>'error','message'=>$msg);
        }
        $pdo->close();
        echo json_encode($response);
    ?>
    

    【讨论】:

    • 你的逻辑很好,我理解的很好。但是即使条形码不存在,模式仍然会显示。
    猜你喜欢
    • 1970-01-01
    • 2022-12-06
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 2017-10-10
    相关资源
    最近更新 更多