I have the following working javascript code:

var user_email = document.getElementById("jform_user_email");
user_email.value = "<?php echo JFactory::getUser()->email; ?>";

The next lines are in the same script and it is not working.

var originator_email = document.getElementById("jform_sendto_first_step");
originator_email.value = "<?php echo JFactory::getUser($this->item->created_by)->get('email'); ?>";

It produces this error:

(using $this when not in object context)...

What's wrong with the $this variable? Why can I easily get the logged in user email, but can't get the email of the user who created the submission?

Original thread: Joomla - PHP+$this wrapped in Javascript.

  • 1
    Where did you add this code? Which path/file?
    – Lodder
    Aug 10 '15 at 16:15
  • This is jForm constructor by Gyro and I'm adding the script file on "After display" event. I can check via browser's "view page source" that this sript is added at the bottom of the generated page. Aug 10 '15 at 16:34

It is not clear how you are executing this script. If you are creating a plugin to insert a script into all site pages.

In any case, you could easily add this script with a module to insert source code in all pages, check JED's Coding & Scripts Integration. For instance, NoNumber's sourcerer. In this way, you can focus your effort to create the script instead of the insertion method.


PHP is a Pre Hypertext Processor (technically Hypertext Preprocessor). This means the code is compiled and executed by the server prior (Pre) to the HTML (HyperText Markup Language) being served to the client. JavaScript runs on the client side of the web, meaning that it is served by the HTML and executed after (post) an event occurs in the HTML page. As such, you cannot execute PHP code from Javascript as the Javascript is executed post HTML and the PHP is executed pre HTML.

You can do the opposite, however, and use PHP (pre) to output Javascript (post) to the HTML which is then executed after the HTML is served. To do this, find the location in your PHP file where you want the Javascript to be output in the HTML. Then, echo the PHP results along with your Javascript as so:

<pre><code>your Javascript goes here</code></pre>

This will allow you to ensure the PHP is executing your code correctly and the correct values are being written to the HTML/Javascript. Once you are sure everything is correct, you can remove the <pre><code></code></pre> block and output the Javascript directly to your HTML source. (Note: There is nothing special about using <pre><code></code></pre>, it just allows you to see the code easily in your page without viewing the source.)

Keep in mind that when combining PHP and Javascript together, you will need to test the code for each separately. So, make sure the PHP is outputting what you want outside of the Javascript. Then, make sure the Javascript works correctly using static values instead of PHP code. Then, combined the two. Using this approach will allow you to see if the errors you experience are in your PHP or Javascript.

  • 1
    Even though the question title say "Executing PHP from javascript", the question states the PHP error using $this when not in object context, meaning that the error is coming from $this. So this is the problem, not the fact that PHP is being used in Javascript which can actually be done, providing the Javascript is in a PHP file
    – Lodder
    Jun 10 '16 at 15:12
  • @Lodder, that's exactly what the 2nd paragraph says. The rest explains how to test his output. Thanks. Jun 10 '16 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.