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?
Related
I have a php page with a form that submits to itself. On this same page I have a button that uses a javascript function to go back to the previous page.
My issue is that I want to avoid going back to the same page as it is possible to submit to the form multiple times and I cannot use a link because there are multiple ways to access this page.
Current script function:
<script type="text/javascript">
function goBack() {
window.history.back();
}
</script>
... to further clarify I am happy with the way the form is posting multiple times, I just need to fix the back button issue. Thanks!
Solution:
<?php
$previous = $_SERVER['HTTP_REFERER'];
if (isset($_POST["submit"])) {
$previous = mysqli_real_escape_string($link, $_POST['previous']);
}
?>
<html>
<form>
<input id="previous" type="hidden" name="previous" value="<?php echo $previous;?>">
...(rest of form)
</form>
BACK BUTTON
</html>
</form>
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?")' />
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']; ?>
So this is as should be on jsfiddle.
But when I put it within a .ascx page. Which loads within an .aspx page. The <form> decides to make itself null.
Anyone know why it is doing this?
More specifically:
document.getElementById('emvForm').submit();
The above line is returning null.
Okay so looking at the code I noticed I am getting:
<form id="1" runat="server">
<form id="emvForm">
</form>
</form>
Firefox ignores the second form because it is a nested form. The id remains the same it hasn't changed because runat not being defined on it.
Anyway of bypassing this?
Looking at your code you can better do this:
instead of calling:
document.getElementById('emvForm').submit();
Return true:
return true;
Then modify your input button to this:
<input type="submit" value="Submit Form" id="SubmitButton" class="contactFormButton submit emailValid" onClick="return validForm();">
Now when you return true the form will be submitted and where your return false in the validForm function the forms stops the submit.
I have been using different script for auto submitting form like:
document.getElementsByTagName('form')[0].submit();
But all these refresh for endless time without submitting the form. I tried using alert statement which worked fine but still the form was not submitted.
<form id="level" method="post">
<br/>
<label for="answer">Answer:</label>
<input type="text" name="answer" id="answer" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
.
With all your questions, I could try another conclusion.
If you're trying to make a brute-force attempt to retrieve a password, the server could have identified your ip and just stall your request because you've tried too often. Or even notice you're inhumanly fast with your request. These kind of things are a base defense against brute-force attacks.
The code should work fine.
Another guess could be that the page actually does something different when clicking that button, in stead of the normal submit. So you could try simulating a click on the button in stead of submitting the form directy.
$('#submit').click();
that line of code is right, maybe it is DOM issue, there is another <form> before or something else
you can try to access by ID
or jQuery :
$("#level").submit();