Confirm delete javascript, php - javascript

I am using a simple way to have confirmation box when deleting a record, problem here is I couldn't find where to place header code for redirecting to some other page after deletion. I have placed it after executing query and get this error
Cannot modify header information - headers already sent by (output started at ...
and I am not redirected to required page, but somehow on reloading page record wasn't there it was deleted.
if(isset($_POST['submit'])){
$que=$db->prepare("DELETE FROM blogs WHERE blogs_id = :blogId");
$que->execute(array(':blogId'=>$blogId));
header("location:front.php");
}
<form method="POST">
<input type="submit" name="submit" value="Delete" onclick="return confirm("Are you sure you want to delete this?")" />
</form>

replace
header("front.php");
with
echo "<script> window.location='front.php';</script>";

Use meta refresh tag instead of header to redirect the page
<meta http-equiv="refresh" content="0;url=http://example.com">

you need to use header("location:front.php"); instead of header("front.php");//but before header there should not be any echo or print otherwise it will not redirect.
your input for confirmation is wrong change it to this:
<input type="submit" name="submit" value="Delete" onclick='return confirm("Are you sure you want to delete this?")' />

Related

Submit form and redirect not working

EDIT: I should mention the form submits fine manually but with the javascript nothing seems to happen
I'm trying to get a form to autosubmit on page load and then redirect the user. Reason for this is this is part of a PHP page and the data needs to go into my database but then also POST the variables to a 3rd party SMS platform then return to the users dashboard.
My form looks like this:
<html>
<form action="https://www.example.com" id="myForm" method ="POST">
<input type="hidden" name="apiKey" value="withheld">
<input type="hidden" name="message" value="<?php echo $club_name ?> have requested you to play for them this weekend. Please login to your account to see more information and accept.">
<input type="hidden" name="to" value="<?php echo $to ?>">
<input type="hidden" name="from" value="withheld">
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</html>
This seems to work fine so I assume the Javascript is incorrect which is:
<script>
document.getElementById('myForm').submit();
window.location.replace("https://www.example.com");
</script>
You have to use a different name than
name="submit"
as all name attributes are set as properties on the form,
hence overriding the default "submit" property on the form,
and the default form method "submit()" is gone.
https://developer.mozilla.org/de/docs/Web/API/HTMLFormElement
"Named inputs are added to their owner form instance as properties, and can overwrite native properties if they share the same name (eg a form with an input named action will have its action property return that input instead of the form's action HTML attribute)."
In your code window.location.replace("https://www.example.com"); line won't make sense because submit() function will try to submit the form and will change the page and then replace function will prevent submitting the form and will redirect the page. The right way to do this via js can be, submit the form via ajax and in the success callback of Ajax run document.getElementById('myForm').submit()

How to append dynamic text to url in PHP?

I have a html form which method type is post, now i want to append text to my website url in PHP. The text changes dynamically.
current url :
www.example.com/index.php
expected url :
www.example.com/index.php/hotels-in-bangalore
(or)
www.example.com/index.php?qry=hotels-in-bangalore
I just want to append text, other than that every thing should remain same. Thank you.
Im not sure why you try to accomplish, but here are few solutions:
put url in form action:
<form action="index.php?qry=hotels-in-bangalore" method="post">
or you can do it in php
if (!empty($_POST['name']) {//put some logic here
header("Location: index.php?qry=hotels-in-bangalore");
exit;
}
You need to use JavaScript if you want to redirect to correct page after submitting immediately.
So it will be something like this:
<form>
<input type="text" name="tbSearch" id="tbSearch" />
<input type="submit" id="submitSearch" value="Submit" />
</form>
<script type="text/javascript">
$("#submitSearch").click(function() {
window.location.href="index.php?qry=" + encodeURIComponent($("#tbSearch").val());
});
</script>

how to open same page onsubmit? [duplicate]

This question already has answers here:
POST form and prevent response
(2 answers)
Closed 9 years ago.
I have html form whose action is a php script. php code basically replaces the file.
HTML code:
<form name="input" action="//copy.php" method="POST" onsubmit="return alertbox();">
<input type="hidden" name="path1" value=path to image 1/>
<input type="hidden" name="path2" value='path to image 2' />
<input type="submit" name="submit" value="Copy image"/>
</form>
Php code:
if isset($_POST['submit']))
{
$image1 = $_POST['path1'];
$image2 = $_POST['path2'];
copy($image1, $image2);
}
?>
Now when I click submit, a alert box opens that "file is updated successfully" and when I click ok on it, a blank page load. How can I avoid loading the blank page? I want to stay on the same page after clicking submit with pop up msg.
SOLUTION
As I don't see "Answer your own question" option, I am posting solution here.
This link POST form and prevent response, gives you textual answer to the question. While I providing the answer by code.
So basically, it very simpl. Just put
header("HTTP/1.0 204 No Response");
in the php file and it will work successfully on all browser, without opening new page. This will avoid use of jquery.
Leave action empty.
<form method="post" action="">
Then check if posted using isset function
if(isset($_POST)){
...
}
This will keep you on the same page after submit button..
<form name="input" action="" method="POST" onsubmit="return alertbox();">
<input type="hidden" name="path1" value=path to image 1/>
<input type="hidden" name="path2" value='path to image 2' />
<input type="submit" name="submit" value="Copy image"/>
</form>
But now, your image(s) may not be loaded. Maybe it will work, maybe not. If not, you will need to resolve functions which may reside in copy.php file. Since we dont know whats in that file, its hard to answer your question correctly, but.. you may try this "blind" shot..
if(isset($_POST['submit']))
{
//include "//copy.php"; // this file probably contains functions, so lets load functions first IF needed..
$image1 = $_POST['path1'];
$image2 = $_POST['path2'];
copy($image1, $image2);
}
Have you tried changing the type on the input:
<input type="submit" name="submit" value="Copy image"/>
to
<input type="button" name="submit" id="submit" value="Copy image"/>
<input type="button" /> won't submit a form by default (check all browsers to be sure).
<input type="submit"> by default, the tag in which the submit input is, is submitted. If you still want to use this, you will have to override the input submit/button's functionality with an event.preventDefault(). In that case, you need:
$('#submit').click(function (event) {
event.preventDefault();
//Do whatever you need to
//Submit if needed: document.forms[0].submit();
});
For further details refer this link
action="<?php echo $_SERVER['PHP_SELF']; ?>

reload form on submit with latest post data

I have a form to post content into a database. The existing database content for the form is posted into the form as the value. enalbeing the form to show the existing database content.
On submit the database is updated and to view the newly updated content in the form the page must be reloaded.
I have produced a reload script in javascript to reload the page on submit. The page reloads but the php content doesn't update. The page still need to be reloaded manually for the new content to show up.
This is the code for my form.
<form method="POST" action="">
<input type="text" name="title" <?php echo "value=\"" .$row['title']."\">"?>
<textarea id="editor" name="content"><?php echo $row['content']; ?></textarea>
<input type="submit" name="save" value="Save" class="submit" onclick="reload();">
</form>
Javascript
function reload(){
document.location.reload(true);
}
I have also tried
window.location = window.location.href;
Both are relaoding the page but the php isn't being refreshed.
you should first update the db with submitted value before selecting the records to display in the form value.
use <?php $_SERVER['PHP_SELF'] ?> in form action.
mysql_query("UPDATE xyz SET title=$_request['title'],... WHERE id = 1") .
2.Then select query mysql_query("SELECT * from xxx where id =1").
These may solve your problem of reloading to get new values.
java script excecute only on the client side. php is Server side. you need to reload the PHP.
<form method="POST" action="<<NAME OF YOUR PHP>>.php">
<input type="text" name="title" <?php echo "value=\"" .$row['title']."\">"?>
<textarea id="editor" name="content"><?php echo $row['content']; ?></textarea>
<input type="submit" name="save" value="Save" class="submit" onclick="reload();">
</form>
Is there a reason it needs to be done with ajax? If you don't need ajax it's better to handle it with php. ajax is more work and doesn't have the same rate of success as submitting a form via php, sometimes weird things happen. You can just do a redirect after saving the form:
header("Location: /routeToYourPage&id=".$ID,TRUE,303);

Why doesn't javascript redirect work?

I have a form that should redirect the user to a page when clicking the Delete button. This is the only object in the form.
<input type="submit" name="delete" value="Delete" class="submitbutton" id="submitbutton" onclick="Redirect();">
Unfortunately the redirect is not working:
<script type="text/javascript">
function Redirect()
{
alert('b');
window.location="http://www.tutorialspoint.com";
}
</script>
The alert is displayed. Then nothing happens. I also tried window.navigate. I am pulling my hairs out.
There is a session in the beginning of a page if that matters:
<?php
session_start();
?>
<html>...
I tried in chrome and firefox. I am clearly missing something.
It's probably running your script and then submitting the form.
You should use:
<input type="button" ...>
Instead of:
<input type="submit" ...>
It was in front of my eyes the whole time:
The accepted solution on this page:
Try to combine javascript confirm box with php post method?

Categories