Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  83] [ 2]  / answers: 1 / hits: 37621  / 11 Years ago, wed, august 14, 2013, 12:00:00

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.


More From » php

 Answers
18

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.
});
});

[#76368] Monday, August 12, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zahrafrancisr

Total Points: 176
Total Questions: 105
Total Answers: 99

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
;