?
Solved

confirm function

Posted on 2002-04-08
7
Medium Priority
?
401 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

800 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