[thelist] Quick javascript question

jeff jeff at members.evolt.org
Thu Apr 12 20:29:05 CDT 2001


janet,

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: From: Green, Janet
:
: - I *also* have a bit of JS that is supposed
:   to prompt visitors to fill in the required
:   fields of the form. The <form> tag now looks
:   like this:
:
:   <form
:    name="aedcform"
:    method="post"
:    action="mailto:jgreen at desmoinesmetro.com"
:    enctype="text/plain"
:    onSubmit="return checkrequired(this)"; "FormRedirect()">
:
:   This is supposed to check/prompt for incomplete
:   fields, THEN submit the form, THEN redirect the
:   visitor. It's doing everything except redirecting
:   me.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

the first problem is that you've got two event handlers in the onSubmit
function with the first being prefaced with a return statement.  this is a
problem because as soon as the script engine encounters the return statement
it executes that and then stops (no matter if it's processing a true or
false value) further script execution beyond that point.

here's an example of what i'm talking about:

function checkReturn()
{
  alert('hello, world!');
  return false;
  alert('hello, world!');
}

if you call this function from an event handler in your document, you'll
only get the first alert().  the second one won't fire because the "return
false" statement halts further execution of the function and returns a value
of false.  the same is true in event handlers.  take the following example:

onClick="alert('hello, world!'); return true; alert('hello, world!')"

place that onclick event handler in an <a> tag and click it.  you'll get
exactly one alert(), not two as you see in the event handler.

so, relating this back to your question and a solution, to get the
formRedirect() function to work you'll need to move it to the end of your
checkrequired() function just before the line that returns true.  assuming
your checkrequired() function looks kind of like this:

function checkrequired(form)
{
  if(!form.somefield.value)
  {
    alert('Some Field is required.');
    form.somefield.focus();
    return false;
  }
  formRedirect();
  return true;
}

your onSubmit event handler will now look like this:

onSubmit="return checkrequired(this)"

you'll notice though that calling the formRedirect() function from the
checkrequired() function and simply moving the body of the formRedirect()
function within the checkrequired function would yield the same results and
make your code cleaner.

i hope this makes things alittle clearer.

this is all really a moot point though as you're likely to run into much
larger issues trying to use a form that sends its contents via email.  if at
all possible, i would suggest that you use one of the many free cgi scripts
available (http://www.cgi-resources.com/) or even one of the many free email
form processing services -- http://www.response-o-matic.com/ being one of
the better suggestions available.

good luck,

.jeff

name://jeff.howden
game://web.development
http://www.evolt.org/
mailto:jeff at members.evolt.org





More information about the thelist mailing list