Solved

confirm function

Posted on 2002-04-08
7
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JQuery Search Filter 2 71
MySqli Real Escape String and SQL Injection 1 50
Make checkboxes execute queries on click 8 37
if statement malfunction 5 25
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 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