Sun
Sun

Reputation: 89

How can I include a javascript file using php echo?

How can I include a javascript file using php echo ?

I am creating a html page using php ---

echo "<html><head><link rel='stylesheet' type='text/css' <script src="Calculation.js"> </script>  href='style.css' </head><body>";

I have included script code in the above line and then in form tag ---

<form action='/workdonecalculation/index.php?_pagination_off=1' method='post'  class='form'  onSubmit='return workdone();'>

But my php code doesn't call my javascript file (Calculation.js)

Upvotes: 3

Views: 10467

Answers (6)

EM-Creations
EM-Creations

Reputation: 4301

print("<script type=\"text/javascript\" src=\"Calculation.js\"></script>");

Are you running this code on Windows, if not are you sure the file is called "Calculation.js" and not "calculation.js"? Also, is it definitely in the same directly as the PHP script that's calling it?

Upvotes: 1

Nwafor
Nwafor

Reputation: 184

use single qoute:

echo '<html><head><link rel="stylesheet" href="style.css" type="text/css" /> <script type="text/javascript" src="script.js"></script> </head><body>';

hope it helps

Upvotes: 1

Merlin
Merlin

Reputation: 81

Seems the link tag is not closed properly and the double quotes in script tag is not escaped.

Try the below code :

echo "<html><head><link rel='stylesheet' type='text/css' href='style.css'><script src='Calculation.js'></script></head></html>";

Upvotes: 1

Mohit Mehta
Mohit Mehta

Reputation: 1285

You'll need to complete link tag first.

Try this

echo "<html><head><link rel='stylesheet' type='text/css' href='style.css' /> <script src='Calculation.js'></script>  </head><body>";

Upvotes: 1

geekman
geekman

Reputation: 2244

 echo "<html><head><link rel='stylesheet' type='text/css'/> <script src='Calculation.js'></script>"

First close link tag, and you are using double quotes.. for the src= use single, because you are already using double in the echo "". So src='Calculation.js'

Also you can do this the easy way use variables.

$a=<<<HERE
<script src="Calculation.js"> </script>
HERE;
echo $a;

Also you can close php and start html where ever you wish

<?php
if($i)
{
?>
<script src=""></script> 
<?php
}
?>

Is that what you wanted???

Upvotes: 5

Muthu Kumaran
Muthu Kumaran

Reputation: 17910

You didn't close <link> tag and <script> tag misplaced inside <link> tag. Also you need to escape the double quotes in src="Calculation.js" or simple you enclose with single quotes.

Here is the correct code,

echo "<html><head><link rel='stylesheet' type='text/css' href='style.css' /><script src='Calculation.js'></script> </head><body>";

Upvotes: 3

Related Questions