Connecting Tech Pros Worldwide Forums | Help | Site Map

canceling the submition of a form

greg brant
Guest
 
Posts: n/a
#1: Jul 20 '05
i have a form made up of 2 file inputs and a submit button..
the imputs are to upload images, namley jpeg's for a e-greatings thing im
working on.
this only works with jpegs so i have a script that will check the value of
the imput tag to make sure it ends with .jpg, .jpeg.

this works fine on the jubmit button using

onclick="myHandler(this)"

it works fane that it alerts the user that only jpegs are valid but then
when the alert
has been ok'd it continues to submit the form to my php script.

how can i get this to cancle the submit action?

cheers

Greg Brant



Lee
Guest
 
Posts: n/a
#2: Jul 20 '05

re: canceling the submition of a form


"greg said:[color=blue]
>
> i have a form made up of 2 file inputs and a submit button..
>the imputs are to upload images, namley jpeg's for a e-greatings thing im
>working on.
>this only works with jpegs so i have a script that will check the value of
>the imput tag to make sure it ends with .jpg, .jpeg.
>
>this works fine on the jubmit button using
>
>onclick="myHandler(this)"
>
>it works fane that it alerts the user that only jpegs are valid but then
>when the alert
>has been ok'd it continues to submit the form to my php script.
>
>how can i get this to cancle the submit action?[/color]

Don't user the onclick handler of the submit button.
That's not even supported in all browsers.
Use the onSubmit handler of the form, itself, and have
it return true, if the form is to be submitted, and
false if not:

<form action="whatever" onsubmit="return myHandler(this)">

greg brant
Guest
 
Posts: n/a
#3: Jul 20 '05

re: canceling the submition of a form


hi,

thanks for the reply.. i tried this once but there was somthing i could not
understand

i was returning true or false but whatever i had it was still going to the
action="" URL

im using ie6 on win xp

cheers
"Lee" <REM0VElbspamtrap@cox.net> wrote in message
news:bgh12d09ao@drn.newsguy.com...[color=blue]
> "greg said:[color=green]
> >
> > i have a form made up of 2 file inputs and a submit button..
> >the imputs are to upload images, namley jpeg's for a e-greatings thing im
> >working on.
> >this only works with jpegs so i have a script that will check the value[/color][/color]
of[color=blue][color=green]
> >the imput tag to make sure it ends with .jpg, .jpeg.
> >
> >this works fine on the jubmit button using
> >
> >onclick="myHandler(this)"
> >
> >it works fane that it alerts the user that only jpegs are valid but then
> >when the alert
> >has been ok'd it continues to submit the form to my php script.
> >
> >how can i get this to cancle the submit action?[/color]
>
> Don't user the onclick handler of the submit button.
> That's not even supported in all browsers.
> Use the onSubmit handler of the form, itself, and have
> it return true, if the form is to be submitted, and
> false if not:
>
> <form action="whatever" onsubmit="return myHandler(this)">
>[/color]


greg brant
Guest
 
Posts: n/a
#4: Jul 20 '05

re: canceling the submition of a form


so heres my script

function checkExt(file1, file2) {
alert("hello");
files = Array(file1, file2);

if (files[0] == "" && files[1] == ""){
alert("Please select a file or browse our gallery");
return false;
}else{
return true;
}
}

and on the form tag i have

<form action="designer2.php?upload=1" method="post"
enctype="multipart/form-data" name="uploadForm" target="_top"
id="uploadForm" onSubmit="return checkExt(this.userFile1.value,
this.userFile2.value)">

and i dont even get the
alert("hello");

it just submits

cheers

"Lee" <REM0VElbspamtrap@cox.net> wrote in message
news:bgh12d09ao@drn.newsguy.com...[color=blue]
> "greg said:[color=green]
> >
> > i have a form made up of 2 file inputs and a submit button..
> >the imputs are to upload images, namley jpeg's for a e-greatings thing im
> >working on.
> >this only works with jpegs so i have a script that will check the value[/color][/color]
of[color=blue][color=green]
> >the imput tag to make sure it ends with .jpg, .jpeg.
> >
> >this works fine on the jubmit button using
> >
> >onclick="myHandler(this)"
> >
> >it works fane that it alerts the user that only jpegs are valid but then
> >when the alert
> >has been ok'd it continues to submit the form to my php script.
> >
> >how can i get this to cancle the submit action?[/color]
>
> Don't user the onclick handler of the submit button.
> That's not even supported in all browsers.
> Use the onSubmit handler of the form, itself, and have
> it return true, if the form is to be submitted, and
> false if not:
>
> <form action="whatever" onsubmit="return myHandler(this)">
>[/color]


Grant Wagner
Guest
 
Posts: n/a
#5: Jul 20 '05

re: canceling the submition of a form


Then you are most likely generating a JavaScript error somewhere along the way.
Change the onsubmit button to be:

onsubmit="return (false && checkExt(this.userFile1.value,
this.userFile2.value));"

then check the lower-left corner of IE for a yellow !, or type "javascript:" (no
quotes) into the Address bar of Mozilla/Netscape 7. If there are any errors,
they will be reported.

greg brant wrote:
[color=blue]
> so heres my script
>
> function checkExt(file1, file2) {
> alert("hello");
> files = Array(file1, file2);[/color]

Why do you put both files into an array and then access the individual
elements?
[color=blue]
> if (files[0] == "" && files[1] == ""){[/color]

if (file1 == "" && file2 == "") {
[color=blue]
> alert("Please select a file or browse our gallery");
> return false;
> }else{
> return true;
> }
> }
>
> and on the form tag i have
>
> <form action="designer2.php?upload=1" method="post"
> enctype="multipart/form-data" name="uploadForm" target="_top"
> id="uploadForm" onSubmit="return checkExt(this.userFile1.value,
> this.userFile2.value)">
>
> and i dont even get the
> alert("hello");
>
> it just submits
>
> cheers
>
> "Lee" <REM0VElbspamtrap@cox.net> wrote in message
> news:bgh12d09ao@drn.newsguy.com...[color=green]
> > "greg said:[color=darkred]
> > >
> > > i have a form made up of 2 file inputs and a submit button..
> > >the imputs are to upload images, namley jpeg's for a e-greatings thing im
> > >working on.
> > >this only works with jpegs so i have a script that will check the value[/color][/color]
> of[color=green][color=darkred]
> > >the imput tag to make sure it ends with .jpg, .jpeg.
> > >
> > >this works fine on the jubmit button using
> > >
> > >onclick="myHandler(this)"
> > >
> > >it works fane that it alerts the user that only jpegs are valid but then
> > >when the alert
> > >has been ok'd it continues to submit the form to my php script.
> > >
> > >how can i get this to cancle the submit action?[/color]
> >
> > Don't user the onclick handler of the submit button.
> > That's not even supported in all browsers.
> > Use the onSubmit handler of the form, itself, and have
> > it return true, if the form is to be submitted, and
> > false if not:
> >
> > <form action="whatever" onsubmit="return myHandler(this)">
> >[/color][/color]

--
| Grant Wagner <gwagner@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html


Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#6: Jul 20 '05

re: canceling the submition of a form


Grant Wagner <gwagner@agricoreunited.com> writes:
[color=blue]
> Change the onsubmit button to be:
>
> onsubmit="return (false && checkExt(this.userFile1.value,
> this.userFile2.value));"[/color]

The would be equivalent to
onsubmit='return false;"
In Javascript, the "&&" operator is "short-circuit". If the first operand
is false, then the second isn't evaluated, since we already know that the
result must be false.

Try
onsubmit="return (false && alert('this is not happening'));"
and see (not) that it is indeed no happening.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Grant Wagner
Guest
 
Posts: n/a
#7: Jul 20 '05

re: canceling the submition of a form


Lasse Reichstein Nielsen wrote:
[color=blue]
> Grant Wagner <gwagner@agricoreunited.com> writes:
>[color=green]
> > Change the onsubmit button to be:
> >
> > onsubmit="return (false && checkExt(this.userFile1.value,
> > this.userFile2.value));"[/color]
>
> The would be equivalent to
> onsubmit='return false;"
> In Javascript, the "&&" operator is "short-circuit". If the first operand
> is false, then the second isn't evaluated, since we already know that the
> result must be false.
>
> Try
> onsubmit="return (false && alert('this is not happening'));"
> and see (not) that it is indeed no happening.
>
> /L[/color]

You're right of course, it should have been:

onsubmit="return (checkExt(this.userFile1.value, this.userFile2.value) &&
false);"

Since he isn't sure the function is even being called, it necessary to halt
the onsubmit event unconditionally so he can see the error(s), if any.

onsubmit="checkExt(...); return false;" would work just as well.

--
| Grant Wagner <gwagner@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html


greg brant
Guest
 
Posts: n/a
#8: Jul 20 '05

re: canceling the submition of a form


thanks guys
"Grant Wagner" <gwagner@agricoreunited.com> wrote in message
news:3F301E74.CD3D4E41@agricoreunited.com...[color=blue]
> Lasse Reichstein Nielsen wrote:
>[color=green]
> > Grant Wagner <gwagner@agricoreunited.com> writes:
> >[color=darkred]
> > > Change the onsubmit button to be:
> > >
> > > onsubmit="return (false && checkExt(this.userFile1.value,
> > > this.userFile2.value));"[/color]
> >
> > The would be equivalent to
> > onsubmit='return false;"
> > In Javascript, the "&&" operator is "short-circuit". If the first[/color][/color]
operand[color=blue][color=green]
> > is false, then the second isn't evaluated, since we already know that[/color][/color]
the[color=blue][color=green]
> > result must be false.
> >
> > Try
> > onsubmit="return (false && alert('this is not happening'));"
> > and see (not) that it is indeed no happening.
> >
> > /L[/color]
>
> You're right of course, it should have been:
>
> onsubmit="return (checkExt(this.userFile1.value, this.userFile2.value) &&
> false);"
>
> Since he isn't sure the function is even being called, it necessary to[/color]
halt[color=blue]
> the onsubmit event unconditionally so he can see the error(s), if any.
>
> onsubmit="checkExt(...); return false;" would work just as well.
>
> --
> | Grant Wagner <gwagner@agricoreunited.com>
>
> * Client-side Javascript and Netscape 4 DOM Reference available at:
> *
>[/color]
http://devedge.netscape.com/library/...ce/frames.html[color=blue]
>
> * Internet Explorer DOM Reference available at:
> *
>[/color]
http://msdn.microsoft.com/workshop/a...ence_entry.asp[color=blue]
>
> * Netscape 6/7 DOM Reference available at:
> * http://www.mozilla.org/docs/dom/domref/
> * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
> * http://www.mozilla.org/docs/web-deve...upgrade_2.html
>
>[/color]


Closed Thread