I am currently doing a simple search query with laravel and I was wondering the difference between hitting the reload button and actually refreshing by clicking URL bar and hitting return button. Here is what I've done.
I am not passing any parameters or such. I am aware that with the current setup, the reload button would do a GET request. This is why I've used match in this situation. I get the current searched results just fine. But if I go on to the URL bar and hit the return button, it gives me all the posts. It is obvious that as there is no set parameter given to search for, and when GET method is used, it just displays all the available data. Now how can I resolve this? Do I 'have' to pass in the search input's data to the route in order for this to not happen?
If you refresh, you are resending the POST request (typically your browser will ask you to confirm form resubmission), while hitting enter when you have focussed on the address bar is explicitly a GET request.
It might be worthwhile submitting the search form as a GET request in any case - make it bookmarkable/linkable.
So my question now is, How would I pass the second parameter for the search. Should I use JS to just take the input value and put it in there? Or is there a right Laravel/PHP way?
Yes that is the second parameter which is suppose to be the input of search. Here is what I had in mind (If I were to use JS). Doing something like this (Not tested):
var query = $('#form #search').val();
var submit = $('#form');
submit.click(function(e){
e.preventDefault();
window.location('/search/'+query);
});
I was actually looking for an alternative other than to display the action, method and other stuff and just completely use js, jquery, ajax etc... My application currently is based off of almost completely ajax and I wanted actually be able to use js for this one. Will using the above method be recommended or should I do this the simple way and save the hassle?
Also, wouldn't using the GET method still return all the data if I visited the /search link without any input?
wouldn't using the GET method still return all the data if I visited the /search link without any input?
No, as I mentioned in the last comment, you would check for the presence of a 'search' parameter in the Request. If there is do a search query, otherwise do not. Here is a more complete example:
public function mainSearch(Request $request)
{
$results = [];
if ($request->has('search') {
$results = // do a query that gets the search results using $request->search
}
return view('main.search', compact('results'));
}
*You could intercept the form submit using javascript and do some string manipulation on the input value, e.g. replace spaces with underscores, and then undoing that in the mainSearch controller
But why bother... ?search=this+is+my+searchis good enough for google
Yap removed that part. Still on the whole "passing parameter" thing. Meaning /search/what+ever+here kinda thing. But yeah this works the same way and like you mentioned, SEO approved (they really need to come up with emojis) ?