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
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' />";
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
With that kind of variable, the heredoc concept would be pretty useful
$variable = <<<XYZ
........
........
XYZ;
Add # sign in front of string like this
echo #" and now you can have multiple lines
I hope this helps
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.
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
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 .
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!
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 :)
Related
I'm making a Quiz. And with each question I'm showing the possible answers( "True" or "False") with a While loop in PHP:
echo "<form method='post' action='quizCheck.php'>";
while(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer' value='true' />
</label>
<label>False
<input type='radio' name='answer' value='false' />
</label>
</div>";
}
echo "</form>";
Let's say there are 10 questions and I select "True" on 6 questions.
What code do I have to put in quizCheck.php so it can count the number of "True" answers and store it in a variable?
You will need to do two things, first you need a submit button in the form:
<button type="submit" value="Submit">Submit</button>
Then you will also need the names of the radio inputs to be unique so in the while loop (which you really should just change to a for loop) do:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer{$x}' value='true' />
</label>
<label>False
<input type='radio' name='answer{$x}' value='false' />
</label>
</div>";
}
When the form is submitted, then in quizCheck.php you just check $_POST[answer0] through $_POST[answer9] to see which are true and increment a counter.
If you want the answers in a single array then do this:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answers[$x]' value='true' />
</label>
<label>False
<input type='radio' name='answers[$x]' value='false' />
</label>
</div>";
}
When this form is submitted, then in quizCheck.php you just get something like $answers = $_POST[answers] and then go through answers[0] to answers[9] for example
I have a popup plugin. Whenever i click the link, the things inside element_to_pop_up DIV are written in the popup window. However i added a function which is not appearing in the popup, it is showed outside of it in the main page. Why does that happen?
I guess that the dots make this function get echoed but they are out of the element to pop up DIV. How to get over it?
function writecomments($photoid){
echo $photoid;
}
echo "
<div class='element_to_pop_up'>
".writecomments($photoid)."
<img id='stop' src='".$numphotos['link']."' alt='photo' class='photolink' align='middle'>
<form action='main.php' class='commentsform' method='post'>
<textarea rows='8' cols='80' name='comments'></textarea> <br />
<input type='hidden' name='pid' value='".$photoid."'>
<input type='submit' name='send' value='Wyślij'>
</form>
<a class='b-close'></a>
</div>";
}
I am using bpopup plugin
http://dinbror.dk/blog/bPopup/
Source code:
<div class='element_to_pop_up'>
writecomments(302)
<img id='stop' src='upload/Dzuliet_3.jpg' alt='photo' class='photolink' align='middle'>
<form action='main.php' class='commentsform' method='post'>
<textarea rows='8' cols='80' name='comments'></textarea> <br />
<input type='hidden' name='pid' value='302'>
<input type='submit' name='send' value='Wyślij'>
</form>
<a class='b-close'></a>
To do it correctly it is needed to use return instead of echo
function writecomments($photoid){
return $photoid;
}
Don't echo,just return it :)
function writecomments($photoid){
return $photoid;
}
I have an echo statement that is supposed to run a specific amount of times, i.e 'n' times, right now the function abc() is empty, for testing purposes, what I'm trying to to is this:-
echo "
<form method=\"post\" action=\"<?php abc(); ?>\" >
<input type='text' name='comment' style='width: 80%;height: 70px; margin-left: 60px' /> <br/>
<input type='submit' value='submit comment' style='margin-left:60px' />
</form>
";
but every time I click the button to submit the form I get the error
Forbidden
You don't have permission to access /< on this server.
If what I am trying to do isn't possible, is there an alternative way?
What I want to do is; create a button which, when clicked, calls a php function (which may or may not reload the page, doesn't really matter). There will be multiple functions created via a loop and for each loop iteration the values passed to the function will be different. By values, I don't mean variable types, I mean variable values will be different. I know there aren't any variables passed to the abc function at the moment, but like I said, abc function is for testing only to try to get past the forbidden error.
What I am actually trying to do is this..
$doubleinverted='"';
echo "
<form action=".$doubleinverted."<?php f_comment(".$row['ScrapId'].",'".$row1['Email']."');?>".$doubleinverted." method='post'>
<input type='text' name='comment' style='width: 80%;height: 70px; margin-left: 60px' /><br/>
<input type='submit' value='submit comment' style='margin-left:60px' />
</form>
";
I know you can add inverted commas like \", but I just found that out.
Also, this echo statement will be in a loop and for each iteration, the values passed to the function will be different
You cannot use PHP blocks inside of echo statements.
If f_comment echoes out a string, you should be doing something along the lines of:
echo "blah blah blah";
f_comment(...);
echo "more blah blah";
If it returns a value, store it in a variable or concatenate the string:
$string = "blah blah blah";
$string .= f_comment(...);
$string .= "more blah blah";
echo $string;
echo "
<form method=\"post\" action=\"<?php abc(); ?>\" >
<input type='text' name='comment' style='width: 80%;height: 70px; margin-left: 60px' /> <br/>
<input type='submit' value='submit comment' style='margin-left:60px' />
</form>
";
The action of the form is <?php abc(); ?> while you are already in PHP mode. I'm afraid I can't let you do that Dave!
Change the form to <form method=\"post\" action=\"' . abc() . '\" >
While you are at it, get rid of the confusing quote escaping. This reads more clearly...
<?php
echo '<form method="post" action="' . abc() . '">
<input type="text" name="comment" style="width: 80%;height: 70px; margin-left: 60px" /> <br/>
<input type="submit" value="submit comment" style="margin-left:60px" />
</form>';
?>
how to pass a php variable to dynamicaly generated div's onclick event?
here is my code
while($rowset = mysql_fetch_array($result))
{
$idTT1++;
$editTT='b'.$idTT1;
$hidTextTT='t'.$idTT1; $hidSubIdTT='s'.$idTT1;
echo "<tr><td>".$rowset[1]." ".$rowset[2]."</td><td> </td>
<td class='table_label'>
<div id='".$idTT1."' onclick='editView($idTT1,$editTT)' >".$rowset[3]."</div>";
echo "<div id='".$editTT."' style='display:none;'>
<input id='".$hidSubIdTT."' name='box1' type='hidden' value='".$row['dDegreeName']."'>
<input id='".$hidTextTT."' name='box2' type='text' value='' />
<input type='submit' value='Update' name='submit'
onclick='updateSubject($hidSubIdTT,$hidTextTT)'/>
<input type='button' value='Cancel' name='Cancel' onclick='setEditView($idTT1,$editTT)'/>
</div></td></tr>";
}
i want to pass 2 variables $idTT1 and $editTT. im getting the value of $editTT1 in javascript but i cant get value of $editTT value in editView() javascript function.
That is probably because you are not quoting the IDs. Try
setEditView(\"$idTT1\",\"$editTT\")
in the line before last.
You need to put it between quotes. Something like this should work:
"<input type='button' value='Cancel' name='Cancel' onclick='setEditView(\"$idTT1\",\"$editTT\")'/>"
I have a html form for uploading a file, which is as follows:
$uploadhtml = htmlspecialchars(json_encode("<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= />
</form>"), ENT_QUOTES);
I would like to know if it is possible to call the setTimeout function to update a particular layer, like follows:
onclick="setTimeout('updateLayer("text", "ff", "ok"))',1250);"
updateLayer takes 3 variables as arguments, how would I specify them as parameters within quotes?
Something like this:
onclick="setTimeout(function() { updateLayer('text', 'ff', 'ok'); } ),1250);"
You can also backslash the quotes. Note that this only works with " qoutes and not ' quotes in php, but works with both quotes in javascript:
onclick="setTimeout(function() { updateLayer(\"text\", \"your's\", \"ok\"); } ),1250);"