Solved

confirm function

Posted on 2002-04-08
7
391 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
ID: 6925750
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
ID: 6925785
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
ID: 6928330
adriv how can i write such code using php language?

0
 
LVL 5

Accepted Solution

by:
andriv earned 100 total points
ID: 6929706
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
ID: 6940735
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

914 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

12 Experts available now in Live!

Get 1:1 Help Now