如何插入到PHP jQuery的?PHP、jQuery

由网友(权场帝王)分享简介:据我所知,一个人不能从客户端(JavaScript)执行PHP,但什么是适当的方法?写我的jQuery在PHP?这里是我要找的一个例子。 I understand that one can't execute PHP from the client side (JavaScript), but what would...

据我所知,一个人不能从客户端(JavaScript)执行PHP,但什么是适当的方法?写我的jQuery在PHP?这里是我要找的一个例子。

I understand that one can't execute PHP from the client side (JavaScript), but what would then be the appropriate approach? Writing my jQuery in PHP? Here is an example of what I'm looking for.

<!-- Wordpress template URL -->
<div id="template_directory" style="display: none;">
    <?php bloginfo('template_url'); ?>
</div>

<!-- Fetch with jQuery -->
var templDirec = $("#template_directory").html();
var imgHTML = '<img src="' + templDirec + '/images/my-image.jpg" />';

由于我是新来这个,简单的描述答案是preferred。什么是有JS和PHP一起工作的最好方法?谢谢

Since I'm new to this, simple descriptive answers are preferred. What's the best way of having js and php work together? Thanks

推荐答案

首先,你应该用保鲜js- code 剧本 -tag:

First, you should wrap js-code with script-tag:

<!-- Wordpress template URL -->
<div id="template_directory" style="display: none;">
    <?php bloginfo('template_url'); ?>
</div>

<!-- Fetch with jQuery -->
<script type="text/javascript">
var templDirec = $("#template_directory").html();
var imgHTML = '<img src="' + templDirec + '/images/my-image.jpg" />';
</script>

如果您想从PHP的一些数据传递给JS使用回声

If you want to pass some data from PHP to JS use echo:

<script type="text/javascript">
var templDirec = $("#template_directory").html();
var imgHTML = '<img src="<?php echo $image ?>" />';
</script>

其中, $图像 -php变量。一般来说,你可能会产生c。使用PHP以及HTML的任何JS $ C $。

where $image-php variable. In general you may generate any JS code using PHP as well as html.

要由JS数据传递到PHP(从客户端到服务器)使用 AJAX

To pass data from JS to PHP (from client to server) use AJAX

我希望这会有所帮助。

阅读全文

相关推荐

最新文章