Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run JavaScript code in PHP

Status
Not open for further replies.

lupidol

Programmer
Apr 23, 2008
125
IL
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("[URL unfurl="true"]http://localhost/images/greensilver.gif");[/URL]
    }
  </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("[URL unfurl="true"]http://localhost/tlushim/images/greensilver.gif");[/URL]
    }
  </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
 
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 '[COLOR=#BABDB6]<script>[/color]'.
      '[COLOR=#BABDB6]var xyz = ChangeImage(1);[/color]'.
      '[COLOR=#BABDB6]document.getElementById("logo").style.backgroundImage = "url(\"xyz\")";[/color]'.
      '[COLOR=#BABDB6]</script>[/color]';
    ?>












----------------------------------
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
 
Thanks vacunita,
Running the code attached to your post my page shows:
Code:
'. 'var xyz = ChangeImage(1);'. 'document.getElementById("logo").style.backgroundImage = "url(\"xyz\")";'. ''; ?>
 
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
 
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.
to-tek-tips_wtyumt.gif

Thanks for your effort !
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top