为什么函数不在php中显示值
我试图获取Shot_code.php中定义的函数中变量的值 在类外部显示该值,但在函数内部尝试获取该值时,得到的是空白输出,并且没有任何语法错误 怎么了 Shot.php为什么函数不在php中显示值,php,jquery,oop,Php,Jquery,Oop,我试图获取Shot_code.php中定义的函数中变量的值 在类外部显示该值,但在函数内部尝试获取该值时,得到的是空白输出,并且没有任何语法错误 怎么了 Shot.php <?php include './Shot_code.php'; $cart_class = new Cart_Class(isset($_POST['ID'])); ?> <script> $(document).ready(function(){
<?php
include './Shot_code.php';
$cart_class = new Cart_Class(isset($_POST['ID']));
?>
<script>
$(document).ready(function(){
$('.getSrc').click(function() {
var src = $(this).attr('src');
var id = $(this).attr('id');
var specific = $('.showPic').attr('src');
var specific = src;
$.ajax({
type: "POST",
url: "Shot.php",
data: {"ID":id},
success: function(result){
console.log(result);
}
});
});
});
</script>
<body>
<div data-toggle='modal' data-target='#myModal' id='single_product'
class='col-md-3 col-sm-4 col-xs-12'>
<img src="images/kabootar.jpg" class="getSrc" id="5">
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<img src="" class="showPic">
<div class="modal-header" style="padding:35px 50px;">
<button type="button" class="close" data-dismiss="modal">×</button>
<?php
$cart_class->cart_head_info_print();
?>
</div>
</div>
</div>
</div>
</body>
$(文档).ready(函数(){
$('.getSrc')。单击(函数(){
var src=$(this.attr('src');
var id=$(this.attr('id');
特定变量=$('.showPic').attr('src');
var-specific=src;
$.ajax({
类型:“POST”,
url:“Shot.php”,
数据:{“ID”:ID},
成功:功能(结果){
控制台日志(结果);
}
});
});
});
&时代;
Shot_code.php
<?php
// Here if(isset) returning value
class Cart_Class{
public $aho;
public function __construct($id)
{
$this->aho = $id;
}
function cart_head_info_print(){
echo $this->aho;
//Here nothing is displaying
}
}
isset()
将返回布尔值,而不是POST['ID']
。此外,PHP不会识别它,因为您没有告诉Cart\u类在post请求出现时需要实例化
最好这样写
<?php
include './Shot_code.php';
if (isset($_POST['ID'])) {
$cart_class = new Cart_Class($_POST['ID']);
$cart_class->cart_head_info_print();
exit();
}
....
....
@jszobody代码缩进正确,我认为在阅读语法时没有任何问题。您正在实例化您的类吗?e、 g.包括“Shot_code.php”$购物车类别=新购物车类别($id)代码>@BenM。查看我的更新代码添加此代码后,我仍然无法在模式标头中获取值。它仍然返回空白输出当然,您需要通过jQuery获得输出——比如说——成功回调,然后将其设置为任意位置,即$('.modal header').html('result')
对我有效!棒 极 了上帝保佑你!