2

i have following code

In controller

class AccountController extends Controller
{

    public function login(Request $request) {
      $data=$request->all();
      //print_r($request->all())
     //return view('main',['data'=>$data]);
     }
 }

In route

Route::get('account/login', function() {
  return View::make('manager.test');
});
Route::post('account/login', 'AccountController@login');

login view

@extends('layout.manager')
@section('content')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Example of Bootstrap 3 Vertical Form Layout</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
<style type="text/css">

</style>
<script type="text/javascript">
$(document).ready(function(){
  $('.send-btn').click(function(){            
    $.ajax({
      url: 'login',
      type: "post",
      data: {'email':$('input[name=email]').val(), '_token': $('input[name=_token]').val()},
      success: function(data){
        console.log(data);
      }
    });      
  }); 
});
</script>
</head>
<body>
<div class="bs-example">
 <div class="secure">Secure Login form</div>
{!! Form::open(array('url'=>'account/login','method'=>'POST', 'id'=>'myform')) !!}
<div class="control-group">
  <div class="controls">
     {!! Form::text('email','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Email')) !!}
  </div>
</div>
<div class="control-group">
  <div class="controls">
  {!! Form::password('password',array('class'=>'form-control span6', 'placeholder' => 'Please Enter your Password')) !!}
  </div>
</div>
{!! Form::button('Login', array('class'=>'send-btn')) !!}
{!! Form::close() !!}
</body>
</html>                                     

@stop

when i click submit then all data from ajax will print in my browser console.i have tried to pass ajax value to view in my controller but every time it return 500 internal error.Can anyone tell how to pass ajax value to another view and where i am doing wrong?. thank you

Update

following is not working

public function login(Request $request) {
          $data=$request->all();
       $send= User::where('email','=', $data->email)
        return view('main',['data'=>$send]);
         }



public function login(Request $request) {
             return Redirect::to('home');
             }

every think working fine .only problem when i return to view.it throw error in my browser console as 500 internal error otherwise if i print _r inside the controller then it will show data in my browser console.

update 2

In my controller

public function login(Request $request) {

    $data=$request->all();

    return $data;
}

Object { email: "[email protected]", _token: "gqqXvVfwbKRHQWczYNfFMmB9ZB943CLfLdG…" }
15
  • It's a bad practice to put your data in content while not having anything in your extends class Commented Sep 5, 2015 at 5:02
  • @AdityaGiri.i didnt got what you said Commented Sep 5, 2015 at 5:03
  • Put your script above the closing tag of body Commented Sep 5, 2015 at 5:06
  • @aldrin27.every think working fine .only problem when i return to view.it throw error otherwise if i print _r inside the controller then it will show data in my browser console Commented Sep 5, 2015 at 5:08
  • @tester I meant to say your view file is messed up. Try to take a look at how it should be organized at laravel.com/docs/5.1/blade Commented Sep 5, 2015 at 5:08

3 Answers 3

2

In your success callback of your jquery you can access each element of your data object as shown the demo

success: function(data){
        alert(data.email);
        alert(data._token);
      }
Sign up to request clarification or add additional context in comments.

2 Comments

why do you want to pass data from view to another view using ajax, you can have independent ajax call in each view
@Sourabh.sorry.i got now where i am doing wrong .lets see if any further error occured
1

I don't know what you are doing with your ajax. But I do it like

$("#myform").submit(function(e) {
    e.preventDefault();
    var form_url = $( this ).attr('action');
    var form_data= $( this ).serialize();

    $.ajax({
        url: form_url,
        type: 'POST',
        data: form_data,
        dataType: 'json',
        success: function( result ){
               $('#result-area').html( result );
        }
    });
});

And I get the appropriate response from the form.

And yeah as per your update, if you want that those values should be available in Javascript, you should use laracasts/utilities package by Jeffery Way. It's amazing to do that. You could simply do:

public function login(Request $request) {
   $data=$request->all();
   $send= User::where('email','=', $data->email);
   Javascript::put([ 'send' => $send ]);
   return view('main');
}

And then in your blade you can access by simply saying

console.log('send');

And it will do the magic.

1 Comment

@AdAditya Giri. i understood the laravel ajax topic in wrong manner.any way thank you
1

I think it's the route's problems.

Route:

   Route::get('account/login', function() {
       return View::make('manager.test');
    });
   Route::post('account/login', 'AccountController@login');

Change that into:

    Route::get('account/login, ['as' => 'getLogin, 'uses' => 'AccountController@getLogin']);

   Route::post('account/postLogin, ['as' => 'postLogin', 'uses' => 'AccountController@postLogin']);

Controller:

This:

    public function login(Request $request) {
      $data=$request->all();
      $send= User::where('email','=', $data->email)
      return view('main',['data'=>$send]);
    }

     public function login(Request $request) {
         return Redirect::to('home');
     }

To:

   public function postLogin(Request $request) {
      $data=$request->all();
      $send= User::where('email','=', $data->email)
      return $send;
   }

    public function getLogin(Request $request) {
       return Redirect::to('home');
    }

1 Comment

Laravel is confused if that url is post or get from your function so make the functions name to unique if that route is for getting the view or posting the queries.

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.