Jump to content

HTML forms - alert to save changes


karenn1

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

 

Archived

This topic is now archived and is closed to further replies.

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