Solved

confirm function

Posted on 2002-04-08
7
387 Views
Last Modified: 2008-03-10
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
Comment
Question by:oceanis
  • 3
7 Comments
 
LVL 5

Expert Comment

by:andriv
Comment Utility
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
 
LVL 5

Expert Comment

by:andriv
Comment Utility
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
 

Author Comment

by:oceanis
Comment Utility
adriv how can i write such code using php language?

0
 
LVL 5

Accepted Solution

by:
andriv earned 100 total points
Comment Utility
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
 

Expert Comment

by:kplonk
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now