1

My jquery validation script is below. When i click the submit button it does not submit the form data to mysql. The submit button is called submit. If I remove the jquery validation script it submits to mysql so it is an issue with the validation script.

Jquery Validation script:

$(function() {
    function validate(id) {
        var enabled = ($("input[name='attendance" + id + "']:checked").val() == 'Yes');
        if (enabled) {
            //Please select option is selected              
            if ($("#food" + id)[0].selectedIndex == 0 || $("#drink" + id)[0].selectedIndex == 0) {
                alert('Please select color');
                return false;
            }
        }
        return true;
    };

    $("input[name^='attendance']").click(function(e) {

        var id = this.name.replace('attendance', '');
        $("#food" + id + ", #drink" + id).prop("disabled", this.value == 'No');
        validate(id);
    });

    $("input:submit").click(function(event) {

        event.preventDefault();
        var retVal = false;
        $.each([1, 2], function(i, val) {
            retVal = (validate(val) || retVal);
        });
        if (retVal) {
            $('list').submit();
        }
    });
});

submit button:

<input type="submit" name="submit" id="submit" value="Submit" />

form data:

<form name="colour" action="" method="post" id="list">
4
  • ...why are you using 6 UPDATE queries to update one row? Commented Feb 7, 2012 at 18:10
  • 1
    That doesn't answer the question as to why you would do that instead of just one query. A solution is not good just because "it works". Commented Feb 7, 2012 at 18:12
  • @Jacob consider this a downvote to your comment. You could reduce the amount of code by a factor of 6. Commented Feb 7, 2012 at 18:14
  • its not that i am worried about. When jquery validation is removed it submits all my form info to mysql so It works fine so why change it! it is the jquery that is the issue Commented Feb 7, 2012 at 18:15

1 Answer 1

1

The selector in the submit() is incorrect. You're looking for the form by its id, list. Your selector is looking for tags called <list>.

if(retVal){$('list').submit();}
// Should be
if(retVal){$('#list').submit();}
Update: if it won't submit in IE:
if (retVal) {
  document.getElementById('list').submit();
}

Update 2:

Instead of binding this to the submit button's .click(), bind it to the form's .submit() and return true or false:

// Bind to the form's .submit()
$("#list").submit(function(event) {
    event.preventDefault();
    var retVal = false;
    $.each([1, 2], function(i, val) {
        retVal = (validate(val) || retVal);
    });
    // return the true/false to the submit action
    // false will prevent submission.
    return retVal;
});
Sign up to request clarification or add additional context in comments.

8 Comments

I have changed it to what you said however it still does not submit. On internet explorer there is an error (button right). Line 3 'Object doesnt support this property or method'. When I remove the '#' I do not get this error. Any ideas?
@Jacob According to docs (api.jquery.com/submit), the submit attribute doesn't bubble up in IE. You can do it with a simple javascript call. See above.
thanks but it still not submitting. getting same error but differnet line 173
I have made new code pastebin.com/pGb8MDNZ. This code submits but if 'please select' is selected when submit is clicked the validation error occurs and it redirects to the page i requested in my php. So i need to prvent it from going to the page in php if 'please select' is selected when submit is clicked
Haven't given up, but it may require a lengthy answer I don't have time to undertake right now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.