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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to send Javascript in PHP?

Status
Not open for further replies.

marijonas

Technical User
Apr 20, 2004
29
SE
Hi!
I would like to have a small javascript in page generated by php.
<code>
<TR class=searchresultodd
onmouseover="javascript: if(this.className=='searchresultodd'){on=1;};this.style.backgroundColor='#FFDA81';"
onclick="op('cid=2040211');"
onmouseout="javascript: if(on==1){this.style.backgroundColor='#EDF4FA';on=0;} else {this.style.backgroundColor='#ffffff'}">
</code>

I don't know how to send it with print ' as this script contains both " and ' which messes the print command.

Does any know how to write that? Thanks!
 
Enclose your string in one quote type or another (I prefer ") and escape those symbols within the string.

echo "displaying \"quoted\" text";
 
Thanks, I will test that... you mean like:
echo "<TR class=searchresultodd
onmouseover=\"javascript: if(this.className=='searchresultodd'...
 
the other option is if you don't need to send any php content to the javascript is to just close and then reopen your php tags:
Code:
?> <!--close php tag
<code>
<TR class=searchresultodd
              onmouseover="javascript: if(this.className=='searchresultodd'){on=1;};this.style.backgroundColor='#FFDA81';"
              onclick="op('cid=2040211');"
              onmouseout="javascript: if(on==1){this.style.backgroundColor='#EDF4FA';on=0;} else {this.style.backgroundColor='#ffffff'}">
</code>
<?php //open you php tag and continue on
 
I'm guessing you want to condition it.

Say if something is true in PHP then it prints out the javascript right?

well you could escapoe the php delimiters altogether.

Code:
if($myvar==[blue]true[/blue])
[red]?>[/red][green]close PHP delimiters [/green]
[purple]<TR class=searchresultodd
              onmouseover="javascript: if(this.className=='searchresultodd'){on=1;};this.style.backgroundColor='#FFDA81';"
              onclick="op('cid=2040211');"
              onmouseout="javascript: if(on==1){this.style.backgroundColor='#EDF4FA';on=0;} else {this.style.backgroundColor='#ffffff'}">[/purple]

[red]<?PHP[/red][green]reopen PHP tags[/green]

} [green]close if statement curly brace[/green]


Or you could use the alternative HEREDOC syntax.
Code:
echo <<<END
javascript to print.
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END;

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi marijonas,

Have a look at this solution. Four files to complete the example use:

My script loader

[tt]load_script.php[/tt]
Code:
<?php
$script_file_name = $_REQUEST["fn"];
$default_script = "default.my_js";

function output_script($file_name) {
  header("content-type: application/x-javascript");
  $script = file($file_name);
  foreach($script as $line_no => $line)
    echo $line;
}

if(!$script_file_name || !file_exists($script_file_name))
  output_script($default_script);
else
  output_script($script_file_name);
?>

A default javascript

[tt]default.my_js[/tt]
Code:
function init() {
  alert('Default JS loaded');
}

A demo javascript

[tt]some_script.my_js[/tt]
Code:
function init() {
  alert('Intended script loaded');
}

And finally the html to test it out

[tt]index.html[/tt]
Code:
<html>
<head>
  <title>PHP serving JS</title>
  <script type="text/javascript" src="load_script.php?fn=some_script.my_js"></script>
</head>
<body onload="init()">
</body>
</html>

That should give you an idea howto play around on your own.

Regards


Jakob
 
Thanks for the ideas! I have actually taken the simpliest way (lgarner) but I guite sure that I will need other examples in the more complex situations!

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top