3

Here is script I have. I'm trying to load it after page display. I'm trying to execute PHP code from javascript. What I need is to get email address of user who submitted the form at the textbox control.

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

This is the error I have. using $this when not in object context bla bla bla. What's proper way of doing this ?

Have tried to include the code:

<?php  echo JFactory::getUser($this->item->created_by)->get('email'); ?>

into the page directly and I can see it throw the result to the screen no problems.

  • Am I right in assuming you're putting all your Javascript in the PHP file? – Lodder Aug 6 '15 at 13:01
  • when i putting JS inside PHP it is working. if i'm trying to link .js file to PHP it return error – Oleg Agadjanyan Aug 6 '15 at 14:32
  • 1
    Well of course. You can't put PHP in a JS file ;) – Lodder Aug 6 '15 at 14:33
  • var user_email = document.getElementById("jform_user_email"); user_email.value = "<?php echo JFactory::getUser()->email; ?>"; – Oleg Agadjanyan Aug 6 '15 at 14:51
  • this works fine – Oleg Agadjanyan Aug 6 '15 at 14:54
2

As Lodder says you can't expect a .js file (which is run in the browser) to run PHP - which is run on the server.

The simplest way round this is to echo out your javascript code on the server side. So in your template's index.php, somewhere in the section add the following:

<script type="text/javascript">
<?php  echo JoomlaEmail = "' . JFactory::getUser($this->item->created_by)->get('email') . "';"; ?>
</script>

when PHP runs your template file this should get written out to the browser as:

<script type="text/javascript">
JoomlaEmail = "test@email.com";
</script>

Now in your original javascript code you should be able to do:

var originator_email = document.getElementById("jform_sendto_first_step");
originator_email.value = JoomlaEmail;

In doing things this way you are polluting Javascripts global namespace which is not really the done thing, but you should be able to use this method to write out some JS code in the PHP file which is encapsulated inside a function.

<script type="text/javascript">
(function () {
    var originator_email = document.getElementById("jform_sendto_first_step");
    originator_email.value = "<?php  echo JFactory::getUser($this->item->created_by)->get('email'); ?>"
})();
</script>
  • That's what i have done so far. I just wanted to keep intact php template . Anyway thanks for explanation. Very clear answer – Oleg Agadjanyan Aug 10 '15 at 13:34
  • @OlegAgadjanyan If this answers your question, please mark it as the answer by clicking the tick next to it. – TryHarder Aug 21 '15 at 23:39
  • @Rob Clayburn Is there a Joomla way to do this without polluting the Javascripts global namespace? – TryHarder Aug 21 '15 at 23:46

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.