470,636 Members | 1,601 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Prompt to submit

I have a form that the user can edit data in. However, there are a couple
of links on the page. The user can edit the data and click on a link, not
submitting the form.

Is there some code out there that will check to see if they changed the
form, and prompt them to submit it if they try to exit the page any other
way?

Perfect example:
www.windowsupdate.com
Using IE, scan for updates, then click "personalize windows update". Change
something on that list, then try to navigate away.

Matthew

Jul 20 '05 #1
6 4088
Matthew wrote:

I have a form that the user can edit data in. However, there are a couple
of links on the page. The user can edit the data and click on a link, not
submitting the form.

Is there some code out there that will check to see if they changed the
form, and prompt them to submit it if they try to exit the page any other
way?

Perfect example:
www.windowsupdate.com
Using IE, scan for updates, then click "personalize windows update". Change
something on that list, then try to navigate away.

Matthew


Matthew,

You can use the onUnload event in combination with some form checks
which check the current value of the form fields against the default
values.
Not secure, not foolproof (you can easily turn off javascript), but it
will allow you to do what you ask.

Gr, J.
Jul 20 '05 #2
Thanks.

This is just for my customers convenience; no "security" necessary.

Matthew

Jul 20 '05 #3
Below is the code I wrote.

It seems to work properly. As I am a bit new to JavaScript, can somebody
take a look at the logic for inefficiencies?

I don't know a better way to do "function nocheck".

Also, is there a quick, easy way to submit the form "cart", then continue to
where they wanted to go originally?

Thanks for the help.

Matthew

Code:

<HEAD>
<script type="text/JavaScript">
<!--

function nocheck() {
changeform = nothing
}

function nothing(){
}

function changeform() {
if (checkform())
{
return true
} else {
var confirmed = confirm("You have changed the quantity of an item.\nClick
OK to save your changes.");
if (confirmed == true) {
document.cart.submit();
return false
} else {
return true
}
return false
}
}

function checkform() {
if (document.cart.qty01.value != 8)
{
return false
}
if (document.cart.qty02.value != 7)
{
return false
}
if (document.cart.qty03.value != 6)
{
return false
}
if (document.cart.qty04.value != 4)
{
return false
}
return true
}
//-->
</script>
</HEAD>
<BODY onUnload="changeform();">
<FORM method="post" name="cart" action="test.htm" onSubmit="nocheck();">

some code

</FORM>

some links

</BODY>

Jul 20 '05 #4
JRS: In article <x9*****************@fe01.usenetserver.com>, seen in
news:comp.lang.javascript, Matthew <tu*************@alltel.net> posted
at Tue, 3 Feb 2004 19:37:46 :-

function changeform() {
if (checkform())
{
return true
} else {
var confirmed = confirm("You have changed the quantity of an item.\nClick
OK to save your changes.");
if (confirmed == true) {
document.cart.submit();
return false
} else {
return true
}
return false
}
}


function changeform() {
if (checkform()) return true // else not needed after return
var confirmed = confirm("You have changed the quantity of an item." +
"\nClick OK to save your changes.");
if (confirmed) document.cart.submit() // ==true is normally pointless
return !confirmed }

I split the string in order to for News convention of margin 72; code
should NEVER be wrapped by a system that does not understand it.

The return statement is an EXIT statement; not like Result in Delphi,
which is a variable that can be accessed repeatedly.

While else is perfectly legal, nested else becomes hard to read. Some
teach that functions should only be exited at their ends; my view is
that their exits should be obvious - so the early return could be
commented // EARLY RETURN.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #5
Thanks for looking at the code.

It looks much cleaner.

Matthew

Jul 20 '05 #6
JRS: In article <x9*****************@fe01.usenetserver.com>, seen in
news:comp.lang.javascript, Matthew <tu*************@alltel.net> posted
at Tue, 3 Feb 2004 19:37:46 :-
function checkform() {
if (document.cart.qty01.value != 8)
{
return false
}
if (document.cart.qty02.value != 7)
{
return false
}
if (document.cart.qty03.value != 6)
{
return false
}
if (document.cart.qty04.value != 4)
{
return false
}
return true
}

The following should be equivalent; but it seems a strange need :

function checkform() { return (
document.cart.qty01.value == 8 &&
document.cart.qty02.value == 7 &&
document.cart.qty03.value == 6 &&
document.cart.qty04.value == 4 ) }

function checkform() { with (document.cart) return (
qty01.value == 8 && qty02.value == 7 &&
qty03.value == 6 && qty04.value == 4 ) }


To turn off checking by function X, use global G

function X() {
if (G) return true // or false
<series-of-tests> }

function X() {
return G || <series-of-tests> }

function X() {
return G && <series-of-tests> }

The first and second return true if G is true; the third returns false
if G is false; in each case, without further tests. I think.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Petesman | last post: by
3 posts views Thread by vijay21 | last post: by
1 post views Thread by Rezas | last post: by
3 posts views Thread by J.P. Cummins | last post: by
1 post views Thread by andwing | last post: by
4 posts views Thread by Geoff Cox | last post: by
???
reply views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.