[SOLVED] String wrappers besides single and double quote

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

[SOLVED] String wrappers besides single and double quote

Post 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]
Last edited by HiddenS3crets on Thu Jul 05, 2007 8:56 pm, edited 1 time in total.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Post 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).
Last edited by HiddenS3crets on Thu Jul 05, 2007 7:40 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Write it out without PHP involved. Once you get that, you can involve PHP.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I would recommend using double quotes in your HTML and single quotes in the embeded Javascript.
(#10850)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
Post Reply