include javascript code in a php file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ridwan
Forum Commoner
Posts: 55
Joined: Thu Aug 22, 2002 3:15 am

include javascript code in a php file

Post by ridwan »

Hi, all I was wondering is it possible to include javascript info in a php file' cos when I try and do something like this it spits out an error and my javascript code doesn't work->

Code: Select all

<div id='menu8Container'>
		<div id='menu8Content' class='menu'><table border='0' cellspacing='0' cellpadding='0'>
  <tr>

        <td align='left' valign='top' rowspan='5'><img src='../images/dealers_top.gif' width='40' height='14'></td>
        <td bgcolor='#768DC1' align='right'>&nbsp;<a href='../dealers/dealers.php?pageframe=intro' onMouseOver='MM_swapImage('dealers','','../images/menu%20b_r15_c2.gif',1)' onMouseOut='MM_swapImgRestore()' target='_parent'>Login</a></td>
  </tr>
  <tr>
        <td bgcolor='#FFFFFF'><img src='/images/spacer.gif' width='100%' height='1'></td>
  </tr>
</table></div>
	</div>
I have looked in my php bible and can't seem to pick up anything about it so hopefully someone knows something about it???

thanks
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Yup, you can use that in .php files.

It might help, though, if you pasted the whole PHP file, or at least the part you are having trouble with.
ridwan
Forum Commoner
Posts: 55
Joined: Thu Aug 22, 2002 3:15 am

the error i get

Post by ridwan »

The error I get is something to do with my javascript code. In effect what it really is; is that the javascript code is looking for an apostrophe but when writing something in php you need to use this -> ' . Is there any way to fix it?

Any suggestions ??

thanks
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The problem you are experiencing is because you are using single quotes around your HTML attributes instead of double quotes like you're supposed to and this is then messing up your Javascript because when the page is displayed the browser thinks that the opening quote in your Javascript is the closing quote for the mouseover attribute.

Code: Select all

<div id="menu8Container">
	<div id="menu8Content" class="menu">
		<table border="0" cellspacing="0" cellpadding="0"> 
		<tr> 
			<td align="left" valign="top" rowspan="5">
				<img src="../images/dealers_top.gif" width="40" height="14">
			</td> 
			<td bgcolor="#768DC1" align="right">
				<a href="../dealers/dealers.php?pageframe=intro"
					onMouseOver="MM_swapImage('dealers','','../images/menu%20b_r15_c2.gif',1)"
					onMouseOut="MM_swapImgRestore()" target="_parent">Login</a>
			</td> 
		</tr> 
		<tr> 
			<td bgcolor="#FFFFFF">
				<img src="/images/spacer.gif" width="100%" height="1">
			</td> 
		</tr> 
		</table>
	</div> 
</div>
In your PHP you can now either escape out of PHP (by putting a ?> before and a <?php after the code) to add the HTML or use single quotes around your block (use the escape character \ to escape the single quotes in the JS) or use heredoc format. For more info:
echo() and http://www.php.net/manual/en/language.types.string.php

Mac
Post Reply