Jump to content


Photo

running clean up code when user leaves page without onbeforeunload dialog.


  • Please log in to reply
5 replies to this topic

#1 njdubois

njdubois

    Advanced Member

  • Members
  • PipPipPip
  • 158 posts

Posted 09 May 2013 - 01:24 PM

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



#2 Irate

Irate

    Advanced Member

  • Members
  • PipPipPip
  • 358 posts
  • LocationHamburg, Germany
  • Age:17

Posted 09 May 2013 - 01:41 PM

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.


Quod placet mihi non placeat tibi. - What I think to be good must not always equal your perception of it.

I am not perfect. I try a lot with the code I provide and I don't guarantee for it to work as I have mostly no option to test it on my mobile phone. I do apologize for any inconvenience I caused, but if I do happen to have helped, liking my posts or marking them as to have solved or answered your question would be nice.

#3 njdubois

njdubois

    Advanced Member

  • Members
  • PipPipPip
  • 158 posts

Posted 09 May 2013 - 02:08 PM

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



#4 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,651 posts
  • LocationBonita, FL

Posted 09 May 2013 - 02:12 PM

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.
Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#5 njdubois

njdubois

    Advanced Member

  • Members
  • PipPipPip
  • 158 posts

Posted 09 May 2013 - 02:26 PM

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



#6 .josh

.josh

    .josh

  • Staff Alumni
  • 14,815 posts

Posted 09 May 2013 - 06:59 PM

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.



Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com