How to output message after form submit on the same page? - laravel

When a user sumbit a form I need to output message for him on the same page.
When I submit a form the page refreshes, handler fires (php function), some POST data inserts into database, I set $this['success'] = 1 and then check with Twig:
{% if success %}
here is many tags with information and a message
{% else %}
here is a form
{% endif %}
But if I press F5 the form is resubmitting, the insert query executes again.
Do this request with AJAX isn't a real option.

There are numerous ways to answer this question (use Cache, database, cookies, session). This is really easy using Session Flashing or in this instance I am going to show you how to do it with Returning a redirect with flash data.
Spin up a CMS Page
Place HTML Form code: *Note the {{ something }} is important to show our response after submitting the form. This request does not use AJAX.
{{ something }}
{{ form_open({ request: 'onSubmit', class: 'row', autocomplete: 'off' }) }}
{{ form_input('text', 'something', '', { class: 'input padding' }) }}
{{ form_submit('Submit', { class: 'button padding' } ) }}
{{ form_close() }}
PHP code:
function onStart() {
$something = Session::get('something');
$this['something'] = $something;
}
function onSubmit() {
$something = Input::get('something');
return Redirect::refresh()->with('something', $something);
}
Test and enjoy one way to create a message after submitting a form.

If you are using laravel then you can easily implement to handle this functionality
In View file you need to write this code, which will display message on the same page where you are displaying form.
file.blade.php file
#if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
</div>
#endif
FormController.php
public function addUser(Request $request){
User::create([
'name' => $request->name,
'email' => $request->email,
'gender' => $request->gender,
'contact' => $request->contact,
.....]);
//below code help to display the message that you want on same page whereuser entered data
return back()->with('success', 'Thanks you for adding!!');
}
return back()->with('success', 'Thanks you for adding!!');
use this code for laravel project to display message on same page

Related

Laravel Cannot Decrypt Encrypted ID On Controller

I cannot decrypt the encrypted value on a controller after clicking on the submit button on my blade file below.
Controller :
public function edit($id)
{
$encrypted_id = encrypt($id);
return view('my.blade.edit', compact('encrypted_id'));
}
public function update(Request $request, $id)
{
$decrypted_id = decrypt($id);
dd($decrypted_id);
}
Blade: (my.blade.edit)
{{ Form::open(['route' => ['route.update', $encrypted_id ], 'method' => 'PATCH']) }}
{{ Form::button('Update', ['type' => 'submit', 'name' => 'update']) }}
{{ Form::close() }}
I am expecting an integer value on my dd(); but I still getting an encrypted string.
Well, as I've already written in the comments, first and simple is to check expected output and exact output.
So far we discovered, that value was sent to view isn't equal to value received in update() method.
id was encrypted twice, but we don't see two encrypt() calls in the code from the question. Probably some other code layer was making that.

How to clear form input after save?

How can I clear form input on form submit?
After URL is saved I return the view for creating a form so I can add new URL.
The problem is that this time URL is populated from previous save.
How can I empty it after save?
Isn't that the point of second parameter which is set to null?
Using laravel version 5.4.36.
In the "create" iew I have a form with URL input:
{!! Form::open(['route' => 'urls.store']) !!}
{{ Form::url('url', null, array('class' => 'form-control')) }}
{{ Form::submit('Add domain', array('class' => 'btn' )) }}
Store method saves URL to database and returns the same view:
public function store(Request $request)
{
// URL Validation
$this->validate($request, array(
'url' => 'required|url|unique:urls'
));
// Save
$url = new Url;
$url->url = $request->url;
$url->save()
return view('urls.create');
}
Your form view should be handled by a get request on Route and submitting form is a post request .So after submitting the form you should redirect the same page not just returning the view . Check the web.php is the Routes are ok or not . Try to
add it to the store method ,I think it may help you
return redirect()->back();
or with flash message
return redirect('urls.create')->with('success', 'URL has been added');
you can make value = some values come from the controller with empty values when it's the parameters is set i.e use isset function
or
you can make a flag in the view when it sends to controller check it if it is true means you submit the form, in this case, make the values with empty

Laravel5.2 delete doesn't work

I developed website with CRUD on products table .this is the structure of the table.
Create and update works fine But delete not work.
This is the form in blade to delete product
{{ Form::open(array('url' => 'admin/products/' . $product->id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete ', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
And this the destroy function in controller
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
// Product::destroy($id);
return redirect('admin/products')->with('message', 'Successfully deleted the product!');
}
And This is my routes
Route::group(['middleware' =>'App\Http\Middleware\AdminMiddleware'], function () {
//resource
Route::resource('admin/products','AdminFront');
});
When I click delete button it enter the destroy function and dd($id) correct
But when write
$product = Product::find($id);
$product->delete();
Or
Product::destroy($id);
I get this error
The localhost page isn’t working
localhost is currently unable to handle this request.
This error tired me . I developed delete fun with resource API in another table and work fine.I don't know are the problem in the db or where. please any one help me ,
What does your routes.php look like?
You may need to include the resource route in routes.php.
Route::resource('admin/products/', 'TheNameOfYourController');
But make sure the route is protected either in the controller or routes.php.
Here is somewhat the same setup you have:
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/routes.php LINE 119
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/Controllers/UsersManagementController.php LINES 369-376
https://github.com/jeremykenedy/laravel-material-design/blob/master/resources/views/admin/edit-user.blade.php LINES 243-246
Cheers!

Laravel explain me how working query

I have a problem with query on laravel.
Please show me how it work, because I can't understand doc.
For example, I have VideoController.php and I have some data from forms:
$gall = array(
'name' => Input::get('name'),
'user_id' => Auth::id()
);
now I want to add this data to DB, but I don't know how to call to create function in model (and how this function should look).
And please explain me, how I should select data from database and display it on view, where for example user_id = 15;
In your VideoController.php file you should have a method to store the data, which you should post your data to through a form or something.
In a view, this form should look something like this:
{{ Form::open(['route' => 'video.store']) }} <!-- check your routes to see if video.store exists, you'll get an error otherwise -->
... form elements ...
{{ Form::submit('Submit') }}
{{ Form::close() }}
In your store() function you should have code similar to this
$video = new Video(); // if your video model is Video.php
$video->name = Input::get('name');
$video->user_id = Auth::id();
$video->save();
And if you want to display data, i.e. your video index or something, in your VideoController.php file, in the index() function:
$videos = Video::all();
return View::make('video.index', array('videos' => $videos));
then in your view
#foreach($videos as $video)
{{ $video->name }}
#endforeach

How to get value of Input object in a Route Filter?

I've been following the Laravel guides in their website, but there's one thing that it's not clear to me. In the tutorial they describe this filter:
Route::filter('old', function() {
if (Input::get('age') < 200) {
return Redirect::to('home');
}
});
What is the value of Input how does it get assigned?
The Route using that filter is not very descriptive:
Route::get('user', array('before' => 'old', function() {
return 'You are over 200 years old!';
}));
My guess is that Input represents the user, but how does it get assigned? He's not fetching the user from the DB, nor anything.
The ´Input´ (pretty much) always come from a form the user posted on your page. In this case you should have an input field named 'age' on it.
Take a look at requests and inputs: Laravel 4 Requests & Input
A form example for it, written in Laravel Blade:
{{ Form::open(array('url' => 'foo/bar')) }}
{{ Form::text('name') }}
{{ Form::text('age') }}
{{ Form::submit('Submit this form') }}
{{ Form::close() }}
As soon as you submit this form you will be able to:
echo Input::get('name');
echo Input::get('age');
or just everything you submited:
var_dump(Input::all());

Resources