Consume php ajax POST call for registration

Consume php ajax POST call for registration

How to get all error responses like password dismatch or password not more than 6 chars. or email is invalid.

My ajax:
  1. // process the form
  2. $('#employer').click(function(e) {
  3.     // get the form data
  4.     // there are many ways to get this data using jQuery (you can use the class or id also)
  5.     //Personal account
  6.     var image = $("#avatar").val();
  7.     var first_name = $('#first_name').val();
  8.     var last_name = $('#last_name').val();
  9.     var email = $('#email').val();
  10.     var password = $('#password').val();
  11.     var password_confirmation = $('#password_confirmation').val();
  12.     var gender = $("#option:checked").val();
  13.     //Business
  14.     var logo = $("#logo").val();
  15.     var business_name = $('#business_name').val();
  16.     var business_email = $('#business_email').val();
  17.     var business_phone = $('#business_phone').val();

  18.     // process the form
  19.     $.ajax({
  20.         type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
  21.         url         : 'routes/signasemployer.php', // the url where we want to POST
  22.         data: {
  23.          //Personal account
  24.          'image' : image,
  25.          'first_name' : first_name,
  26.          'last_name' : last_name,
  27.             'email' : email,
  28.             'password' : password,
  29.             'password_confirmation' : password_confirmation,
  30.             'gender' : gender,
  31.             //Business
  32.             'logo' : logo,
  33.             'business_name' : business_name,
  34.             'business_email' : business_email,
  35.             'business_phone' : business_phone
  36.         },
  37.         dataType:'JSON',
  38.         success: function(data) {
  39.          //data = jQuery.parseJSON(data);
  40.             console.log(data);

  41.             // here we will handle errors and validation messages
  42.             if (data.success == false) {
  43.                 console.log(data.success);
  44.                 // handle errors for password ---------------
  45.                 if (data.data.password) {
  46.                     $('#password').addClass('has-error'); // add the error class to show red input
  47.                     $('#password').append('<div class="help-block">' + data.data.password + '</div>'); // add the actual error message under our input
  48.                 }

  49.                 // handle errors for email ---------------
  50.                 if (data.data.email) {
  51.                     $('#email').addClass('has-error'); // add the error class to show red input
  52.                     $('#email').append('<div class="help-block">' + data.data.email + '</div>'); // add the actual error message under our input
  53.                 }

  54.             } else {
  55.                 console.log(data.data.password);
  56.                 // ALL GOOD! just show the success message!
  57.                 $('form').append('<div class="alert alert-success">' + data.message + '</div>');
  58.                 //window.location = 'home.php';

  59.             }
  60.         }
  61.     });
  62.     // stop the form from submitting the normal way and refreshing the page
  63.     e.preventDefault();
  64. });
My api response: if password too short and if doesn't match:

  1. {
  2. success: "false",
  3. message: "Validation Error.",
  4. data: {
  5. password: ["The password must be at least 6 characters.", "The password confirmation does not match."]
  6. }
  7. }