问题
echo "<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'
onsubmit='$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )'>
<pre><input id='excel' type='image' src='img/file.png'></pre>
<p id='save'>Save table data to Excel</p>
<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />
</form>
<input class='noPrint' type='button' id='print' value='Print' />";
When i run the page, i dont get a parse error, however ').append( $('#dataTable').eq(0).clone() ).html() )'>
actually shows on the page, therefore the jQuery doesn't work!
How can i include it in the echo correctly?
Thanks
回答1:
Why not skip the echo
altogether like this:
//your PHP code before this echo statement
//let us say this is part of a IF statement
if(true)
{
?>
<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'
onsubmit="$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )">
<pre><input id='excel' type='image' src='img/file.png'></pre>
<p id='save'>Save table data to Excel</p>
<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />
</form>
<input class='noPrint' type='button' id='print' value='Print' />
<?php
} //if ends here
// continue with your PHP code
EDIT: You also have a improperly nested quote characters in onsubmit
. The code given above ALSO fixes that by converting those quotes to double quotes.
You can also use echo and escape those quotes like this:
echo "<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'
onsubmit=\"$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )\">
<pre><input id='excel' type='image' src='img/file.png'></pre>
<p id='save'>Save table data to Excel</p>
<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />
</form>
<input class='noPrint' type='button' id='print' value='Print' />";
回答2:
You have
onsubmit='$('
Single quotes inside attribute values delimited with single quotes must be represented as '
so they get treated as data and not the other end of the attribute value.
Also, and credit to knittl, double quote delimited strings in PHP interpolate variables. So you need to escape the $
signs for PHP.
This would also be better written using:
- Unobtrusive JavaScript
- Thus keeping the JS in a separate file and not having to worry about nested quotes
- A block of HTML instead of an
echo
statement (conditionals wrapped around it still apply)- Letting you avoid having three levels of quotes (PHP, HTML, JavaScript)
- Avoiding having to worry about variable interpolation in PHP
回答3:
With that kind of variable, the heredoc concept would be pretty useful
$variable = <<<XYZ
........
........
XYZ;
回答4:
Add @ sign in front of string like this
echo @" and now you can have multiple lines
I hope this helps
回答5:
You shouldnt use echo for this, you can just safely stop PHP for a sec. The error seems to be in the HTML, as such:
onsubmit='$('#datatodisplay').
HTML thinks the onsubmit is only $(, you should use " instead.
回答6:
there are several issues …
php substitutes variables in double quoted strings ("$var"
), with their value.
you'd either have to use single quotes and escape the other single quotes, or use heredoc:
echo <<<EOT
<form class="noPrint" action="demo/saveToExcel.php" method="post" target="_blank"
onsubmit="$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )">
<pre><input id="excel" type="image" src="img/file.png"></pre>
<p id="save">Save table data to Excel</p>
<pre><input type="hidden" id="datatodisplay" name="datatodisplay" />
</form>
<input class="noPrint" type="button" id="print" value="Print" />
EOT;
you can also output your html directly, without php
// suspend php script
?>
<form class="noPrint" action="demo/saveToExcel.php" method="post" target="_blank"
onsubmit="$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )">
<pre><input id="excel" type="image" src="img/file.png"></pre>
<p id="save">Save table data to Excel</p>
<pre><input type="hidden" id="datatodisplay" name="datatodisplay" />
</form>
<input class="noPrint" type="button" id="print" value="Print" />
<?php // resume php script
furthermore, javascript event handlers should not be declared inline. use javascript to create and apply them to DOM elements
回答7:
try this
echo "<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'
onsubmit='$(\"#datatodisplay\").val( $(\"<div>\").append( $(\"#dataTable\").eq(0).clone() ).html() )'>
<pre><input id='excel' type='image' src='img/file.png'></pre>
<p id='save'>Save table data to Excel</p>
<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />
</form>
<input class='noPrint' type='button' id='print' value='Print' />";
you have '$('#datatodisplay').
html parses this as '$('
then takes the first >
which is in <div>
and print all whats after .
回答8:
echo "<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'";
echo "onsubmit='\$(\'#datatodisplay\').val(\$(\'<div>\').append(\$(\'#dataTable\').eq(0).clone() ).html() )'>";
echo "<pre><input id='excel' type='image' src='img/file.png'></pre>";
echo "<p id='save'>Save table data to Excel</p>";
echo "<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />";
echo "</form>";
echo "<input class='noPrint' type='button' id='print' value='Print' />";
try this! ;) you have to escape $ with \ and there shouldnt be \n (new line in response) so thats why multiple echos or you can all put to variable and then echo it at end!
回答9:
Sorry, this is right answer lol
echo "<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'
onsubmit=\"$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )\">
<pre><input id='excel' type='image' src='img/file.png'></pre>
<p id='save'>Save table data to Excel</p>
<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />
</form>
<input class='noPrint' type='button' id='print' value='Print' />";
Why are you so angry. hehe :)
来源:https://stackoverflow.com/questions/4784012/how-would-one-echo-this-jquery-in-php