This topic is locked

Confirm dialog box before saving form

3/16/2011 11:38:36 AM
PHPRunner Tips and Tricks
F
FunkDaddy author

So, previously in version 5.2 I could easily prompt a user with a javascript confirm dialog box that checked whether they had selected a checkbox in my edit form before saving that form. The idea was to simply ask the user whether they really wanted to save the form given that they had not checked-off a specific checkbox on the page. Basically a simple "are you sure" type of reminder before saving.
The way I did this in 5.2 was as follows:
I added simply this bit of JS directly in HTML via Visual Editor (in the HEAD tag of my page):



<script language=Javascript>

function decision(){

if (document.forms.editform1.value_myfield_1.checked == 0)

{

if (confirm('You have not checked off the "myfield" box. Are you sure you want to save?')) document.forms.editform1.submit(); else return false;

} else document.forms.editform1.submit();};

</SCRIPT>


However, this didn't method stopped working in version 5.3 because of the new "popup" functionality of add/edit/view forms. This new feature breaks the old model of using "editform1" and "value_myfield_1"... so we can no longer reference those fields directly ahead of time. The new popup forms are generated on the fly, thus you have to use the PHPR API to refer to objects such as var ctrl_Home_Phone= Runner.getControl(pageid, 'Home_Phone'). Unfortunately, the API left out a way for us to refer to the "SAVE" button on the form using the JS onLoad event... so, even though I was able to attach onclick events to the save button (via jquery and/or directly adding it as an attribute in HTML) it nonetheless would always fire the default pre-built save event with the form as well (in other words, I would not stop the event from saving, even when selecting cancel action in the JS confirm dialog box).
Long story short... with a little help from support here is some code you can use to create a confirm dialog when saving a form in 5.3 (place in JS onLoad event of edit and/or add form):


var ctrl_My_Field= Runner.getControl(pageid, 'My_Field');
this.on('beforeSave', function(formObj, fieldControlsArr, pageObj){

if(ctrl_My_Field.getValue() == 0) {

if (confirm('You have not checked off the "myfield" box. Are you sure you want to save?')) {return true;} else {return false};

} else {

return true;

}

}

);


That's all. Cheers,

F
FunkDaddy author 5/6/2011

Quick note & tip...
I came across the need to display a confirmation dialog box before saving and edit form that would check $oldvalues["YourFieldName"] versus $values["YourFieldName"].
At first my initial reaction was to script some JS that pulled PHP variables/values from the form, then responded accordingly. Well, this can be tricky if you are not referring to global PHP variables like $_SESSION["whatever"], etc.
Instead, I used a simpler approach. In the "JS OnLoad" event of the edit form I simply declared a variable at top f my script that would capture the value of my field that was present upon initial load of my form (this would be the equivalent of $oldvalues) then I simply used the same script sample as above, but added a condition that would check the new value of the variable against the one I capture upon load.
Here's an example to clarify:



var ctrl_LinkFamilyID = Runner.getControl(pageid,'LinkFamilyID');

//capture existing value before any edits.

var LinkFamilyID_OLD_VALUE = ctrl_LinkFamilyID.getValue();
//We check the old LinkFamilyID value and warn user if they selected a new one to.

this.on('beforeSave', function(formObj, fieldControlsArr, pageObj){

if(ctrl_LinkFamilyID.getValue() != LinkFamilyID_OLD_VALUE) {

if (confirm('WARNING:\r\nWe found a different value, this will link this record with another family. Are you sure you want to do that?')) {return true;} else {return false};

} else {

return true;

}

}

);
G
giwi_dev 7/30/2011

thanks for posting this
I was looking for that beforeSave jscript function in the first post, is there a list of available jscript functions for a page?
Thanks

C
cgphp 7/30/2011

Here is a more compact version of the same code:



var ctrl_My_Field= Runner.getControl(pageid, 'My_Field');
this.on('beforeSave', function(formObj, fieldControlsArr, pageObj){

if(ctrl_My_Field.getValue() == 0)

return confirm('You have not checked off the "myfield" box. Are you sure you want to save?');

return true;

});
C
choanhemnhe 11/3/2011

Thanks FunkDaddy!

Your code for PHP5.3 works like a charm

<img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=61979&image=1&table=forumreplies' class='bbc_emoticon' alt=':rolleyes:' />