PHP form code malformed

I am creating a simple form with php. The following code has been reduced to illustrate the problem.

<?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'>
$formcode
</div>";

      

As a result of this html code

<h1>hello</h1>
<div id="form">
<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);" /&gt;
</form>

      

Somehow the form code for type = submit completes after my sendinfo () call. Why?

0


a source to share


2 answers


These are apparently escaping issues:

<?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'>
". $formcode . "
</div>";

      



Javascript in onclick needs single quotes or should it use ".

0


a source


This is because of your onclick quotes. Use single quotes instead:

"onclick=\"setTimeout(function() { sendInfo('$blah', '$test'); }, 1250);\" "

      



As of now, your onclick attribute actually reads like this:

onclick="setTimeout(function() { sendInfo("

      

+4


a source







All Articles