Page 1 of 1

[SOLVED] String wrappers besides single and double quote

Posted: Thu Jul 05, 2007 7:18 pm
by HiddenS3crets
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've written a PHP function that takes in HTML code as its parameter.

In a file I pass in some code that displays a login form and I use javascript and essentially I run out of ways to parse quotes:

Code: Select all

<?php
function_name("
Login here: <a href='javascript:createPopup(\"
<!-- various html tags here -->
<input type=submit onClick=login(document.getElementsByTagName("username").item(0).value) value=login />
\")>login</a>
");
?>
The above code does not as shown :?

The problem is that I don't have any way to wrap the word 'username' inside my onClick.

Any ideas for a way around this?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Jul 05, 2007 7:32 pm
by vigge89
Use the backslash to escape the same kind of quotes inside a string like you (unintentionally?) did with the highlighted double quotes in the code you posted. Alternativey you can try the heredoc-syntax.

Posted: Thu Jul 05, 2007 7:38 pm
by HiddenS3crets
The problem is that I can't parse the quotes using a backslash createPopup() is already using \"

Shown simpler:

Code: Select all

createPopup(\"alert(\"username\")\");
As you can see that will close the alert function early, causing an error. That's basically the problem I'm having (I can't use single quotes around username either because I'm already using them; further more, parsing single quotes as \' doesn't work).

Posted: Thu Jul 05, 2007 7:40 pm
by feyd
Write it out without PHP involved. Once you get that, you can involve PHP.

Posted: Thu Jul 05, 2007 8:23 pm
by Christopher
I would recommend using double quotes in your HTML and single quotes in the embeded Javascript.

Posted: Thu Jul 05, 2007 8:25 pm
by vigge89

Code: Select all

<?php
function function_name($string) { echo $string; }
function_name("
Login here: <a href='javascript:createPopup(\"
<!-- various html tags here -->
<input type=submit onClick=login(document.getElementsByTagName(\\\"username\\\").item(0).value) value=login />
\")'>login</a>
");
?>
Things changed: escaped \ and " where you wanted \", added a single quote to the end of <input ...> since it was missing.
I would never use anything like this myself though, it's just plain nasty.