我试图使用AJAX函数从数据库中使用动态数据打开模态。但是当我点击按钮打开模态时,我得到了
ReferenceError:在第3行第2列的category.php中分配给未声明的变量PostRPC。
修改
阅读Firefox document我理解这种错误类型是因为已经为未声明的变量分配了一个值。换句话说,有一个没有var关键字的赋值。声明和未声明变量之间存在一些差异,这可能会导致意外结果,这就是JavaScript在严格模式下出现错误的原因。我试图找到我的代码中未声明的变量的位置,这很奇怪。
category.php
<!DOCTYPE html>
<html lang="en">
<head>
<script src="js/jquery.js"></script>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="js/jquery-migrate-1.2.1.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="js/script.js"></script>
<script src="js/superfish.js"></script>
<script src="js/jquery.ui.totop.js"></script>
<script src="js/jquery.equalheights.js"></script>
<script src="js/jquery.mobilemenu.js"></script>
<script src="js/jquery.easing.1.3.js"></script>
<script src="js/jquery.tooltipster.js"></script>
<script>
$(document).ready(function(){
$().UItoTop({ easingType: 'easeOutQuart' });
$('.tooltip').tooltipster();
});
</script>
<script>
$(function(){
$('#pull').on('click', function(e) {
e.preventDefault();
$('#menu').slideToggle();
});
})
</script>
</head>
<body>
<?php $sql = "SELECT * FROM product WHERE featured = 1";
$featured = $db->query($sql);
?>
<?php while($product = mysqli_fetch_assoc($featured)) : ?>
<div class="grid_4">
<a href="#"> <img src="images/page2_img1.jpg" alt=""></a>
<div class="text1"><a href="#">Product Name</a></div>
<a href="#"> 120.00$ </a>
<div class="alright"><button type="button" class="btn btn-sm btn-success" onclick="detailsmodal(<?php echo $product['id']; ?>)">See Product</button></div>
</div>
<?php endwhile; ?>
<!--==============================footer=================================-->
<?php include'/includes/footer.php'?>
</body>
footer.php
<script src="js/classie.js"></script>
<script src="js/thumbnailGridEffects.js"></script>
<script>
$(document).ready(function detailsmodal(id){
var data = {"id" : id};
jQuery.ajax({
url : "<?php echo BASEURL;?>+ '/MyProjectName/includes/detailsmodal.php",
method : "post",
data : data,
success : function(data){
jQuery('body').append(data);
jQuery('#details-modal').modal('toggle');
},
error : function(){
alert("Something went wrong!");
}
});
};
</script>
答案 0 :(得分:0)
为我解决的问题是在footer.php的javascript中为BASEURL设置不同的路径
而不是
<script>
$(document).ready(function detailsmodal(id){
var data = {"id" : id};
jQuery.ajax({
url : "<?php echo BASEURL;?>+ '/MyProjectName/includes/detailsmodal.php",
method : "post",
data : data,
success : function(data){
jQuery('body').append(data);
jQuery('#details-modal').modal('toggle');
},
error : function(){
alert("Something went wrong!");
}
});
};
</script>
待办事项
<script>
$(document).ready(function detailsmodal(id){
var data = {"id" : id};
jQuery.ajax({
url : "<?php echo BASEURL;?>+ 'includes/detailsmodal.php",
method : "post",
data : data,
success : function(data){
jQuery('body').append(data);
jQuery('#details-modal').modal('toggle');
},
error : function(){
alert("Something went wrong!");
}
});
};
</script>
对我来说,它修复了一切。因此,如果您碰巧遇到同样的问题,请修改您的路径,直到它适合您。这可能有所帮助。