php - Laravel 8 - Request does not retrive input field
I am using Laravel 8. I have the following search box in my blade file:
<form name="searchForm" class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" method="POST" action="{{ url('search') }}">
@csrf
<input name="searchField" type="search" class="form-control form-control-dark" style="width: 426px; " placeholder="Search for news, symbols, tickers or companies">
</form>
I route this to my SearchController
:
public function showSearchPage(Request $request) {
$field = $request->searchField;
$name = Input::get('searchField');
.
.
.
However, $request->searchField
is null. My $request
object looks like the following:
Besides the token, my input field is not within the request
.
Any suggestions on what I am doing wrong?
I appreciate your replies!
Answer
Solution:
Change the form
attribute of your form to id
and try the form
attribute on the input field.
Normally you shouldn't have to define either of those, you might try removing the id
from your form and also the form
from the input field.
It is possible that the initial form
attribute on your form caused some trouble.
Let me know if the second approach also worked.
Answer
Solution:
What I know is that, use form id when you need to perform a validation for a script or you have multiple forms in a single page, try changing your form attribute to id, or better if you completely remove it
Answer
Solution:
for search form its better to use GET method instead of POST
and instead of
type="search"
used
type="text" (for test)
Answer
Solution:
added id="searchField" to your input and changed type to text form search. should work
<form name="searchForm" class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" method="POST" action="{{ url('search') }}">
@csrf
<input name="searchField" id="searchField" type="text" class="form-control form-control-dark" style="width: 426px; " placeholder="Search for news, symbols, tickers or companies">
</form>
Source