Click to See Complete Forum and Search --> : Redirect not working


Joice
03-17-2003, 07:54 AM
Something is going wrong here, but I cannot find the exact error :-(
I have a form with pull-down menus, which looks like this:

<FORM onSubmit="return check();" NAME="MyForm" METHOD="POST">
<SELECT NAME="a">
<OPTION VALUE="#">Maak uw keuze
<OPTION VALUE="#">-----------------------
<OPTION VALUE="20">20
<OPTION VALUE="50">50
<OPTION VALUE="75">75
<OPTION VALUE="#">-----------------------
</SELECT>

<SELECT NAME="b">
<OPTION VALUE="#">Maak uw keuze
<OPTION VALUE="#">-----------------------
<OPTION VALUE="rijen">rijen
<OPTION VALUE="tafels">tafels
<OPTION VALUE="#">-----------------------
</SELECT>

<INPUT TYPE="image" SRC="images/start.gif" WIDTH="42" HEIGHT="16" ALT="Start" BORDER="0">

</FORM>

Then in the HEAD of this same page, I have a JavaScript that looks like this:
<script language="JavaScript">
function check()
{
var a = document.MyForm.a.value;
var b = document.MyForm.b.value;

if(a=="20" && b=="rijen")
{
// document.location.href = 'c.html';
window.location='c.html';
}

}
</script>

As you can see, I would like to redirect to another page here, depending on which options are chosen. But the redirect does not work. If I use an alert in the if-statement, it works fine, but the redirect doesn't. What am I doing wrong??? Is it something in the form itself, or something in the JavaScript?
This is driving me crazy.

skriptor
03-17-2003, 08:01 AM
hi,
try this version:

function check()
{
var a = document.MyForm.a.value;
var b = document.MyForm.b.value;
if(a=="20" && b=="rijen")
{
window.location='c.html';
return false;
}
}

Good luck, skriptor

Joice
03-17-2003, 08:05 AM
Excellent, thanks! The "return false;" was what I needed to add! Can you explain what exactly this thing does (apart from making my code work, *lol*)?

Joyce

skriptor
03-17-2003, 08:34 AM
Hi,
the secret is here: onSubmit="return check();"
The usual way for submitting a form is executing the onSubmit-Statement and then sending the formular.
if check() returns false, the way of execution is stopped after onSubmit-statement.
Your first version gave order to change the location in check() and then, before this has happend, form was sent the usual way and location was changed again.
Hope you understand, what I mean, skriptor

Joice
03-17-2003, 08:38 AM
Ah, OK, I understand. Sounds logical. Is the onSubmit I used the 'conventional' way of checking a form and then redirect, depending on which options were chosen, or should I (for example) use an OnClick in the submit button tag?

Joyce

Nevermore
03-17-2003, 02:09 PM
OnSubmit is better.