• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

confirm function

I have a contact page that i use in order to add delete or modify my contacts.I have also created a "back"  button that i use to navigate to the previous page.
I like to do a confirmation using the back button that is asking for saving or not saving ( in fact updating) a possible change.The problem is, using the "confirm" function, that when i choose (in the box that appears) "cancel" im not transferred to the previous page. (I suppose because the page does not submit)
How can i do this using the confirm function, or something else?

 
0
oceanis
Asked:
oceanis
  • 3
1 Solution
 
andrivCommented:
I'm a little confused about what you are trying to accomplish. This is what I assume.

You have a form with two buttons on to 'Update' and another that say 'Previous'. When the user clicks the 'Previous' button you want to confirm that whether to save or to go to the previous page?

If this is the case you are using the JavaScript script confirm box.  The way the confirm box works is if the user clicks 'OK' it returns 'true' to the script and if they click 'Cancel' it returns 'false' nothing more. It's up to you to write the code to do whatever when 'true' or 'false' is returned. If you want it to go to the previous page when the user clicks cancel you want to do something like this:

<html>
<head>
<script language=javascript>
function verifyConfirm()
{
if(!(confirm("To save changes select 'OK'")))
{
//If user clicks Cancel send to previous page
history.back();
}
else
{
//Do something else, send to next page or whatever
//it is you are trying to do
}
}
</script>
</head>
<body>
<form action="..." method="post">

form fields go here

<input type="submit" value="Save Changes">
<input type="button" value="Previous" onClick="verifyConfirm()">
</form>
</body>
</html>

Let me know if you need more help with it or if I am assuming wrong
0
 
andrivCommented:
You may have to change the history.back() to:

//If user clicks Cancel send to previous page
location.href=history.back();

Try the first way first and if it does not take you to the previous page then try the one above.
0
 
oceanisAuthor Commented:
adriv how can i write such code using php language?

0
 
andrivCommented:
You are better off using javascript because it's faster. If you do it with PHP you cannot display a pop-up.

You have to create your form in such away that if they click "Previous" one variable is set but if they click on "Save" that variable is set to something else.  Then when the user hits the submit button the form variable/value pairs get set to the script, and within the script you test to see what the variables value was set to and depending on it's value do something:

Example, if the person hits 'Previous' button say the variable 'action' was set to the value 'back' but if the click on 'Save' it's set to 'Yes'. The script can look like:

if($action == 'back')
{
display form with two buttons to ask if they want to save changes
also set action to Yes or No depending on which button they select, when they hit the submit button you have to do further conditions to determin how to handle it
}
elseif($action == 'Yes')
{
Save information
}
elseif($action == 'No')
{
//redirect to the previous page (this header must be
//before any content such as HTML is sent to browser

header("Location:path/to/the/previous/page.php");
}

As you can see this is not as easy as JavaScript and takes longer to get the final result.

0
 
kplonkCommented:
I think the javascript should be used foir simple validation, it is much nicer to see a pop up. Go with the andirv solution
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now