Ferenc Straub
Ferenc Straub

Reputation: 115

Laravel 5, link to previous form page and retrieve post input

I have two pages. The search and the found.

On the search page there are searching fields. The 'found' page gives back the results of search.

In the template of found page there is a button.

<a class="btn btn-default" href="/search">Back</a>

When I click on the "BACK" button, the input data, which had been typed in, disappears. How could I make a link, which retains the input fields?

Upvotes: 0

Views: 340

Answers (1)

Nono
Nono

Reputation: 188

You can store the searched variable in the "found" page, retrieve it when you come back to "search," and tell Laravel to "forget" it after you leave that page.

Store in /found:

Session::put("search", Input::get("search_field_name"));

Retrieve in /search when coming back:

Session::get("search");

Forget in /search after you've set the search field variable:

Session::forget("search");

This will display your last search even if you are not coming back from somewhere else. What you can do is only get and forget whenever your URL::previous() is a /found URL.

In case you want to store all the inputs at once, so you don't have to handle each of them sepparately, you can do with Session::put("search", Input::all());

Later on, you can retrieve them both as an array or as an object in order to access its properties:

$input_array = Input::all(); $input_object = (object)$input_array;

Then, access a property called, for instance, "search_term,", you can use the array:

$input_array["search_term"];

Or the object:

$input_object->search_term;

Hope this helps!

Upvotes: 1

Related Questions