Forum Moderators: coopster

Message Too Old, No Replies

Javascript within a php variable

         

madmatt69

11:12 pm on Nov 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey all,

I have some html inside a php variable which I call, sort of like an include.

One part of the html is a form element where people put in their email address to signup for a newsletter.

It looks like this:
<input type="text" name="ea" size="14" value="your@email.com" onclick="this.value='';this.onclick=null;" />

The javascript in the code above makes it so that when the user clicks in the box, the "your@email.com" disappears.

However php seems to be choking on the javascript there.

Basically the code looks like:

$email_form = '<input type="text" name="ea" size="14" value="your@email.com" onclick="this.value='';this.onclick=null;" />'

Is there a way I can escape out the javascript so the php doesn't choke on it? I was thinking I could put it in a seperate javascript file and call that externally, but I'd rather keep it clean and have the code in the variable.

Any suggestions would be appreciated!

Caliber Mengsk

2:21 am on Nov 11, 2007 (gmt 0)

10+ Year Member



Your problem is probably at the this.value = ''....

Using ' to surround the initial text string, means that you can't use ' as a container for a value inside of it.

The way to get around that is to use a \ infront of it. So if you wanted to use a ' in a php string it'd be like this.

$string = 'This is \' a test';

Here is how I would do it.

<?
echo "<input type='text' name='ea' size='14' value='your@email.com' onClick='if(this.value == \"your@email.com\"){this.value = \"\"}'>";
?>

This way checks to see if the value is equal to the default value, so it only erases it if the value of it is the default text. Try it out yourself... It worked fine for me.