简体   繁体   中英

How to submit PHP form if jquery validation is successful

I am pretty familiar with jquery, but a novice at php, so excuse me if this is very simple.

I had a programmer develop a form that submits data into a database, but he did not program in any validation and the page bombs with minor errors. I am going to do the validation with jquery, but need to know how to not allow the php form to post until after the validation has been complete.

Below is the jquery base that I'll be using, obviously minus all of the details.

$(document).ready(function(){
var isValid = true;

$('.submit').click(function(event){
    memberValidation(); 
});

var memberValidation = function () {
    //Do validation

    if(isValid) {
        // Post form via .php   
    }
};

});

Here is the .php submit button:

<input type="image" src="images/signmeup.png" name="submit" id="submit" class="submit" value="Checkout" />

Could you please let me know what changes I would need to make to accomplish this?

Look up jQuery .submit() for submitting a form. You can find it in the API here . You give the form an ID and use the function call.

$('.submit').click(function(event){
    return memberValidation(); 
});

var memberValidation = function () {
    //Do validation
    if something is invalid
    return false;

    if(isValid) {
        // Post form via .php   
        return true;
    }
};

so if there is incorrect, it will return false and your form will not get submitted.

Ultimately the answer here is to do validation on the server-side in addition to the client-side. Client-side form checking cannot be relied upon as I could simply turn JavaScript off and completely bypass your validation.

If you really want to make sure your data is valid it must be done on the server-side, the client has too much control of their side to rely on for validation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM