Search code examples
phpjqueryformscountdown

Unable to submit form using JQuery


So the scenario is on my page I have a form along with a countdown timer. If the countdown timer reaches 0:00 I have a statement which activates and is supposed to submit the form as it stands.

here is that statement:

if (Todays_Date >= Target_Date) {
        clearTimeout(timeoutID);
        $('#testForm').submit();
        alert("Timeup, Submitting exam....");
        return;
    }

what actually happens is, it fires, the timer stops (cleared timeout) and alert pops up. However the form does not submit and take the user to the action page.

I have copied all of the code into this jsFiddle to layout the basic principle.

the time of which countdown is targetted to is passed in the HTML at the bottom. you'll see the countdown() call.

I really am in a pickle and would appreciate someones help.

note: I know this is not a secure method etc.

many thanks


Solution

  • You have form element called submit:

    <input name="submit" type="submit" value="Complete Test">
    

    Change its name so something like:

    <input name="btnSubmit" type="submit" value="Complete Test" />
    

    And your code will work.

    When the form contains element called submit, referring to formElement.submit will refer the element instead of the actual submit method - this is very common problem.