2

I Insert Ledger Record using Ajax and Jquery in Laravel. Success Message has been Displayed Correctly but Error Custom Message Cannot Display in blade View. Whats My Mistake Please Mention.

Jquery :

$("#add").click(function(event) {
  event.preventDefault();

  $.ajax({
    type: 'post',
    url: $("#add").attr('data-url'),
    data: {
      '_token': $("input[name=_token]").val(),
      'form_data': $('#Form').serialize(),
    },
    success: function(data) {
      $('#ledger_name').val('');
      $('#openning_balance').val('');
      $('#ob_type').val('');
      $('#under').val('');
      $('#ledger_address').val('');
      $("#newLedger .close").click();

      $(".result").html(data.success).css({
        'color': 'green',
        'text-align': 'center'
      }).delay(5000).fadeOut();
    },
    error: function(data) {
      $('#response').show().html(data.error).css({
        'color': 'red',
        'text-align': 'center'
      }).delay(5000).fadeOut();
    }
  });
});

Controller :

$values = array();
    parse_str($_POST['form_data'], $values);

    $validation = $this->validator($values,true );
    if($validation->fails()){
    $errors = $validation->errors();
    return response()->json(['error' => 'Please Fill all Mandatory Fields',],500);
    }

    $insertledgers=Ledger::create(['ledger_name'=>$values['ledger_name'],'openning_balance'=>$values['openning_balance'],'ob_type'=>$values['ob_type'],'under'=>$values['under'],'ledger_address'=>$values['ledger_address'],'company_id'=>$companyids,'user_id'=>$usersid,'created_by'=>$usersid]);

    $ledgerinsertids=$insertledgers->id;

    if($values['ob_type'] == 'Cr'){

    $creditamts=$values['openning_balance'];
    $debitamts= 0;

    } else {

    $creditamts=0;
    $debitamts= $values['openning_balance'];

    }

    $insertledgeropenningbalance=Openningbalance::create(['ledgerid'=>$ledgerinsertids,'opening_credit'=>$creditamts,'opening_debit'=>$debitamts,'company_id' => $companyids,'user_id' => $usersid,'created_by' => $usersid,]);

    return response()->json(['success' => 'Ledger Details Added Successfully',],200);   
3
  • post your blade view Commented Jun 23, 2017 at 5:13
  • <!-- Modal --> <div id="response"></div> Commented Jun 23, 2017 at 5:15
  • Improved code formatting Commented Jun 23, 2017 at 7:06

2 Answers 2

1

Try this:

<?php
use Validator;

class SomeController extends Controller {

  public function SomeFunction(Request $request) {
    $values = array();
    parse_str($_POST['form_data'], $values);

    $validation = Validator::make($values, true);

    if($validation->fails()){
      $errors = $validation->errors();
      return response()->json(['error' => 'Please Fill all Mandatory Fields'], 500);
    }

    $insertledgers=Ledger::create(['ledger_name'=>$values['ledger_name'],'openning_balance'=>$values['openning_balance'],'ob_type'=>$values['ob_type'],'under'=>$values['under'],'ledger_address'=>$values['ledger_address'],'company_id'=>$companyids,'user_id'=>$usersid,'created_by'=>$usersid]);

    $ledgerinsertids=$insertledgers->id;

    if($values['ob_type'] == 'Cr'){

      $creditamts=$values['openning_balance'];
      $debitamts= 0;

    } else {

      $creditamts=0;
      $debitamts= $values['openning_balance'];

    }

    $insertledgeropenningbalance=Openningbalance::create(['ledgerid'=>$ledgerinsertids,'opening_credit'=>$creditamts,'opening_debit'=>$debitamts,'company_id'
    => $companyids,'user_id' => $usersid,'created_by' => $usersid,]);

    return response()->json(['success' => 'Ledger Details Added Successfully',],200);

And in view:

error: function(data)
{
$('#response').html(data.error).css({'color': 'red', 'text-align': 'center'})
$('#response').show().delay(5000).fadeOut();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You just need to modify your error callback function as below:

error : function (data) {
    $('#response').show().html(data.responseJSON.error).css({
        'color': 'red',
        'text-align': 'center'
    }).delay(5000).fadeOut();
}

Thanks @voodoo417

2 Comments

Sorry, but your "solution" is marasm.... Do you see that response returns 500 on error and 200 on success? And of course it will not work.
@voodoo417: I haven't noticed the second parameter. Thank You!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.