【发布时间】: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是否工作不正常
【问题讨论】: