0

Sorry for the basic question, just a bit confused as to what is going on here. I have some PHP running in Wordpress, I am able to run html, php and javascript all from within one file. For example:

<?php
//query WP for the tag

$wp_getTag = $wpdb->get_results( 
    "
    SELECT name 
    FROM $wpdb->mydb_wor1.ukj_terms 
    INNER JOIN ukj_term_taxonomy
    ON (
        ukj_terms.term_id = ukj_term_taxonomy.term_id
        )
    WHERE ukj_term_taxonomy.taxonomy LIKE 'post_tag'
    " 
);

$json = json_encode($wp_getTag);

?>

<script type="text/javascript"> 
// pass the value to js

var JsonTags = <?php echo $json ?>;

</script>

So all the above works. I am grabbing some info from wp and then assigning it's value to a JS variable using echo. But it is not clear what is going on here? is the Javascript running on the server instead of the client?

4

2 回答 2

3

不,JavaScript 正在客户端上运行。PHP 正在服务器上运行。也就是说,这段代码在服务器上运行:

<?php echo $json ?>

该代码的评估导致在输出的该位置向客户端发出一个字符串。因此,如果变量$json包含字符串“{ 'value' : 'hello world' }”,那么此代码将在客户端上运行:

var JsonTags = { 'value' : 'hello world' };

首先,所有服务器端代码运行,最终导致对客户端的响应。一旦客户端收到该响应,所有客户端代码都会运行。

于 2013-08-02T13:45:18.050 回答
0

服务器将信息输出到客户端,因此函数名称为“echo”(它从服务器回显到客户端)。

您可以将服务器端代码混合到客户端代码中,因为它首先由服务器处理,这就是为什么您不能(不使用 Ajax)使用 Javascript 影响服务器端代码的原因。

于 2013-08-02T13:46:27.637 回答