We help IT Professionals succeed at work.

syntax error using echo with Input with an onclick

ckellems
ckellems asked
on
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
Comment
Watch Question

Commented:
make it like this

echo '<input type="button" value="Go Home" onClick="window.location.href(\'FastDlr.php'\)" />';
Commented:
oops

put the second slash on the wrong side.
echo '<input type="button" value="Go Home" onClick="window.location.href(\'FastDlr.php\')" />';

Open in new window

Albert Van HalenAnalyst developer
CERTIFIED EXPERT

Commented:
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

Commented:
you can, but then it doesn't follow the xHTML 1.0 standard.
Albert Van HalenAnalyst developer
CERTIFIED EXPERT

Commented:
Just a few seconds behind.

Kudos to nplib ;-)

Author

Commented:
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"?  

Commented:
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

Author

Commented:
nplib,
Perfectly!  Thanks for the fishing lesson.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.