Echo javascript onfocus/onblur script inside PHP

Hey

I am having a problem trying to echo some javascript code in my php code.

I have a simple enough table where by a user can enter their email address to subscribe to the newsletter and using javascript have got the onfocus/on blur to remove the ‘Enter Email Address’ when the user clicks in the box, if there is nothing in the box it comes up ‘Enter Email Address again’ and so on as seen below:

<form name="newsletter" method="post" action="">
<table id="newsletter"><tr><td class="emailtxt"><h6>Sign up to our motoring newsletter</h6></td>
<td class="emailbox"><input type="text" name="email" size="30" value="Enter Email Address" onfocus="if(this.value.substr(0,5) == 'Enter') this.value='';" onblur="if(this.value.length < 1)  this.value='Enter Email Address';"><input class="go" type="submit" value="" name="submit"></td>
</tr></table></form>

Now the problem is when i try to include this inside an php echo, if the form has been submitted the response will be a thank you (I will be adding in validation for the input as well once this is sorted), if not then the table needs to be displayed with the javascript onfocus/onblur working as talked about above. This however is not working and i cannot work out why. Code is shown below:


<?php 
if (isset($_POST['submit'])) {
	echo '
	<form name="newsletter" method="post" action="">
	<table id="newsletter"><tr><td class="emailtxt"><h6>Thank you. You are now signed up to our newsletter</h6></td>
	</tr></table></form>';
} else {
	echo '
	<form name="newsletter" method="post" action="">
<table id="newsletter"><tr><td class="emailtxt"><h6>Sign up to our newsletter for the latest news, offers and updates</h6></td>
<td class="emailbox"><input type="text" name="email" size="30" value="Enter Email Address" onfocus="if(this.value.substr(0,5) == "Enter") this.value=""" onblur="if(this.value.length < 1)  this.value="Enter Email Address""><input class="go" type="submit" value="" name="submit"></td>
</tr></table></form>
';
}
?>

Any help very much appreciated :slight_smile:

Thanks

Jon

Your quotation marks are different, and screwing up the termination of your script.

onfocus="if(this.value.substr(0,5) == "Enter") this.value="""

Follow the bouncing double-quotes…

You changed some single quotes from the original code in double quotes. Don’t.

Thanks for the replies,

StarLion - trying not to sound like a complete idiot but what do you mean by follow the bouncing quotes?

guido2004 - if i keep the single quotes in the script i get the following error:

Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in etc etc…

In my limited knowledge of php i also thought that all quotes in echo statements have to be turned from single to double otherwise the single quote represents the echo closing?

Start the echo with a double.
Any time you need to use a double-quote inside the block, use \" instead.

Amazing! Thank you StarLion - all works perfectly now by doing what you said

Thanks again :slight_smile: