Expanding quoted string in another string
I have two variables containing some html code and another variable containing the code for the html form. I am trying to expand the string in the second to pass it as a parameter to a function, however this is throwing some errors.
My pop popup function is very simple:
function popup(htmlcode){
child1 = window.open ("about:blank");
child1.document.write(htmlcode);
child1.document.close();
}
Code that uses the above function
<?php
$blah = "<h1>Well</h1>"; $test = "<h2>Done</h2>";
echo '<script type="text/javascript" src="fetchlayers.js"></script>';
$formcode = "<form action=\"process.php\" method=\"post\" enctype=\"multipart/form-data \">
<label for=\"file\">Filename:</label>
<input type=\"file\" name=\"file\" id=\"file\"/>
<br />
<input type=\"submit\" name=\"submit\" value=\"Submit\" onclick=\"setTimeout(function() { sendInfo(\"".$blah."\", \"".$test."\"); } ),1250);\" />
</form>";
echo "<h1>hello</h1>
<div id='form'>
<a href='#' onclick=\"popup('" . htmlentities($formcode) . "'); return false;\">
click here</a>
</div>";
This produces good enough html code, however firebug is giving me the error that I have an inexhaustible line in the side direction. I cannot find where it is. I realize that I didn’t do it perfectly, but I am learning and don’t know the best way at the moment. I appreciate any input
edit: OK, so the problem was that I had inexhaustible string literals that were \ n characters. I made the line in one line and named it correctly.
Is it not possible to split one echo statement into multiple lines?
Now the problem is with the html generated in the popupwindow. Some of the code is actually printed on the screen, why is that?
<form action="process.php" method="post" enctype="multipart/form-data "><label for="file">Filename:</label><input name="file" id="file" type="file"> <br><input name="submit" value="Submit" onclick="setTimeout(function() { sendInfo(" type="submit"><h1>Well</h1>", "<h2>Done</h2>"); },1250);" /></form>
See the image here :
a source to share
If you absolutely need to keep the popup menu as it is, I found a solution using this answer at How do I avoid a line inside javascript inside an onClick handler? ".
<?php
$blah = "<h1>Well</h1>"; $test = "<h2>Done</h2>";
echo '<script type="text/javascript" src="fetchlayers.js"></script>';
$formcode = '<form action="process.php" method="post" enctype="multipart/form-data ">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"/>
<br />
<input type="submit" name="submit" value="Submit" onclick="setTimeout(function() { sendInfo(\\x27'.$blah.'\\x27, \\x27'.$test.'\\x27); }, 1250);" />
</form>';
echo "<h1>hello</h1>
<div id='form'>
<a href='#' onclick='popup(\"" . addslashes(str_replace("\n", ' ', $formcode)) . "\"); return false;'>
click here</a>
</div>";
?>
Before editing:
Perhaps you can do it differently.
Javascript functions:
function popup(id, params){
var html = document.getElementById(id).innerHTML;
if (params != undefined) {
html = findAndReplaceStrings(html, params);
}
var child1 = window.open ("about:blank");
child1.document.write(html);
child1.document.close();
}
function findAndReplaceStrings(text, json) {
for (var x in json) {
text = text.replace(x, json[x]);
}
return text;
}
HTML code:
<div style="display:none;" id="process">
<form action="process.php" method="post" enctype="multipart/form-data ">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"/>
<br />
<input id="submit" type="submit" name="submit" value="Submit" onclick="setTimeout(function() { sendInfo('{param1}', '{param2}'); } ), 1250);" />
</form>
</div>
HTML link with json:
<a href="#" onclick="popup('process', {'{param1}':'<h1>Well</h1>', '{param2}':'<h2>Done</h2>'}); return false;">
click here</a>
a source to share
You should avoid carriage return (\ n) by doing
$formcode = str_replace("\n", "\\n", $formcode);
You also need to escape quotes
$formcode = str_replace("'", "\\'", $formcode);
You can combine these two lines into one:
$formcode = str_replace(array("\n", "'"), array("\\n", "\\'"), $formcode);
a source to share
The submit button has an additional )
one that closes the setTimeout function too early. A special place is inside:
} ),1250
you should also consider using single quotes inside the php string to make things easier to read. And since you are using double quotes, you don't need to break out of the string to insert the contents of the $ blah and $ test variables.
something like this should work:
$formcode = "...
<input type='submit' name='submit' value='Submit'
onclick='setTimeout(function() { sendInfo(\"$blah\", \"$test\"); },1250);' />
...
";
EDIT:
it looks like closing onclick is too early. Comparing them as start and end quotes:
onclick=\"setTimeout(function() { sendInfo(\"
I changed the sendInfo line to the following, ran it and looked like it was working. The single quote is hidden here, so it does not prematurely close the popup () call.
sendInfo(\'".$blah."\', \'".$test."\');
a source to share