- Don't use the onsubmit attribute, it's mixing JavaScript into html markup, and goes against best practices.
- Why don't you make the validation part of the register function (server side). That way you aren't making twice as many round trips as you need. Following best practices you should be making as few http requests as possible. You can organize your php scripts into folders depending on the functionality. Much easier to tell what form is being submitted. This way it will also be much easier to create a unique validation script for each form submission. You may also be able to use the REQUEST_URI Server variable to tell which form you are submitting.
See here for instructions on accessing the REQUEST URI of the request within a php script.
If there is a common validation that you are doing for every form submitted, you can move that into a separate php file and import it into all files that will use the function. That function will then be available inside the script you imported it into.
See here for instructions on including files in php.
Then you can add custom validation to each individual script. You will more than likely need custom validation for some of your forms, someday.
- util/validation_util.php
- ajax/user/register.php
- ajax/user/login.php
- ajax/user/logout.php
- ajax/product/search.php
- ajax/product/detail.php
- ajax/product/addToCart.php
etc...
$('#formId').on('submit', function (event) {event.preventDefault();$.ajax({ url : "ajax/user/register.php", type : "POST", data : $(this).serialize(), success : function(data) { $('#'+ obj.id +' :input.form_errors').removeClass('form_errors') data = $.parseJSON(data); if(data['error_count'] >= 1) { $.each(data, function(i, item) { $('#'+ i).addClass('form_errors'); }); } }});});