tags:

views:

112

answers:

3

I am using the following code snippet to generate a html form for use in a popup window:

$uploadhtml = "<form action='up.php' method='post'
enctype='multipart/form-data'>
<label for='file'>Filename:</label>
<input type='file' name='file' id='file'/> 
<br />
<input type='hidden' name='pk' value='".$pk."'>
<input type='hidden' name='username' value='".$USERNAME."'>
<input type='submit' name='submit' value='Submit' onclick=\"setTimeout(function() { updateByPk('Layer2', '".$pk."', '".$brand."', '".$pg."'); } ),1250);\" />
</form>";

Everything is escaped/quoted as it should be.

I am using $uploadhtml like so:

echo "<p><a href='#' onclick=\"makewindows('" . $uploadhtml . "'); return false;\">Upload files</a>";

Why then is the escaped html the following?

<a href="#" onclick="makewindows('<form action='up.php' method='post'
enctype='multipart/form-data'>
<label for='file'>Filename:</label>
<input type='file' name='file' id='file'/> 
<br />
<input type='hidden' name='pk' value='380118185183'>
<input type='hidden' name='username' value='janmaybach'>
<input type='submit' name='submit' value='Submit' onclick=" settimeout(function()="" {="" updatebypk(="" layer2="" 380118185183="" ,="" );="" }="" ),1250);="">
'); return false;"&gt;Upload files</a>

It is absolutely fine until it gets to the onclick parameter, and I see no reason it should generate it so obscurely.

+1  A: 

double quotes instead of single quotes.

SilentGhost
What is the problem though? Double quotes are escaped where they need to be, and are otherwise only used to break out of the quoted string to concatenate a variable
Joshxtothe4
string goes from quote to quote. "..." - string with three dots, "...""." - two strings, one with three and another one with one dot.
SilentGhost
A: 

Because you are already inside a " and inside ':

<a href="#" onclick="makewindows('
                   ^^^          ^^^

you need to escape both your single and double quotes inside the variable, for example:

<input (...) value=\\'Submit\\' onclick=\\\"setTimeout

or just run it through an addslashes() function before inserting.

YiSh
ahhh. Thankyou. Why must I escape single quotes as well?
Joshxtothe4
It's because in the final HTML you are already inside a single quote - the one that is inside the JS makewindows() function.
YiSh
addslashes did not help:<a href="#" onclick="makewindows('"<form action='up.php' method='post'\r\nenctype='multipart\/form-data'>\r\n<label for='file'>Filename:<\/label>\r\n<input type='file' name='file' id='file'\/> \r\n<br \/>\r\n<input type='hidden' name='pk' value='380118185183'>\r\n<input type='hidden' name='username' value='janmaybach'>\r\n<input type='submit' name='submit' value='Submit' onclick=\"setTimeout(function() { updateByPk('Layer2', '380118185183', 'Ed Hardy', '1'); } ),1250);\" \/>\r\n<\/form>"'); return false;">Upload files</a>
Joshxtothe4
A: 

Use htmlspecialchars with ENT_QUOTES and json_encode if possible:

echo '<a href="#" onclick="makewindows('.htmlspecialchars(json_encode($uploadhtml), ENT_QUOTES).'); return false;">Upload files</a>';


Edit   And now the same in your quotation style:

echo "<p><a href='#' onclick=\"makewindows(".htmlspecialchars(json_encode($uploadhtml), ENT_QUOTES)."); return false;\">Upload files</a>";

You don’t need to surround the value you want to pass to the JavaScript function makewindows as json_encode already returns a valid string expression. And to avoid conflicts with the HTML attribute quotes, I used the htmlspecialchars function with ENT_QUOTES to replace both quote characters by corresponding HTML character references.

Gumbo
This results in: <a href="#" onclick="makewindows('"<form action='up.php' method='post'\r\nenctype='multipart\/form-data'>\r\n<label for='file'>Filename:<\/label>\r\n<input type='file' name='file' id='file'\/> \r\n<br \/>\r\n<input type='hidden' name='pk' value='380118185183'>\r\n<input type='hidden' name='username' value='janmaybach'>\r\n<input type='submit' name='submit' value='Submit' onclick=\"setTimeout(function() { updateByPk('Layer2', '380118185183', 'Ed Hardy', '1'); } ),1250);\" \/>\r\n<\/form>"'); return false;">Upload files</a> which is not clickable.
Joshxtothe4
No it doesn’t. You have to leave out the surrounding single quotes as the value returned by `json_encode` is already quoted. Try my example.
Gumbo
I cant change the quoted string containing the link to single quotes. The string I am using uploadhtml with is much longer, and needs double quotes. When I use your example in the double quoted string the above html is generated.
Joshxtothe4
I’m not changing the `$uploadhtml` value in any way. Just compare what I wrote to that what you wrote. If I change the quotation style to your’s, both codes look like this: "onclick=\"makewindows(" . […] . ");" – "onclick=\"makewindows('" . […] . "');" Do you see the difference?
Gumbo