0
votes

I want to send post request with ajax to controller in laravel. The ajax request send two input arguments and I want controller to find the column in the database with the first argument and then to set the name attribute with the second input argument. But I have this error message in console 500 (Internal Server Error).

Ajax function:

var $emailInput = $('input[name=eemail]').val();
var $finduser = $('[name=userName]').val();

$.ajax({
       type:"POST",
       url:'/code/task1/public/editUserAdmin',

       data: {
          'emailInput' : $emailInput,
           'finduser' : $finduser,
           },
          success:function(data){

       //  $("#editEmail").attr("readonly", true);
       //  $("#editEmail").val(data[0].name);

           alert("OK");
           }

   });

Route:

Route::post('/editUserAdmin', 'usersController@editUserAdmin');

Controller function:

$findUserInput = $request->input('finduser');
        $user = User::where('name',$findUserInput)->first();


         if(!$user){
            return response()->json(['status'=>false,'Description' => 'User could not be found.']);
        }
            //$user->name = $request->input('nameInput');
            $user->email = $request->input('emailInput');

            $user->save();
}

And also i import csrf everywhere because last time when I was making AJAX call i have problem with this csrf and the following code has fixed my problem, but now is not working.

<meta name="csrf-token" content="{{ csrf_token() }}">

 $.ajaxSetup({
                headers:{
                    'X-CSRF-TOKEN' : $('meta[name="csft-token"]').attr('content')
                }
            });  

        $.ajaxSetup({
                   headers: {
                    'X-CSRF-TOKEN': $('[name="_token"]').val()
                }
            });

and this 
<h3 class="media-heading" name="userName">{{ $user->name }}</h3>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" class="form-control paddingzero" class=text-center" readonly value="Name Name">
<input type="text" class="form-control paddingzero" class=text-center" name="eemail" id="editEmail" readonly value="{{ $user->email }}">

Any idea?

1
Try to log your server side code - sensorario
The variables in data section, e.g. ` $emailInput` etc. are php variables and they won't work. - learner
Check server logs - Filip KoblaƄski
@learner on the previous ajax function the php variables worked - Devmasta

1 Answers

0
votes

Ajax does not have a "type" property. You will need to pass POST as method

$.ajax({
   method: "POST",
   url:'/code/task1/public/editUserAdmin',

   data: {
      'emailInput' : $emailInput,
      'finduser' : $finduser,
   },
      success:function(data){
       alert("OK");
   }

});

If you are in dev mode you should enable error loggin / output. You can just open dev tools F12 (in chrome for example) and have a look at the error output. Proably it would be method not allowed or whatever.

Another minor thing is that i would recommend to not prefix actual variables with $ if you do not reference the jquery object.

var $emailInput = $('input[name=eemail]').val();
var $finduser = $('[name=userName]').val();

Instead do

var $emailInput = $('input[name=eemail]'); // if you need it more than once
var email = $emailInput.val();

or if you only need it ones better name it

var emailInput = $('input[name=eemail]').val();