CodeIgniter Forums

Full Version: Ajax validation issue with array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to validate my codeigniter based from using jquery ajax.  in my form i have dynamic fields which generates fields name like fault[0],fault[1],fault[2]......  this jquery script i am using works only  the ID attribute & NAME attribute of a field are same. since array fields have [] marks i am unable to validate them. 

i need to know a way doing this without id & same name rule. 

i think this part manages the validate the fields only with same name & id.

$.each(response.messages, function(key, value) {
           var element = $('#' + key);

PHP Code:
$('#form-user').submit(function(e) {

   var me = $(this);

   // perform ajax
     success: function(response) {
       if (response.success == true) {
         // if success we would show message
         // and also remove the error class
         $('#the-message').append('<div class="alert alert-success">' +
           '<span class="glyphicon glyphicon-ok"></span>' +
           ' Data has been saved' +

         // reset the form

         // close the message after seconds
         $('.alert-success').delay(500).show(10, function() {
           $(this).delay(3000).hide(10, function() {
       else {
         $.each(response.messages, function(keyvalue) {
           var element = $('#' key);
           .addClass(value.length 'has-error' 'has-success')
Jquery serialize produces a string, which you are posting in your Ajax call. Your controller needs to decode the string it receives (usually with explode) into the parts you want. You can then validate each part, or whatever you do with this data when you get it is up to you. Then you can format your Json response and echo it back to the originating Ajax call.

Your Ajax call will detect if the response is the expected data type or not, and do whatever you have in the success or error conditions depending if it is of the correct format for that expected data or not, in your case it is expecting a Json data set to be received. Again, what you do with this data when you get it back to your JS is up to you.

The key here, is that your controller is being posted a string, not an array. So decode the string in whatever way suits the data you are expecting.

Hope that helps,