Query Master
Query Master

Reputation: 7097

Firefox Form submit issue with jQuery

I am using the following script to submit a form with jQuery, however Firefox has generated a crash error report, please let me know what am I doing wrong.

function teamValidation(){
    var cat_id = $('#cat_id').val();    

    if(cat_id == "0"){
        alert("Category is invalid...");    
        return false;
    }else{
        $('#addForm').submit(); 
        return false;
    }
}

<form action="<?php echo SITE_URL; ?>adminteams/saveDetail/" 
 name="addForm" id="addForm" onsubmit="return teamValidation();">
 <input type="hidden" name="cat_id" id="cat_id" value="1"/>
</form>

Firefox error report

enter image description here

It is working fine in other browsers.

Upvotes: 6

Views: 1646

Answers (4)

Code Prank
Code Prank

Reputation: 4250

The problem is your form is submitting multiple times.You can achieve your goal using any of the following ways:
HTML:

<form action="<?php echo SITE_URL; ?>adminteams/saveDetail/" name="addForm" id="addForm">
 <input type="hidden" name="cat_id" id="cat_id" value="1"/>
</form>

JAVASCRIPT:

$('#addForm').submit(function(){
    var cat_id = $('#cat_id').val();    

    if(cat_id == "0"){
        alert("Category is invalid...");    
        return false;
    }else{
        return true;
    }
});

Another Way:
HTML:

<form action="<?php echo SITE_URL; ?>adminteams/saveDetail/" name="addForm" id="addForm" onsubmit="return teamValidation();">
     <input type="hidden" name="cat_id" id="cat_id" value="1"/>
    </form>

JAVASCRIPT:

function teamValidation(){
    var cat_id = $('#cat_id').val();    

    if(cat_id == "0"){
        alert("Category is invalid...");    
        return false;
    }else{
       return true;
    }
}

Upvotes: 5

Tabrez Ahmed
Tabrez Ahmed

Reputation: 2950

You missed a '}' at the end. Still, It should not cause a crash. 99% a problem with your Firefox Installation. I just checked with mine here. Works correctly. There is no problem with your code. Try Uninstalling and reinstalling Firefox.

Another discovery. Your form is being submitted indefinitely. Maybe your other browsers are newer and your Firefox is outdated (not capable of handling indefinite recursions which is why it may be crashing).

Anyways, try replacing the following:

$('#addForm').submit();

with:

document.getElementById("addForm").submit();

Hope that helps..

Peace be upon you...

Upvotes: 0

Anthony Grist
Anthony Grist

Reputation: 38345

You have an infinite loop of submitting, then preventing the submission of, your form when the value of the cat_id text box is not 0. Remove the else block, since it serves no actual purpose.

To go into more detail:

function teamValidation(){
    var cat_id = $('#cat_id').val();    

    if(cat_id == "0"){
        alert("Category is invalid...");    
        return false;
    }else{
        $('#addForm').submit(); // this submits the form again - does NOT proceed with this submission of the form
        return false; // returning false will prevent the submission from being completed
    }
}

<form action="<?php echo SITE_URL; ?>adminteams/saveDetail/" 
 name="addForm" id="addForm" onsubmit="return teamValidation();">
 <input type="hidden" name="cat_id" id="cat_id" value="1"/>
</form>

When you submit the form, teamValidation() gets called. If the validation passes (the else block code is executed), you submit the form again which causes teamValidation() to be called yet again, then return false which will prevent this current submission from completing. Firefox is likely crashing because it finds itself in a loop where it's constantly submitting a form, which is never allowed to complete; the fact the browser is pretty awful in terms of memory usage probably doesn't help either.

Upvotes: 2

Andreas Wong
Andreas Wong

Reputation: 60516

You forgot to close your function definition

function teamValidation(){
    var cat_id = $('#cat_id').val();    

    if(cat_id == "0"){
        alert("Category is invalid...");    
        return false;
    }else{
        $('#addForm').submit(); 
        return false;
    }
} <--- this

Upvotes: 0

Related Questions