INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to run JavaScript code in PHP

How to run JavaScript code in PHP

(OP)
Hello everyone,
The following HTML/JavaScript code inserts a background imag to a div without causing problems.

CODE

<!DOCTYPE html>
<html lang = "en">
<head>
  <title>to-forum.html</title>
  <script>
    function ChangeImage(param)
    { 
      return("http://localhost/images/greensilver.gif");
    }
  </script>
</head>
<body>
  <div id="logo" style = "height:400px; width:400px";> 
    <script>
      var xyz = ChangeImage();
      document.getElementById("logo").style.backgroundImage = "url('" + xyz + "')";
    </script>
  </div>
</body>
</html> 
I need to run that JS script from a PHP code so I wrote:

CODE

<!DOCTYPE html>
<html lang = "en">
<head>
  <script>
    function ChangeImage(param)
    { 
      return("http://localhost/tlushim/images/greensilver.gif");
    }
  </script>
</head>
<body>
  <div id="logo" style = "height:400px; width:400px";> 
    <?php
      echo '<script>',
      'var xyz = ChangeImage"('" + 1 + "')";',
      'document.getElementById"('" + logo +"')".style.backgroundImage = "url('" + xyz + "')";',
      '</script>'
    ?>
  </div>
</body>
</html> 
With the second code I was less lucky/ This time the desired background image didn't show up, probably, because
I dont know how to run JS code from PHP.
Could anyone help me with that please?
Thanks

RE: How to run JavaScript code in PHP

You don't run Js code from PHP. PHP is a server side language. Javascript is client side.
That means that Javascript runs after PHP has finished running, and the page has been delivered to the browser.

What you can do is deliver Js code to an HTML page from a PHP script, and have the browser run it then.

Your main issue here is your messy quoting scheme.

Your code should be throwing a PHP error first and foremost because you are closing and opening quotes very strangely.

Your echo statement starts with a single quote. which means all your JS must be contained within single quotes at the end of the echo.

CODE

<?php
      echo '<script>'.
      'var xyz = ChangeImage(1);'.
      'document.getElementById("logo").style.backgroundImage = "url(\"xyz\")";'.
      '</script>';
    ?> 










----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

RE: How to run JavaScript code in PHP

(OP)
Thanks vacunita,
Running the code attached to your post my page shows:

CODE

'. 'var xyz = ChangeImage(1);'. 'document.getElementById("logo").style.backgroundImage = "url(\"xyz\")";'. ''; ?> 

RE: How to run JavaScript code in PHP

That means the PHP is not being parsed. Its spitting it out as it is.

How are you running this? what is the extension for the file this is in? .php? .html?

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

RE: How to run JavaScript code in PHP

(OP)
Attached is a screen shot of Apache server as it activated on my platform;
My HTML code in which a PHP subset code resides;
A web page showing how I run the code and what it yields.[img ]
Thanks for your effort !

RE: How to run JavaScript code in PHP

Your PHP is not being run. Unless you configured XAMP to run HTML (*.html) files through the PHP parser, it won't do it natively. Change the file extension to PHP (*.php) to make sure it gets run.

As it is, your PHP code is not being run through the PHP parser.

You can start with something simple to make absolutely sure:


CODE

<?php
$var = 2+5;
echo "Hellow World:" . $var;
?> 


That should output "Hello World 7" nothing more, nothing less. No PHP code should be able to be seen in the source there. Only html and JS should be sent to the browser.


----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close