calling setTimeout from a form with quotes - php

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);"

Related

find and replace application - F&R multiple strings in one text

Hei. I would very much appreciate some help with this. I am creating a very simple "find and replace" application to generate links. I am using the code below to do this.
It now replaces the word "chocolate" with anything I'd like. But i would also like to change other words within the same text with the push of the same button.
Forexample; if i would like to change the word "loves" to "hates" as well.
How would i do this? Appreciate all the help i can get.
<?php
$offset=0;
if(isset($_POST['text']) && isset($_POST['searchfor']) && isset($_POST['replacewith']))
{
$text=$_POST['text'];
$search=$_POST['searchfor'];
$replace=$_POST['replacewith'];
$search_length=strlen($search);
if(!empty($text)&&!empty($search)&&!empty($replace))
{
$strpos=strpos($text,$search);
$text=substr_replace($text,$replace,$strpos,$search_length);
echo $new = str_replace(' ', '%20', $text);
}
else
{
echo 'pls fill in all fields';
}
}
?>
<form action='index.php' method='POST'>
<textarea name='text' style="display:none;" rows='6' cols='30'>http://andy.com/loves/chocolate/cake</textarea><br><br>
<input type='hidden' value= 'chocolate' name='searchfor'><br><br>
Replace the word chocolate with:</br>
<input type='text' name='replacewith'><br><br>
<input type='submit' value='find and replace'>
</form>
You can use arrays in str_replace, so this code will work using 2 comma separated valus in the input field:
$offset=0;
if(isset($_POST['text']) && isset($_POST['searchfor']) && isset($_POST['replacewith']))
{
$text=$_POST['text'];
$search=explode(",",$_POST['searchfor']);
$replace=explode(",",$_POST['replacewith']);
var_dump($replace);
$text=str_replace($search,$replace,$text);
echo $text;
}
<form action='#' method='POST'>
<textarea name='text' style="display:none;" rows='6' cols='30'>http://andy.com/loves/chocolate/cake </textarea><br><br>
<input type='hidden' value= 'chocolate,cake' name='searchfor'><br><br>
Replace the word chocolate with:</br>
<input type='text' name='replacewith'><br><br>
<input type='submit' value='find and replace'>
If you want to use 2 (or more) input fields use this code:
$offset=0;
if(isset($_POST['text']) && isset($_POST['searchfor']) && isset($_POST['replacewith']))
{
$text=$_POST['text'];
$search=explode(",",$_POST['searchfor']);
$replace=array($_POST['replacewith'],$_POST['replacewith2']);
var_dump($replace);
$text=str_replace($search,$replace,$text);
echo $text;
}
<form action='#' method='POST'>
<textarea name='text' style="display:none;" rows='6' cols='30'>http://andy.com/loves/chocolate/cake </textarea><br><br>
<input type='hidden' value= 'chocolate,cake' name='searchfor'><br><br>
Replace the word chocolate with:</br>
<input type='text' name='replacewith'><br>
Replace the word cake with:</br>
<input type='text' name='replacewith2'><br><br>
<input type='submit' value='find and replace'>

How to group form's parameters for $_POST?

Ex. (HTML file)
<form name='test' method='POST'>
<input type='text' name='name'>
<input type='text' name='surname'>
<input type='submit' name='sub'>
</form>
Now in the PHP file I'll get the $_POST like:
$_POST['name']= something
$_POST['surname']= something
etc...
What about if I want to "group" that to made a $_POST like this:
$_POST[name-of-the-form]['name']= something
$_POST[name-of-the-form]['surname']= something
etc...
How could I do it?
AbraCadaver is spot on here, all you do is name your form element accordingly.
e.g.
<input type='text' name='name-of-the-form[name]'>
Assuming you are doing this from a PHP file, you should use square braces andn the same name of in all element names:
<?php
$formName = 'test';
?>
<form name='<?=$formName?>' method='POST'>
<input type='text' name='<?=$formName?>[name]'>
<input type='text' name='<?=$formName?>[surname]'>
<input type='submit' name='sub'>
</form>

PHP Like Button Problems

I am trying to make a like button for posts on my website.
PHP for like query (d_db_update is
function d_db_update($string) {
return mysql_query($string);
}
)
if($_GET['like']) {
$like = d_db_update("UPDATE posts set post_rating = post_rating+1 WHERE post_id = {$_GET['like']}");
}
Button
<form action='{$_SERVER['PHP_SELF']}&like={$posts_row['post_id']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='submit' name='like' value='Like' /></p>
</form>
What can I do to fix it/make it work?
Use below form with a hidden input it solve your problem.
<form action='{$_SERVER['PHP_SELF']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='hidden' name='like' value='{$posts_row['post_id']}' />
<input type='submit' value='Like' /></p>
</form>
You are using your form action wrong.. if you are using get method than there is not need to use the form..
try this..
<a href='yourpage.php?like=<?php echo $post_id ?>'>Like</a>
your submit button name and like variable which you have used in action url are the same , and you used get method in method of form.So, you need to change the submit button name.
or
you can do it without using form only on button click try below code
<input type='button' name='like' value='Like' onclick="location.href='yourpage.php?like=<?php echo $post_id ?>'" />
Change your code to this
You can not write PHP variables using {}. You need to echo them out.
<form action='' method='get'>
<p align='left'><?php echo $posts_row['post_rating'] ?>
<input type='hidden' name='like' value='<?php echo $posts_row["post_id"] ?>' />
<input type='submit' value='Like' /></p>
</form>
Edit--
You were not returning the post id correctly, I made the changes, also there is no need to provide any action as it will be self only.

How would one echo this jQuery in PHP

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

Pass a php variable to dynamically generated div's onclick event

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\")'/>"

Categories