Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/63181-html-forms-alert-to-save-changes/
Share on other sites

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.

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>

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

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...

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.