Calling a PHP variable in jQuery

javascript, jquery, php

Solution

You have two or three options: if the Javascript is in the php file, you can

var phpVar = <?php echo $var; ?>;

Otherwise if the Javascript is anywhere at all, you can do:

<input type="hidden" id="phpVar" value="<?php echo $var; ?>">

and then access it as

$('#phpVar').val();

Example 1:

jQuery(document).ready(function($){
$('#image1').addimagezoom({ // single image zoom
    zoomrange: [3, 10],
    magnifiersize: [800,300], 
    magnifierpos: 'right',
    cursorshade: true,
    largeimage: <?php echo $var; ?> //we add the directory of the image.
});
});

Example 2: Html:

<input type="hidden" id="phpVar" value="<?php echo $var; ?>">

Javascript

jQuery(document).ready(function($){
$('#image1').addimagezoom({ // single image zoom
    zoomrange: [3, 10],
    magnifiersize: [800,300], 
    magnifierpos: 'right',
    cursorshade: true,
    largeimage: $('#phpVar').val(); //we add the directory of the image.
});
});

Problem

I'am developing a zoom tool in my shopping cart and I am stuck on how to call a PHP variable in a jQuery function. Here's my code : ``` jQuery(document).ready(function($){ $('#image1').addimagezoom({ // single image zoom zoomrange: [3, 10], magnifiersize: [800,300], magnifierpos: 'right', cursorshade: true, largeimage: "php variable" //we add the directory of the image. }); }); ``` I need to put ``` $src ="images/products/".mysql_result($execute_select_product_query,0,'image1')." ``` in my function where I put PHP variable.

Original source