I wanted to apply ajax in the laravel 4.2.i wanted to redirect to a page without reloading it.i'm have tried using the window.loaction.href
and window.loaction.assign
but it's not working. here are my files
Route.php
//login+logout+auth
Route::get('login','authenticationController@create')->before('guest');
Route::get('logout','authenticationController@destroy');
Route::get('create_user_view',array('as'=>'create_user','uses'=>'authenticationController@create_user'));
Route::post('create_user_store',array('as'=>'store_user','uses'=>'authenticationController@store_user'));
Route::resource('authentication','authenticationController');
controller.php
public function store()
{
// return Response::json(array('msg'=>'true'),200);exit;
$input=Input::all();
$user=User::where('email',$input['email'])->pluck('id');
$user_priviledge=Priviledge::where('user_id', $user)->pluck('user_authority');
//var_dump($user_priviledge);die;
if($user_priviledge==1)
{
$attempt=Auth::attempt([
'email'=>$input['email'],
'password'=>$input['password']
]);
//var_dump($input['password']);die;
//var_dump($attempt);exit;
if($attempt)
{
return 'true';
}else{
return Redirect::intended('login');
}
}else{
return Redirect::intended('login')->with('message', 'User Disabled');;
}
}
balde
@extends('app')
@section('content')
<!--login form-->
<div class="login">
{{Form::open(array('url'=>route('authentication.store')))}}
<div>
<h2>Sign in to your account</h2>
<table>
<tr>
<th class="table">{{ Form::text('email', Input::old('E-mail'), array('id'=>'email','placeholder'=>'E-Mail','class'=>'placeholder')) }}</th>
<th>{{$errors->first('email')}}</th>
</tr>
<tr>
<th class="table">{{ Form::password('password', array('id'=>'password','placeholder'=>'Your Password','class'=>'placeholder')) }}</th>
<th class="table">{{$errors->first('password')}}</th>
</tr>
<tr>
<div>
<th class="table">{{Form::button('Login',array('class' => 'btn-login','onclick'=>'myfunc()'))}}</th><!--login button code-->
</div>
</tr>
<tr>
<th class="table"><!--reset button code-->
<div class="reset">
<a href="">I can't access my account</a>
</div>
</th>
</tr>
<tr>
<th class="table"><!--create button code-->
<div class="create-account">
<a href="{{route('create_user')}}">Create Account</a>
</div>
</th>
</tr>
</table>
</div>
</div>
<script>
function myfunc(){
var email =document.getElementById("email").value;
var password =document.getElementById("password").value;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//alert("hello");
// var data = xmlhttp.responseText;
// console.log(xmlhttp.respons?eText);
var text = Json.parse(xmlhttp.responseText);
console.log(text);
if(xmlhttp.responseText =='true'){
window.location.assign('http://localhost/votting-system/public/home');
//alert("hello");
}
}
}
xmlhttp.open("POST",'http://localhost/votting-system/public/authentication',true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("email=" +email+ "&password=" +password);
}
</script>
@stop
Now the problem is when i clicked the submit button it should have redirected to a page where the home page exist but when i clicked the login button the it does nothing then when i reload the page it shows the home page.that means its being logged in but the page is not getting redirected to the home page without reloading.