在 jQuery 中调用 PHP 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18230294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a PHP variable in jQuery
提问by Anthony G. Helou
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.
我正在我的购物车中开发一个缩放工具,但我一直在思考如何在 jQuery 函数中调用 PHP 变量。
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.
在我放置 PHP 变量的函数中。
回答by Wren
You have two or three options: if the Javascript is in the php file, you can
您有两个或三个选项:如果 Javascript 在 php 文件中,您可以
var phpVar = <?php echo $var; ?>;
Otherwise if the Javascript is anywhere at all, you can do:
否则,如果 Javascript 在任何地方,你可以这样做:
<input type="hidden" id="phpVar" value="<?php echo $var; ?>">
and then access it as
然后访问它
$('#phpVar').val();
Example 1:
示例 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:
示例 2:
HTML:
<input type="hidden" id="phpVar" value="<?php echo $var; ?>">
Javascript
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.
});
});
回答by Sahil Mittal
Do it like this:
像这样做:
largeimage: "<?php echo $src; ?>"
回答by M1K1O
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 是服务器端语言,javascript 是用户端语言。不要混合它。就用ajax吧。或者,如果您已将脚本包含在 PHP 文件中,则可以将其编写为代码:
<?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:
在 js 中:
var phpvar = $("body").attr("data-phpvar");
回答by Sal00m
You can use the php tags to echo your variable:
您可以使用 php 标签来回显您的变量:
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:
编辑:如果 jQuery 未包含在 php 文件中,那么您可以在 php 文件中声明一个 javascript 变量:
<?
php code
?>
<script type="text/javascript">
var my_js_variable = '<?=$my_php_variable?>';
</script>
<?
php code
?>
回答by Nathan Srivi
Add php tag into your jquery "<?php echo $sample; ?>"
将 php 标签添加到您的 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.
});
});