Need help echoing out a input button using PHP - php

I am trying to echo out this following input submit button Using php.. .
<input type='submit' value='submit' onClick='parent.location='order_summary.php'>
I wrote the following code before but its not working :(
Can anyone please help me out with this? Thanks!
echo "<input type='submit' value='submit' onClick='parent.location="."'order_summary.php'".">";

You need to learn about escaping quotes so you don't write broken HTML:
echo "<input type='submit' value='submit' onClick='parent.location=\"order_summary.php\"'>";
or alternatively (since valid HTML uses double quotes):
echo '<input type="submit" value="submit" onClick="parent.location = \'order_summary.php\'">';

Related

Update on a mysql row not working

I'm trying to update a column value in a mysql table, but nothing is happening at all.
It's neither showing the update message nor the error message.
html form:
<html>
<form action='xyz.php' method='post'>
<input type='text' size='25' id='country' name='country'>
<input type='submit' id='submit' value='update'>
</form>
</html>
php code:
<?php
session_start();
$con=mysqli_connect("localhost","root","","server2go");
if(mysqli_connect_errno())
echo "<h2>Failed to connect to MySQL database!".mysqli_connect_errno();
if(isset($_POST['submit'])){
$country=$_POST['country'];
$userid=987654326;
$sql2=mysqli_query($con, "UPDATE user_profile set country='$country' WHERE userid='$userid'");
if(!$sql2){
die(mysqli_error($con));
echo "<h6>Failed to update</h6>";
}
else {
echo "<h5>Successfully updated!</h5>";
}
}
?>
what have i done wrong here?
plz help.
The reason why your code is not executing, is that you've set a conditional statement
if(isset($_POST['submit'])){...}
but did not name your submit button:
<input type='submit' id='submit' value='update'>
rename to:
<input type='submit' id='submit' value='update' name='submit'>
You cannot rely on id alone when it comes to POST variables.
Footnotes:
Your present code is open to SQL injection.
Use prepared statements, or PDO with prepared statements.
To help protect yourself from an SQL injection attack while using what you have now till you hopefully move over to prepared statements, use:
$userid = (int)987654326; // assuming it's an integer
$country = stripslashes($_POST['country']);
$country = mysqli_real_escape_string($con,$_POST['country']);
Please change
<input type='submit' id='submit' value='update'>
To:
<input type='submit' id='submit' value='update' name='submit'/>
Because, no name attribute is set and control is not going to this condition.
if(isset($_POST['submit'])){
Hope it works for you.
change your submit input in the form
from this
<input type='submit' id='submit' value='update'>
to this
<input type='submit' id='submit' name='submit' value='update'>
mysqli does not automatically secure your query. bind your value.
always leave the error reporting at the time of production by using this line at the top
error_reporting(E_ALL);

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.

Variable in echo statement correction

The code below parses ok but is not working, I have tried several versions of how its written and the best i have been able to get is the redirect to work but the PHP variable is not placed in the passed url. I'm having real difficulty in figuring out when/where single quote vs double quotes are needed/used when the code has a lot of combinations of both. Using AJAX or JQUERY etc. etc. isn't an option for me in this case so even though this could be approached differently, please stick with the HTML/PHP code. Thanks in advance.
echo '<input name="Edit" type="button" value="Edit Product" onclick="location.href=\"add-firearm.php?eval=edit&eitem=.\"$row[SKU]\" " />';
As is, your echo produces:
<input name="Edit" type="button" value="Edit Product" onclick="location.href=\"add-firearm.php?eval=edit&eitem=.\"$row['SKU']\" " />
You want
<input name="Edit" type="button" value="Edit Product" onclick="location.href='add-firearm.php?eval=edit&eitem=$row['SKU']'" />
So your echo should look like:
echo '<input name="Edit" type="button" value="Edit Product" onclick="location.href=\'add-firearm.php?eval=edit&eitem=' . $row['SKU'] . '\'" />';
echo "<input name='Edit' type='button' value='Edit Product' onclick='location.href='add-firearm.php?eval=edit&eitem=".$row[SKU]."' />"
You forgot the '.''s.

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

calling setTimeout from a form with quotes

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

Categories