I am trying to do a check to see if the user has any recorded violations or not.
I have got the flash massage to appear when the user id is entered, but I am having troubles when pressing the button, the entered ID is disappearing, I am trying to reload the page with the previously entered values.
Here is my view:
<strong>Customer ID:</strong>
<input class="form-control" type="text" name="custidno" id='cust' autocomplete="off" onkeypress="myFunction()" placeholder="Customer ID" value="{{ old('custidno') }}">
<a class="btn btn-info" href="{{ url('violationcheck') }}">Show</a>
#if(Session::has('flash_message'))
<div class="alert alert-success"><em> {!! session('flash_message') !!}</em></div>
#endif
and here is my controller:
public function violationcheck(Request $request)
{
$checked = violation::find($request->custidno);
if (!empty($checked))
Session::flash('flash_message','No violations.'); //<--FLASH MESSAGE
else {
Session::flash('flash_message','Ops, Found some violations.'); //<--FLASH MESSAGE
}
return redirect()->route('assignees.create')->withInput();
}
how do I redirect with to previous page and keep user input?
Whilst editing the controller code that you submitted about I noticed some missing curly braces in your if statement - try this and let me know if it works:
public function violationcheck(Request $request)
{
$checked = violation::find($request->custidno);
if (!empty($checked)) {
Session::flash('flash_message','No violations.'); //<--FLASH MESSAGE
} else {
Session::flash('flash_message','Ops, Found some violations.'); //<--FLASH MESSAGE
}
return redirect()->route('assignees.create')->withInput();
}
Related
I have created an online store, but there is a problem that when I enter the discount code in, the condition is executed correctly and returns a message of success of the operation, but no data for the entered discount code is returned.
the check function
public function coupon_check()
{
$coupon = Coupon::where('code', request()->post('code'))->first();
if (isset($coupon) || !empty($coupon)) {
toast('Successfully !!!', 'success');
return redirect()->back()->with(['coupon'=>$coupon]);
} else {
toast('Not Found !!!', 'error');
return redirect()->back();
}
}
in here
<tr>
<td style="color: white">{{__('discount')}}</td>
<td style="color: white">#if($coupon){{$coupon->discount_value }}
#endif </td>
</tr>
when executing this form
<form action="{{ route('coupons.check') }}" method="POST">
#csrf
<input type="text" name="code" class="form-control text-center" placeholder="
{{ __('cupon code') }}">
<button name="confirm_code" class="btn btn-success col-md">{{ __('Confirm')
}}</button>
</form>
When you use with, you create a session, and to access that session, you should use session helper. So change your if statement like this:
#if(session('coupon')){{session('coupon')->discount_value }}
I am using a html form to get user data using laravel php. On form submit I am executing an api which in Web.php is defined as
Route::post('/register_user', 'UserRegistrationController#registerUser');
which executes,
public function registerUser(Request $request){
$apiResult = $this->executeApi($request->input('mobile_no'));
if($apiResult === "Success"){
return view('further_view');
}else{
toastr()->error('Registration Failed. Please check mobile number.');
return back();
}
}
html code
<div class="form-group{{ $errors->has('mobile_no') ? ' has-error' : '' }}">
<label for="mobile_no">Mobile No</label>
<input type="text" name="mobile_no" class="form-control" id="mobile_no" value="{{ old('mobile_no') }}" placeholder="Enter Mobile No" required>
<small class="text-danger">{{ $errors->first('mobile_no') }}</small>
</div>
My issue here is , whenever my $apiResult === "Success" condition goes false, i get toast message on screen but my page get refreshed and all the data user has typed gets cleared from the input box.
Hence my question is how can I show the toast messages without form being cleared or how would I prevent the input box getting cleared on such conditions.
I am using this Toast library
https://github.com/yoeunes/toastr
I would suggest that you use a session to store the data. For example:
session()->put('forms.mobile_no', $request->get('mobile_no'));
Your HTML:
<input value="{{ $mobile_no }}" type="text" name="mobile_no" class="form-control" id="mobile_no" placeholder="Enter Mobile No" required>
Try to return the error message back with this after the toastr call:
return redirect->back()->withInput();
I have a simple input form with a text input field and a submit button. I am trying to get the value from the input field to be displayed again on the same page after the submit button is clicked. So far laravel always throws an error that the variable is undefined.
Route:
Route::get('/find/names', "FindController#get_name")->name('names');
Controller
public function get_name(){
$name = Input::get('name_by_user');
return $name;
}
view
<form role="form" method="GET">
<div class="row">
<div class="form-group">
<div class="input-group">
<input type="text" name="name_by_user"/>
<span class="input-group-btn">
<button class="btn search-button" type="submit">
<span aria-hidden="true">
<span>Search</span>
</button>
</span>
</span>
</div>
</div>
</div>
</form>
display name after submitting: {{$name}}
I would do something like this
Route
Route::name('names')->get('/find/names', "FindController#get_name");
Controller
public function get_name(){
$collection = Input::all();
$name = $collection->pluck('name_by_user');
return view('view_file_in_resources', compact('name'));
}
Now you will have a $names collection in your view.
But if you only want to fetch result from one row, you controller should look like this:
public function get_name($name){
$name = Input::where('name_by_user', $name)->get();
return view('view_file_in_resources', compact('name'));
}
And your routes file
Route::name('names')->get('/find/names/{name}', "FindController#get_name");
When generating a view inside a controller for a route, you can do the following in a function to return a view with data depending on whether it exists.
public function showNameView() {
if(is_null(Input::get('name_by_user'))
{
return view('my.view')->with(['name' => Input::get('name_by_user')]);
}
else
{
return view('my.view')->with(['name' => Input::get('name_by_user')]);
}
}
You need to return the same view:
public function get_name(Request $request)
{
return view('same.view', ['name' => $request->name]);
}
Or you can redirect back:
return redirect()->back()->with('name', $request->name);
And display name like using session data:
#if (session()->has('name'))
{{ session('name') }}
#endif
I am using same method for get and post request of login authentication like
public function login()
{
if (Request::isMethod('post')){
// Getting credentials
$username = Input::get('username');
$password = Input::get('password');
// Authenticating super admin
if(Auth::attempt(array('email'=>$username,'password'=>$password,'sysadmin'=>1))){
return Redirect::To('superadmin');
}else{
Session::flash('message', 'Invalid Credentials !');
return View::make('superadmin::login');
}
}else{
echo View::make('superadmin::login');
}
}
This is the code of login view to display error messages:
#if(Session::has('message'))
<div class="alert-box message">
<h2>{{ Session::get('message') }}</h2>
</div>
#endif
The problem is when i enter invalid credentials while login, then error message is displayed at the form but after that on manually refreshing the login page, session message does not disappear. Where as, on second time page refresh, session message disappears.
I know that this is happening because URL (referrer) remain same in my case because same method "login" is being used for both types of request (get and post).
What would be the best way to display error messages in this case so that on page refresh or automatically the message should disappear.
This is an old question but I want to explain the easiest way:
#if(Session::has('message'))
<div class="alert-box message">
<h2>{{ Session::pull('message') }}</h2>
</div>
#endif
Wouldn't this be a beter fit?
return View::make('superadmin::login')->with('message', 'Invalid credentials');
and then in your view:
#if($message)
<div class="alert-box message">
<h2>{{ $message }}</h2>
</div>
#endif
This is because you need not to flash, as you never use a redirect after a flash.
I'd rather use Session:now() instead of Session:flash() and that's it. Method now flashes messages for immediate use.
i am currently working on a project where the login/register is handled through modal boxes (so i click the login button and a nice modal reveals with a form in).
Im using foundation 5's reveal modal to house my login form but when the form is submitted and theres a validation error the modal closes. The reason this is happening is because i am redirecting back to the route where the login form is and in that route a button needs to be clicked to fire the modal.
What i was wondering is, is there something i can set so that modal stays open if there is a validation error or exception (account not found etc.) So if there is a validation error the modal stays open.
looking for any type of solution. my code is shown below.
Login function
public function postLogin()
{
// Declare the rules for the form validation
$rules = array(
'email' => 'required|email',
'password' => 'required|between:3,32',
);
// Create a new validator instance from our validation rules
$validator = Validator::make(Input::all(), $rules);
// If validation fails, we'll exit the operation now.
if ($validator->fails())
{
// Ooops.. something went wrong
return Redirect::back()->withInput()->withErrors($validator);
}
try
{
// Try to log the user in
Sentry::authenticate(Input::only('email', 'password'), Input::get('remember-me', 0));
// Get the page we were before
$redirect = Session::get('loginRedirect', 'dashboard');
// Unset the page we were before from the session
Session::forget('loginRedirect');
// Redirect to the users page
return Redirect::to($redirect)->with('success', Lang::get('auth/message.signin.success'));
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
$this->messageBag->add('email', Lang::get('auth/message.account_not_found'));
}
catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
{
$this->messageBag->add('email', Lang::get('auth/message.account_not_activated'));
}
catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
{
$this->messageBag->add('email', Lang::get('auth/message.account_suspended'));
}
catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
{
$this->messageBag->add('email', Lang::get('auth/message.account_banned'));
}
// Ooops.. something went wrong
return Redirect::back()->withInput()->withErrors($this->messageBag);
}
Login modal
<div id="myModalLogin" class="reveal-modal small" data-reveal>
<h2>Login</h2>
<form method="post" action="{{ route('login') }}">
{{-- CSRF Token --}}
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
{{-- Email --}}
<label for="email"> Email
<input type="text" name="email" id="email" value="{{ Input::old('email') }}" />
</label>
{{ $errors->first('email', '<label class="error">:message</label>') }}
{{-- Password --}}
<label for="password"> Email
<input type="password" name="password" id="password" value=""/>
</label>
{{ $errors->first('password', '<label class="error">:message</label>') }}
{{-- Remember me --}}
<input name="remember-me" value="1" id="remember-me" type="checkbox"><label for="remember-me">Remember me</label>
<hr>
{{-- Form Actions --}}
<button type="submit" class="button">Sign in</button>
I forgot my password
<a class="close-reveal-modal">×</a>
</div>
You need to create a flag variable that you will pass to your view and set it true if you want the modal to auto open and set it false if you don't want to open it:
The problem with this is that ->with() doesn't work with Redirect::back() so we need a workaround: lets pass our flag variable as an input. For this you have to get all the old input and add the new flag variable to them. Make sure that the key (your flag variable name) doesn't already exist. You can check this with a var_dump(Input::all()).
$input = Input::all();//Get all the old input.
$input['autoOpenModal'] = 'true';//Add the auto open indicator flag as an input.
return Redirect::back()
->withErrors($this->messageBag)
->withInput($input);//Passing the old input and the flag.
Now in your view you have to print this "old" input into your JavaScript condition. If it exists it will print its value: true, otherwise it will print the second argument: false.
<script>
$(document).ready(function () {
if ({{ Input::old('autoOpenModal', 'false') }}) {
//JavaScript code that open up your modal.
}
});
</script>
You can return false; when you return the validations results.