karenn1 Posted August 3, 2007 Share Posted August 3, 2007 Hi there! Is there a way in PHP to prevent a user from moving away from a page with an HTML form on when they made changes and didn't click submit? I would like some alert box to confirm if they want to leave or if they want to save the changes. Is that at all possible? I've tried Javascript but with no luck. Can anyone please help? Thanks, Karen Quote Link to comment https://forums.phpfreaks.com/topic/63181-html-forms-alert-to-save-changes/ Share on other sites More sharing options...
ramli Posted August 3, 2007 Share Posted August 3, 2007 Try: Header: <script type="text/javascript"> function ex() { var x=confirm("Do you want to save your work ?") if (x) window.location="save.php"; else window.location="exit.php"; } </script> Body: <body onunload="ex();"> This will display a queystion if you want to save your work or not. i hope it will help u. ps: its not tested. Quote Link to comment https://forums.phpfreaks.com/topic/63181-html-forms-alert-to-save-changes/#findComment-314908 Share on other sites More sharing options...
seikan Posted August 3, 2007 Share Posted August 3, 2007 What do you mean by "moving away" ? Is that include close the browser? Actually I think JavaScript can fix your problem here. Maybe like this: <?php $name = "Karen"; ?> <html> <head> <title></title> <script language="JavaScript"> <!-- function checkChanges(){ var obj = document.all.myform; var name = "<?php echo $name; ?>"; if(obj.name.value != "" && obj.name.value != name){ alert("You made some changes!"); } } //--> </script> </head> <body> <form name="myform" method="post"> <input type="text" name="name" value="<?php echo $name;?>" onBlur="checkChanges();"> <input type="submit" name="submit" value="Submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/63181-html-forms-alert-to-save-changes/#findComment-314911 Share on other sites More sharing options...
karenn1 Posted August 7, 2007 Author Share Posted August 7, 2007 I've tried the body unload function given above but this comes up every time I want to go to another page. I only want this to appear when changes have been made. Seiken, regarding your script. I have 17 fields on the page and not just $name. I want to check if any of them has been changed. Can I still use this script? How do I modify it? With "moving away", I don't mean closing the browser. I actually mean just clicking on any link to go to another page. Can you help me futher? Karen Quote Link to comment https://forums.phpfreaks.com/topic/63181-html-forms-alert-to-save-changes/#findComment-317344 Share on other sites More sharing options...
karenn1 Posted August 8, 2007 Author Share Posted August 8, 2007 Anybody....please help!!! Thanks, Karen Quote Link to comment https://forums.phpfreaks.com/topic/63181-html-forms-alert-to-save-changes/#findComment-318254 Share on other sites More sharing options...
pranav_kavi Posted August 8, 2007 Share Posted August 8, 2007 Probably u can try tis, <html> <head> <title></title> <script language="JavaScript"> <!-- function validateForm(this){ // u can do all the validation here } //--> </script> </head> <body> <form name="myform" method="post"> <input type="text" name="name" value="" > <a href="#" onclick="javascript: if ( validateForm(document.adminForm)){ document.adminForm.submit() }" >SAVE</a> </form> </body> </html> I hope tis s wat ur lukin 4... Quote Link to comment https://forums.phpfreaks.com/topic/63181-html-forms-alert-to-save-changes/#findComment-318277 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.