Title: Using PHP variables in javascript
Last modified: August 31, 2016

---

# Using PHP variables in javascript

 *  Resolved [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * (@jsrestaurantlink)
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/)
 * If I’m using PHP [insert_php], and want to use a variable in some script that
   follows – how is that possible?
 * For example…
 *     ```
       <head></head>
       <body>
         [insert_php]
           $testVar = "Hello";
           echo $testVar . "\n";
         [/insert_php]
         <script>
           // Want the value of $testVar here....
           document.write($testVar);
         </script>
       </body>
       ```
   
 * But of-course – that doesn’t work.
 * Can someone correct my thinking?
 * thanks,

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241178)
 * In PHP, echo out a data attribute on an element with the value that you want 
   to convey. Then in JavaScript, grab that data attribute and in turn grab the 
   value.
 *  Thread Starter [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * (@jsrestaurantlink)
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241182)
 * Can you share or correct my code please? I’m not sure of how the syntax should
   work on that.
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241185)
 * The concept is to keep PHP and JavaScript separate.
    As I see, you’re using a
   plugin to insert PHP. Great. Now use a plugin to insert JavaScript.
 * What specifically aren’t you sure how to do?
 *  Thread Starter [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * (@jsrestaurantlink)
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241187)
 * Thanks for jumping in here.
 * I have some php code this is getting a session ID and token. Then I need those
   ID’s and tokens in Javascript to call some .js code. (I’m working on getting 
   TokBox to work from my wordpress sites).
 * But I’m just doing a test with the code above – as once I come out of the php
   code – I don’t know how to access any of the variables that I created and populated
   in the php code.
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241190)
 * So does it make sense if you were to do something like this in PHP:
 *     ```
       echo '<i data-cookie="' . $testVar . '" />';
       ```
   
 * Then this in jQuery:
 *     ```
       var cookieData = jQuery('[data-cookie]');
   
       cookieData = cookieData.attr('data-cookie');
       ```
   
 *  Thread Starter [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * (@jsrestaurantlink)
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241192)
 * Yes – very helpful!
 * On the “echo ‘<i …. line – what is the i for? integer?
 * And I would need to make sure that I have the jQuery jar included?
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241193)
 * The “<i” is for an element in HTML known as the ‘<i>’ element. People sometimes
   say that this is for italic text, but it is not. It’s used for text in general,
   but that’s not important. I recommend you use this element because it is the 
   least significant element. Putting an empty “<i>” tag into your page won’t affect
   your page.
 * > And I would need to make sure that I have the jQuery jar included?
 * I think you mean the jQuery library? Yes, you can make sure that the jQuery library
   is included using your functions.php file:
 *     ```
       function enqueue_scripts() {
           wp_enqueue_script('jquery');
       }
   
       add_action( 'wp_enqueue_scripts', 'enqueue_scripts );
       ```
   
 *  Thread Starter [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * (@jsrestaurantlink)
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241204)
 * Ok – thank you!
 * So, I have code like this…
 *     ```
       <head></head>
       <body>
         [insert_php]
           $testVar = "Hello";
           echo '<i testVar="$testVar" />';
         [/insert_php]
         <script type="text/javascript">
           document.write('In Script...');
           var testVar = jQuery('[testVar]');
           testVar = testVar.attr('testVar');
           document.write(testVar);
         </script>
       </body>
       ```
   
 * My output is
    In Script…$testVar
 * Why isn’t my output
    In Script…Hello ?
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241209)
 * Try this instead:
 *     ```
       echo '<i data-cookie="' . $testVar . '" />';
       ```
   
 * Then update your JavaScript to reflect that.
 * But the first thing I would do is check if jQuery (library) is being loaded.
 *  Thread Starter [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * (@jsrestaurantlink)
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241215)
 * No – I didn’t have my jquery loaded.
    I’ve got it loaded now. Although – I’m 
   not sure where to put the add_action( ‘wp_enqueue_scripts’, ‘enqueue_scripts );
   line? The function – I just added to the end of the functions.php file – but 
   if I put the add_action at the end – I get an error re-loading my WordPress (
   makes sense) But I’m not sure where it needs to go.
 * So – data-cookie – is that a name you just made up – or is that a standard object?
 * And – even with out putting in the enqueue scripts – I’m getting what I would
   expect on my test (woo-hoo). So, now this code….
 *     ```
       <head></head>
       <body>
         [insert_php]
           $testVar = "Hello";
           echo '<i data-cookie="' . $testVar . '" />';
         [/insert_php]
         <script type="text/javascript">
           document.write('In Script...');
           var cookieData = jQuery('[data-cookie]');
           cookieData = cookieData.attr('data-cookie');
           document.write(cookieData);
         </script>
       </body>
       ```
   
 * Produces this…
    In Script…Hello
 * Again – Thanks for your help on this. It’s starting to make more sense now.
 *  Thread Starter [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * (@jsrestaurantlink)
 * [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241234)
 * Not sure where to put the add_action – but other than that it’s working. I’ll
   mark this as resolved.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Using PHP variables in javascript’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 2 participants
 * Last reply from: [Jeff Stevens](https://wordpress.org/support/users/jsrestaurantlink/)
 * Last activity: [10 years ago](https://wordpress.org/support/topic/using-php-variables-in-javascript/#post-7241234)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
