Reputation: 153
I have a webpage test.php with:
a form
a "Submit" button to send the answers to the form;
a "Leave" button to allow the user to leave the page (and go to 'leave.php') without filling the form;
a Controller which controls the data when the user clicks on Submit, and redirects elsewhere when everything is correct.
I would like the user to confirm that (s)he wants to leave before redirecting. Here is my code so far:
function confirmation(){
var result = confirm("Do you really want to leave this page?");
if(result){
window.location = 'leave.php';
}
else{
return FALSE;
}
}
<form method="post" action="test.php">
<input type="text" name="answer" id="answer" placeholder="Type your answer here">
<input type="submit" name="submit_answer" value="Submit your answer">
<input type="submit" name="leave" value="Leave this page" onClick="return confirmation();">
</form>
I have two problems:
Upvotes: 0
Views: 727
Reputation: 326
If you have to Submit-Buttons each of them will submit your form. So you've got a conflict between the redirection to "test.php"
and "leave.php"
. In this case, the action from the form comes always first.
function confirmation(){
var result = confirm("Do you really want to leave this page?");
if(result){
window.location = 'leave.php';
}
else{
return FALSE;
}
}
<form method="post" action="test.php">
<input type="text" name="answer" id="answer" placeholder="Type your answer here">
<input type="submit" name="submit_answer" value="Submit your answer">
<input type="button" name="leave" value="Leave this page" onClick="return confirmation();">
</form>
Upvotes: 1
Reputation: 296
Here is a working version. I believe the issue was with the return statement, "return confirmation()":
<form method="post" action="test.php">
<input type="text" name="answer" id="answer" placeholder="Type your answer here">
<input type="submit" name="submit_answer" value="Submit your answer">
<input type="submit" name="leave" value="Leave this page" onClick="confirmation(); return false;">
</form>
<script>
function confirmation(e){
var result = confirm("Do you really want to leave this page?");
if (result) {
window.location = '/leave.php';
} else {
return FALSE;
}
}
</script>
Upvotes: 1
Reputation: 4605
You can submit the form using JavaScript. First, change both buttons to type="button"
. Using type="submit"
submits your button causing your errors. Then write a simple function to submit the form.
function confirmation(){
var result = confirm("Do you really want to leave this page?");
if(result){
window.location = 'leave.php';
}
else{
return FALSE;
}
}
function submitForm(){
var form = document.getElementById('form');
form.submit();
}
<form id="form" method="post" action="test.php">
<input type="text" name="answer" id="answer" placeholder="Type your answer here">
<input type="button" name="submit_answer" value="Submit your answer" onClick="submitForm()">
<input type="button" name="leave" value="Leave this page" onClick="return confirmation();">
</form>
Upvotes: 2