4

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.

2
  • You can't. Not without AJAX or include your JavaScript function inside a PHP file. Commented Aug 14, 2013 at 11:19
  • Do you use some template engine? Commented Aug 14, 2013 at 11:20

5 Answers 5

17

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.
});
});
Sign up to request clarification or add additional context in comments.

Comments

5

Do it like this:

largeimage: "<?php echo $src; ?>"

5 Comments

What if the OP's jQuery is not inside a PHP file?
Note that this will only work if your PHP script actually outputs JavaScript inline or serves up the JS as text/javascript.
He have not mentioned that, so as per normal understanding it seems that his function is in the same PHP file.
@AnthonyG.Helou have you fetched that database output separately in a variable and tested?
Could you edit and extend your ques, and add the code where you had defined the PHP variable
4

PHP is server side language and javascript is user side language. Don't mix it. Just use ajax. Or if you have included your script into the PHP file, you can write it to code:

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

Ou use data-attrs

<body data-phpvar="<?php echo $srcImg; ?>">

And in js:

var phpvar = $("body").attr("data-phpvar");

1 Comment

Depending on which version of jQuery he's using, you can also do. var phpvar = $("body").data("phpvar");
2

You can use the php tags to echo your variable:

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

Edited: If jQuery is not included in php file then you can declare a javascript variable into the php file:

<?
php code
?>
<script type="text/javascript">
var my_js_variable = '<?=$my_php_variable?>';
</script>
<?
php code
?>

1 Comment

Unnecessary inclusion of print functions.
1

Add php tag into your jquery "<?php echo $sample; ?>"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.