Link to home
Start Free TrialLog in
Avatar of ckellems
ckellems

asked on

syntax error using echo with Input with an onclick

This does not work:
echo '<input type="button" value="Go Home" onClick='window.location.href("FastDlr.php")' />';
Please provide the necessary correction and a brief discription of the solution
Avatar of nplib
nplib
Flag of Canada image

make it like this

echo '<input type="button" value="Go Home" onClick="window.location.href(\'FastDlr.php'\)" />';
ASKER CERTIFIED SOLUTION
Avatar of nplib
nplib
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Albert Van Halen
You use single quotes in the onclick attribute
You have to escape them
echo '<input type="button" value="Go Home" onClick=\'window.location.href("FastDlr.php")\' />';

Open in new window

you can, but then it doesn't follow the xHTML 1.0 standard.
Just a few seconds behind.

Kudos to nplib ;-)
Avatar of ckellems
ckellems

ASKER

Thanks nplib, I attempted several permeations but none of them correct.  What is the rule so that I may" learn how to fish" verses asking for a "fish"?  
When encapsulating anything in single quotes or double quotes, if you need to use a quote in the string that is used for the encapsulation then you need to escape the quote with a \

in other  words
if you make a string like this
$string = "How's it going"; //you don't need to escape anything because the quote in the string is not the same as the quote used to encapsulate the string.
 
if you make a string like this
$string = 'How\'s it Going"; //notice the \ to escape the single quote. without that it would error.
 
This same rule applies in no matter which quote you use
 
$string = 'This is string has a "quote" in it'; //same as the first example, the quotes in the string is not the same as the ones encapsulating the string, so they do not need to be escaped.
 
$string = "This is string has a \"quote\" in it"; //this time they need to be escaped with the \ other wise you will get an error.
 
Does this make sense?

Open in new window

nplib,
Perfectly!  Thanks for the fishing lesson.