Welcome Guest, Not a member yet? Register   Sign In
Ajax validation issue with array
#1

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.



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



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

 
   var me = $(this);

 
   // perform ajax
 
   $.ajax({
 
     urlme.attr('action'),
 
     type'post',
 
     datame.serialize(),
 
     dataType'json',
 
     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' +
 
           '</div>');
 
         $('.form-group').removeClass('has-error')
 
                 .removeClass('has-success');
 
         $('.text-danger').remove();

 
         // reset the form
 
         me[0].reset();

 
         // close the message after seconds
 
         $('.alert-success').delay(500).show(10, function() {
 
           $(this).delay(3000).hide(10, function() {
 
             $(this).remove();
 
           });
 
         })
 
       }
 
       else {
 
         $.each(response.messages, function(keyvalue) {
 
           var element = $('#' key);
 
           
            element
.closest('div.form-group')
 
           .removeClass('has-error')
 
           .addClass(value.length 'has-error' 'has-success')
 
           .find('.text-danger')
 
           .remove();
 
           
            element
.after(value);
 
         });
 
       }
 
     }
 
   });
 
 }); 
Reply
#2

(This post was last modified: 08-14-2016, 02:32 PM by PaulD.)

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,

Paul.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB