1
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

9 Answers 9

4

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' />";
Sign up to request clarification or add additional context in comments.

4 Comments

I was just concerned as the code is part of an if statement, so i wanted it to be included in that
This doesn't fix the problem, which is improperly nested quote characters.
@benhowdle89: you can do it with an if statement too. Check the updated answer.
@David Dorward: Nice. I have fixed that now.
3

You have

onsubmit='$('

Single quotes inside attribute values delimited with single quotes must be represented as &#39; 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

1 Comment

Although it might not be the best way to go, he could use double quotes within the onsubmit attribute and escape them. Best -
1

With that kind of variable, the heredoc concept would be pretty useful

$variable = <<<XYZ
........
........
XYZ;

Comments

0

Add @ sign in front of string like this

echo @" and now you can have multiple lines

I hope this helps

2 Comments

this is PHP, not .NET. @ supresses errors in PHP and is completely different from »do not interpret special characters« in .NET
jap I get it now, don't be angry
0

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.

Comments

0

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

Comments

0

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 .

Comments

0
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!

Comments

0

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.