1

I have an idea how to pass PHP value into a jQuery variable, I have done it in the past and it worked. But I tried the below code but for some reason it not working. I dont know where the problem lies.

I am trying to hide a custom virtuemart drop down cart I designed when the cart is empty.

jQuery(document).ready(function ($) {   
var cart = "<?php if ($data->totalProduct) echo  $data->cart_show; ?>"
jQuery('.btn-cart1').hover(if (cart === "") {
jQuery(this).find('.dropdown').hide();
} 
else {          
jQuery(this).find('.dropdown').hide();
jQuery('.btn-cart1').hover(
    function() { jQuery(this).find('.dropdown').stop().show(500)},
    function() { jQuery(this).find('.dropdown').stop().hide(500)})
}
});
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
Joseph
  • 313
  • 1
  • 4
  • 16

1 Answers1

1

PHP is server side scripting language and is executed well before your javascript executes on web browser.

The correct execution of your code depends on the values of $data->totalProduct & $data->cart_show after the PHP has been executed on your server side and the values are places in your javascript.

It is not a good programming practice and I would strongly suggest you to use AJAX instead to access the values of PHP variables.