【问题标题】:Ajax resquest responses always equal to zero in php mysql [duplicate]php mysql中的Ajax请求响应始终为零[重复]
【发布时间】:2019-08-05 14:53:54
【问题描述】:

我想通过 ajax php 将一些数据从 mysql 加载到 div。自增的id字段总是为零。

我尝试过使用 get、post 等,但都没有用

<buttton onclick="openNav()" data-id="<?php echo $row['customer_id']?>"  data-target="#txtHint" id="getUser" ><?php echo $row['display_name'];?></button>    
    <script>
          $(document).ready(function(){

           $(document).on('click', '#getUser', function(e){

            e.preventDefault();

            var uid = $(this).data("customer_id");   // it will get id of clicked row

            $('#txtHint').html(''); // leave it blank before ajax call
            $('#mySidenav').show();      // load ajax loader

            $.ajax({
              url: 'getCustomerDetails.php',
              type: 'GET',
              data: 'customer_id='+uid,
              dataType: 'html'
            })
            .done(function(data){
              console.log(data);  
              $('#txtHint').html('');    
              $('#txtHint').html(data); // load response 
              $('#modal-loader').hide();      // hide ajax loader 
            })
            .fail(function(){
              $('#txtHint').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
              $('#modal-loader').hide();
            });

          });

         });

        </script>


                <?php

        include_once('../../config/dbconfig.php');


            if (isset($_REQUEST['customer_id'])) {

                $id = intval($_REQUEST['customer_id']);
                $query = "SELECT * FROM customers WHERE customer_id=:id";
                $stmt = $pdo->prepare( $query );
                $stmt->execute(array(':id'=>$id));
                $row=$stmt->setFetchMode(PDO::FETCH_ASSOC);


                ?>
                <div class="row">
                    <div class="col-md-1">
                    <div class="col-md-4" >
                        <?php echo $row['first_name'];?>
                    </div>
                    <div class="col-md-6">

                    </div>
                </div>
            </div>


            <?php echo $id;?><br/>


        <?php
        }

        ?>

结果没有回显 first_name,因为 $id 始终为 0。我想在完成后获取个人 $id(auto_increment) 它应该显示用户记录 我不知道data-id是否工作不正常

【问题讨论】:

    标签: php jquery mysql ajax


    【解决方案1】:

    你必须替换这一行:

    var uid = $(this).data("customer_id")
    

    通过这一行:

    var uid = $(this).data("id")
    

    var uid = $(this).attr("data-id")
    

    【讨论】:

      猜你喜欢
      • 2019-12-12
      • 2011-09-17
      • 2013-02-19
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 2012-06-08
      相关资源
      最近更新 更多