Can you make a form prompt the user to save changes when they exit?

I’ve created a button that prompts the user if they want to save changes. The function for the alert doesn’t return anything, so if you click cancel, it just goes back to the form. If you click ok, it updates the table, and redirects to another page. I tried the function sc_exit©, but this doesn’t seem to do what I want. Instead of all this, is there any default button I could use, or something else to do this instead of writing my own code?

Re: Can you make a form prompt the user to save changes when they exit?

Have a look at sc_confirm() and use them in your own buttons, or place them in any required event.
‘No’ cancels the activity.

Regards,
Scott.

Re: Can you make a form prompt the user to save changes when they exit?

I have used sc_confirm but it doesn’t seem to return a value, so I don’t know if the user clicked ok or cancel, like you said in another post, if their documentation was a little better, I wouldn’t be here asking these somewhat silly questions. If sc_confirm does return something, what is it?

Re: Can you make a form prompt the user to save changes when they exit?

also, is there an onExit() event, or would I have to do that with JavaScript or AJAX?

Re: Can you make a form prompt the user to save changes when they exit?

You could use JS/Form/onSubmit and add:
return confirm(‘Are you sure?’);

That would trap the Update button, but not the cancel. Myself I would not trap cancel if not changed.
You can trap a field change using sc_changed(field), but you will have to check each field. You may want to compare before after record buffer in there is this option instead of checking each field for a change.

You could also use in JS:

myfunction();
return myfunction() instead of confirm to get better control.

There is also AJAX where you could trap onClick

Regards,
Scott.

Re: Can you make a form prompt the user to save changes when they exit?

On thing that will about regarding the help is to search the net like you are not using SC and just you are using PHP, JS, AJAX and search for what you are trying to achieve. Once you get the answer, then determine how you are going to get SC to perform this and usually you will find you can move forward. I consider SC my shortcut tool, but use the net to learn how things are done.

Regards,
Scott.

Re: Can you make a form prompt the user to save changes when they exit?

im not a novice, and i do know a good amount, it’s SC that is tripping me up because of the documentation. I don’t get how some of the things work in SC. My question about what you just said is, if I put

return confirm('Are you sure?');

in JS/onSubmit part, where does that return value go, where do I check it to see whether they clicked ok or cancel?

Re: Can you make a form prompt the user to save changes when they exit?

As mentioned, you can have more control using your own function instead of using confirmed().

Something like:


function myconfirm() {
 var is_confirm = confirm("Are you sure?");

 if (is_confirm) { return true; } else { return false; } 

}

return myconfirm();

Re: Can you make a form prompt the user to save changes when they exit?

To further this, you may have perform this in AJAX, as the above code determines this if the OK was pressed and checks the answer, not if you click the cancel button in the form itself (I am slow sometimes :wink:

(See below instead of using AJAX)

Regards,
Scott.

Re: Can you make a form prompt the user to save changes when they exit?

Yes… I created a new JS button and added the code I presented in myconfirm and it worked. You will then have to handle the mysql calls (if any) yourself and drop the Exit/Cancel buttons from the toolbar.

Regards,
Scott.

Re: Can you make a form prompt the user to save changes when they exit?

i need to use php to do the SQL calls, so how would i be able to get the return value from the javascript function into php?

Re: Can you make a form prompt the user to save changes when they exit?

First, I just want to say thanks for all your help. I would still be stuck on my other problem if it wasnt for you. I’ve taken your advice and I’ve been looking on the net for a solution to this problem. I just need to get more familiar with the inner workings of scriptcase. If you could answer me this though, how do you do this?

You may want to compare before after record buffer in there is this option instead of checking each field for a change.

Re: Can you make a form prompt the user to save changes when they exit?

Well… a function from another library (win32) must have run over into my head when I commented on this.

In a db I used to use, they maintain a record buffer for this. I suspect this would be easy to mimic in code.

The only way I can see built into mysql is to use NEW/OLD in a trigger. As for SC, it seems you would have to create function using sc_changed for all fields, or go a little lower level and populate a PHP array on load and in onValidate compare the fields to that array, or something like that.
I was hoping for a sc_record_changed, but it does not exist.

Regards,
Scott.

Re: Can you make a form prompt the user to save changes when they exit?

Hi Guys,

If I understand you guys correctly, there’s no (standard) way in Scriptcase to check if data in a form has been changed other than check this for each field seperately?

Is there however a (standard) function/macro to save all unsaved data in one action? if so, does this solve your problem or not?

Re: Can you make a form prompt the user to save changes when they exit?

if there is a function to save all the fields, then I would like to know. right now I’m using a SQL UPDATE statement to save the changes to the record.

Re: Can you make a form prompt the user to save changes when they exit?

Sorry I jumped in this discussion withoud reading the whole thread… But could you please clarify what your goal is, what do you want to achieve? (from a user perspective).

Are you (like me) looking for a way to alert the user that there are unsaved changes when the user hits the exit, cancel or close window buttons?

Re: Can you make a form prompt the user to save changes when they exit?

What I want is, is a prompt that asks if the user wants to save changes made, to pop-up when the user clicks the exit button. I don’t think there is anything built-in, so I am trying to do it myself with code. I’m close, but I still haven’t gotten it to work. I’ll let you know if I do get it too work.

Re: Can you make a form prompt the user to save changes when they exit?

I hope the developers of SC read these forums, I thought I had this problem fixed, but it still isn’t. If the ‘sc_confirm()’ function actually returned a value, then this would be a simple solution. I don’t see what the point of having this function not return a value. Maybe this is something they can fix…

Re: Can you make a form prompt the user to save changes when they exit?

Alright, I figured it out, and it ended up being a simple solution. I know it would’ve helped me alot if someone else posted this before so here is the code I ended up with. I tried so many different things and this is what I ended up with, I made a javascript button, and added this code:

var ans = confirm("Would you like to save your changes?");
if(ans == true)
{
	nm_atualiza('alterar');
}

You would need to add a check for all the fields to see if they’ve been changed, but this prompts the user and if they click ‘Ok’ then it calls a function that updates the record. I knew there had to be a function to do this instead of writing my own code. Also, you would need to add the ‘else’ part to check if they clicked ‘Cancel’.

Re: Can you make a form prompt the user to save changes when they exit?

vivid, your code works fine. What I want however goes a bit further, but I cannot get it to work. Maybe you, or anybody else, can help?

Here’s what I want:
A button with label “Exit Form” on a simple form. When pushing this button the following code should be executed (in pseudo code):


if one or more form fields have been changed on the client
then
 ask user if he wants to save changes
 if he chooses yes, save and exit form
 else exit form
else (no form fields have been changed)
 exit form
end if

I took your piece of code and added code using sc_changed and sc_exit, but it doesn’t work as I hoped for. sc_changed doesn’t work at all, and sc_exit only works in php buttons according to documentation.

Can you help me? I need to do a demo on this next saturday…