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?

3
  • 1
    Unless you are using Node.js (and you are not) javascript always runs on the client side. What you are doing here is using PHP to query the database, then encoding that information as JSON, and then creating a javascript object from that JSON. This allows you to access the information on the client side (using javascript) instead of having to query the server everytime you need whatever information that query returns. Commented Aug 2, 2013 at 13:43
  • 1
    To add to what @cernunnos said, the PHP and database querying parts happen on the server, and it builds an HTML page with some embedded JavaScript in it. This HTML page is then sent to the client browser, which then sees the JavaScript with your database results already magically there (it has no idea PHP queried a database for it and echoed it to the final HTML page to send to your browser). Commented Aug 2, 2013 at 13:45
  • Thank you for the help on this, it was not clear, but now it is! Commented Aug 2, 2013 at 14:14

2 Answers 2

3

No, the JavaScript is running on the client. The PHP is running on the server. That is, this code runs on the server:

<?php echo $json ?>

The evaluation of that code results in a string being emitted to the client in that location of the output. So, if the variable $json contains the string "{ 'value' : 'hello world' }" then this code would then run on the client:

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

First all the server-side code runs, which ultimately results in a response to the client. Once that response is received by the client, all the client-side code runs.

Sign up to request clarification or add additional context in comments.

Comments

0

The server is outputting information into the client side, hence the function name 'echo' (it echo's from the server onto the client side).

You're able to mix server side code into client side code as a result as it's processed by the server first, this is why you can't (without the use of Ajax) effect server side code with Javascript.

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.