/    Sign up×
Community /Pin to ProfileBookmark

Change Data warning before exit

I have a HTML form that updates the database when users click the ‘submit’ button. If users make changes to the data but forget to hit ‘submit’, the changes DO NOT get saved. Is there anyway to check if any data was added/deleted/changed before closing the form/window and giving the users a warning message that they must submit.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@scragarDec 20.2004 — <body onbeforeunload="return ConfirmLeave();">
<form name=frm1>
<textarea name=txt1>some content is in here.</textarea>
</form>
<script>
var txtCont = frm1.txt1.value;
function ConfirmLeave(){
if(frm1.txt1.value != txtCont){
if(confirm("click OK to leave and abort your changes.")){
return true;
}else{
return false;
};
};
};
</script>
Copy linkTweet thisAlerts:
@Warren86Dec 20.2004 — <HTML>

<Head>

<Script Language=JavaScript>


function submitIt(){

form1.submitBtn.disabled = true;

}

function checkSubmit(){

if (form1.submitBtn.disabled != true)
{
if (confirm('You did not submit the form.nTry again?'))
{
window.open("ThisDocument.html");
}
}
else {alert("Thank you for your submission")}
}

window.onbeforeunload = checkSubmit;


</Script>

</Head>

<Body>

<form name='form1'>

1st Field:<br>

<textarea rows=1, cols=25></textarea><br>

2nd Field:<br>

<textarea rows=1, cols=25></textarea><br>

3rd Field:<br>

<textarea rows=1, cols=25></textarea><br><br>

<input type=submit id=submitBtn onClick="submitIt()">

</form>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@Warren86Dec 20.2004 — My error. Try this.

<HTML>

<Head>

<Script Language=JavaScript>


function submitIt(){

form1.submitBtn.disabled = true;

}

function checkSubmit(){

if (form1.submitBtn.disabled != true)
{
if (confirm('You did not submit the form.nTry again?'))
{
window.open("ThisDocument.html");
}
}
else {alert("Thank you for your submission")}
}

window.onbeforeunload = checkSubmit;


</Script>

</Head>

<Body>

<form name='form1' method='post' action='test.html' onSubmit="submitIt()">

1st Field:<br>

<textarea rows=1, cols=25></textarea><br>

2nd Field:<br>

<textarea rows=1, cols=25></textarea><br>

3rd Field:<br>

<textarea rows=1, cols=25></textarea><br><br>

<input type=submit id=submitBtn >

</form>

</Body>

</HTML>
×

Success!

Help @rmagan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...