Page 1 of 1

I'm a total php newbie.. help on simple problem..

Posted: Thu Jun 19, 2003 10:34 pm
by th3monk3y
Hi All,

This should be a breeze for you guys... I just started messing around with php couple days ago and I am stuck...

i need to echo some javascript and i am not sure where to put in all the back slashes! \ I keep getting errors and more frustrated!

this is what i need to echo

<a href="javascript:;" onClick="MM_openBrWindow('http://www.mydomain.com/path/path/','', ... 15,left=15')"><img src="http://www.mydomain.com/path/path/unavailable.gif"
border="0" alt="my alt text"></a>

thanks in advance,
-Paul

Posted: Thu Jun 19, 2003 10:40 pm
by trollll
When in doubt, use the addslashes function. In general, you need to add slashes to ", ', \ and null bytes. The link tells all... :)

magic quotes?

Posted: Thu Jun 19, 2003 10:54 pm
by th3monk3y
I don't know if it matters with the link you sent me, but I have magic quotes turned off in order to make another php program run properly.. is there a general rule of thumb here? I am sort of looking for the short quick answer... I have been looking at other php examples and noticed the back slashes behind all the =
thanks,
-paul

<a href=\"javascript:;" onClick="MM_openBrWindow

I just don't understand where to terminate them... any help would be greatly appreciated! I have been struggling for hours...

Posted: Thu Jun 19, 2003 11:00 pm
by nielsene
The back slash is going before the quote, not after the equal. :) All quotes that are inside the string you want to print out will need to be "escaped" with the backslash. If you want a regular back-slash in your string you will need to slash it too.

Posted: Thu Jun 19, 2003 11:05 pm
by Drachlen

Code: Select all

<?php

echo "<a href="javascript:;" onClick="MM_openBrWindow('http://www.mydomain.com/path/path/','','width=200,height=473,top=15,left=15')"><img src="http://www.mydomain.com/path/path/unavailable.gif" 
border="0" alt="my alt text"></a>";

?>
It depends on how you're echoing. If you start your echo with ' you need to \ all the ' except for the closing one. If you're using " to start the echo, use \ behind ALL " except the closing ";
Any pro people correct me if im wrong here it appeared that way for me, and you just need to \ any of the "
So just look at what i gave you and use it as referance for next time, it's really simple, just learn to code \" instead of ", it didnt take me long to get into a habit of doing it, and it shouldnt for you either.

Posted: Thu Jun 19, 2003 11:10 pm
by nielsene
Also, if you're using an editor with syntax highlighting (ie it colors your code as you type, similar to what the PHP blocks on this forum do) you can check to see if your quotes are properly escaped. If you notice in Drachlen's example the entire echo is all the same color. If you didn't escape the quotes properly you would see the colors changing around a lot.

This is one of the reasons people like tools that do the syntax highlighting :)

Posted: Thu Jun 19, 2003 11:36 pm
by th3monk3y
that's it!! You guys are Awesome thanks!

-Paul

Posted: Fri Jun 20, 2003 6:08 am
by m3mn0n
I would use:

Code: Select all

<?php
  echo "
?>
<a href="javascript:;" onClick="MM_openBrWindow('http://www.mydomain.com/path/path/','','width=200,height=473,top=15,left=15')"><img src="http://www.mydomain.com/path/path/unavailable.gif" 
border="0" alt="my alt text"></a>
<?php
  "; // ending the echo
?>
I find this way nice because there isn't a zillion slashes in your source and if you use Dreamweaver as an editor the code can be displayed in the WYSIWYG editor properly. ;)