Karthik
Karthik

Reputation: 5759

Jquery and Ajax Error Message cannot Display in Laravel Blade

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);   

Upvotes: 2

Views: 1555

Answers (2)

linktoahref
linktoahref

Reputation: 7972

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

Upvotes: 0

Dato Sarishvili
Dato Sarishvili

Reputation: 185

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();
}

Upvotes: 1

Related Questions