jQuery Question

Why function not displaying value in php

I am trying to getting the value of variable inside the function defined in Shot_code.php.

Outside the class the value is showing, but i am getting blank output when trying to get it inside the function and there is no any syntax error.

What's going wrong with it?

Shot.php

<?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">&times;</button>

<?php
$cart_class->cart_head_info_print();

?>
</div>

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


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
}
}

Answer

isset() will return boolean instead of POST['ID']. Also, PHP won't recognize it since you're not telling the Cart_Class needs to instantiated once post request comes.

It would better written like this

<?php
include './Shot_code.php';

if (isset($_POST['ID'])) {
    $cart_class = new Cart_Class($_POST['ID']);
    $cart_class->cart_head_info_print();
    exit();
}
....
....

Then grab the result and perform js logic

Comments