views:

254

answers:

3

I have a plain(no fancy frameworks) javascript, which, on the submission of a form element, asks the user a question. If the answer returns true, then the script sets the form's action to a certain URL (which has some server-side logic inside it) and calls the sumbit method of that form. Or at least, in theory, thats what its meant to do! But it doesnt.. it just doesn't do anything. It seems to submit the form, but if it did, the server-side logic in the other file (which has been set as the action property's value) would ensure the user is taken somewhere else.

Here is my form:

<form name='myForm' id='myForm' method='post' onSubmit='annoyTheUser(this);'>

Here is my javascript function:

function annoyTheUser(theForm)
{
    if(confirm("blah?"))
    {
     theForm.action = 'savequestion.asp';
     theForm.submit();
    }
}
+2  A: 

Your JS should look like:

function annoyTheUser(theForm)
{
    if(confirm("blah?"))
    {
        theForm.action = 'savequestion.asp';
        return true;
    }
    else
        return false;
}
K Prime
Also needs `onsubmit="return(annoyTheUser(this));"` to do that, which is probably what the OP wants.
Anonymous
Mmmh it is obvious and I didn't see it... but it really worked in Safari (although not in Firefox (which I only tried after this post ;))).
Felix Kling
A: 

Can you add the part that makes the form submit also. The script looks fine. Only that one seems suspicious.

zapping
A: 

Try this

<form name='myForm' id='myForm' method='post' onSubmit='return annoyTheUser(this);'>

And the script should be

function annoyTheUser(theForm)
{
if(confirm("blah?"))
{
theForm.action = 'savequestion.asp';
return(true);
}
else
{
return(false);
}
}
Anuraj