njdubois Posted May 9, 2013 Share Posted May 9, 2013 I have a data entry form that has required fields. I want to disable the ability for a user to browse away from this page until either these required fields are filled out, or they choose to cancel. I found this on the net : window.onbeforeunload = function() { return 'You have unsaved changes!';} But it is not going to do what I need it to do. The user can still leave the page ignoring required fields, and not giving me the chance to delete the record. Thus filling our database with bad data. Ideally, The user starts filling out the form and for whatever reason needs to leave the page with the form, say they click the home link or maybe goes to google.com. If any of the required fields are not filled out, I would like the code to prevent the page from changing, and to prompt the user using html and NOT the onbeforeunload dialog. IE: Have some red text populate a div at the top of the page stating "A required field is missing, do you want to cancel this entry?" with a yes and no button. Using the onbeforeunload event, I have no way to change the dialog other than the text in the message. I also have no way of running code if they decide to leave the page. I understand that for security reasons it has to be this way. What are my options? What would you do to get around this? I'm really stuck against a wall with this, thanks for any help and thanks for even taking the time to read my post! Nick Quote Link to comment https://forums.phpfreaks.com/topic/277846-running-clean-up-code-when-user-leaves-page-without-onbeforeunload-dialog/ Share on other sites More sharing options...
Irate Posted May 9, 2013 Share Posted May 9, 2013 window.onbeforeunload = function(){ if(!required_fields){ // do something to check if all fields are filled var conf = confirm("You still have some required fields to fill out. Cancel?"); if(!confirm) return false; else { // add a function that sends something to your database } } }; Try something like that. Quote Link to comment https://forums.phpfreaks.com/topic/277846-running-clean-up-code-when-user-leaves-page-without-onbeforeunload-dialog/#findComment-1429342 Share on other sites More sharing options...
njdubois Posted May 9, 2013 Author Share Posted May 9, 2013 var required_fields='true'; window.onbeforeunload = function(){ if(!required_fields){ // do something to check if all fields are filled var conf = confirm("You still have some required fields to fill out. Cancel?"); if(!confirm) return false; else { // add a function that sends something to your database } } }; does nothing? Whether is set it to true or false, it goes right to the chosen page, no prompts, nothing? Based on what I have read, this isn't an option because you don't have control over whats going on inside the onbeforeunload event. Thanks for the reply! Nick Quote Link to comment https://forums.phpfreaks.com/topic/277846-running-clean-up-code-when-user-leaves-page-without-onbeforeunload-dialog/#findComment-1429354 Share on other sites More sharing options...
kicken Posted May 9, 2013 Share Posted May 9, 2013 I want to disable the ability for a user to browse away from this page until either these required fields are filled out, or they choose to cancel. You can't. The user can ALWAYS leave the page. There is no way at all for you to prevent that. There isn't even a reliable way to determine if the user has left the page. What you need to do is setup your system so that until all these fields have been filled in, that DB record is considered to be incomplete and is more or less ignored. If you want, you can have a cron job periodically run to delete all the older incomplete records to conserve space. Another alternative is to just not create the DB record at all until they have submitted all the necessary information. Store everything into session variables until everything is complete, then create the DB entry at the end. Quote Link to comment https://forums.phpfreaks.com/topic/277846-running-clean-up-code-when-user-leaves-page-without-onbeforeunload-dialog/#findComment-1429355 Share on other sites More sharing options...
njdubois Posted May 9, 2013 Author Share Posted May 9, 2013 You can't. The user can ALWAYS leave the page. There is no way at all for you to prevent that. There isn't even a reliable way to determine if the user has left the page. What you need to do is setup your system so that until all these fields have been filled in, that DB record is considered to be incomplete and is more or less ignored. If you want, you can have a cron job periodically run to delete all the older incomplete records to conserve space. Another alternative is to just not create the DB record at all until they have submitted all the necessary information. Store everything into session variables until everything is complete, then create the DB entry at the end. While reading your reply I had one of those 'DOH!' moments. While I was aware that there was no way to prevent a user from leaving a page, I wasn't looking at it from the other direction. Why not mark is as incomplete, and then output those records differently allowing the user to finish it later. Thank you sooo much!! Nick Quote Link to comment https://forums.phpfreaks.com/topic/277846-running-clean-up-code-when-user-leaves-page-without-onbeforeunload-dialog/#findComment-1429360 Share on other sites More sharing options...
.josh Posted May 9, 2013 Share Posted May 9, 2013 It really grinds my tits when a site tries to prevent me from leaving. One way trip to blacklist in my book. I agree with kicken, this is how I would do it: Another alternative is to just not create the DB record at all until they have submitted all the necessary information. Store everything into session variables until everything is complete, then create the DB entry at the end. Quote Link to comment https://forums.phpfreaks.com/topic/277846-running-clean-up-code-when-user-leaves-page-without-onbeforeunload-dialog/#findComment-1429399 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.