skip to Main Content

We have a simple search/list form built into the Home page:

<form id ="search-by-country" method='post' action='/alphabetical-list'>
    <!-- Include the CSRF token -->
    @csrf
    <select name="country_id" id="country_id">
        <option value="US">United States</option>
        <option value="FR">France</option>
        <option value="DE">Germany</option>
        <option value="JP">Japan</option>
    </select>

    <button type="submit" class="btn btn-darkorange">Submit</button>
</form>

We have a separate results page which works fine as a separate page meaning that if you click on it it display properly as a normal page, albeit with formatted search results.

Since the results page offers extended search parameters we set up a route in the web.php and it’s own controller page.

If we make a selection and click Submit it finds the alphabetical-list page but is completely out of context. By this I mean there is no formatting, it is missing all the page parts, is only display the page’s extended search as simple HTML, this image:

enter image description here

I’m fairly new to Laravel, can’t figure out how to get this to work. It seems so so simple.

Here is the controller file.

namespace AppHttpControllers;

use AppDTECustomFunctions;
use IlluminateHttpRequest;
use IlluminateFoundationHttpFormRequest;

class AlphaSearchController extends Controller
{
    public function search(Request $request)
    {
        $fname = $request->input('fname');
        $name = $request->input('name');
        $city = $request->input('city');
        $country = $request->input('country');
        $country_code = $request->input('country_code');
        $country_name = $request->input('country_name');
        $contemporary = $request->input('contemporary');
        $from = $request->input('from');
        $to = $request->input('to');
        $search_type = $request->input('search_type');

        return view('public.search.search', [
            'name' => $name,
            'city' => $city,
            'country_code' => $country_code,
            'country_name' => $country_name,
            'from' => $from,
            'to' => $to,
            'contemporary' => $contemporary,
            'search_type' => $search_type,
        ]);
    }
}

The question: It posts to the search results page but displays an unformatted page, like the images. How do we get it to post and display the complete page with HTML, CSS, Javascript, and the other parts like header, footer that make up the page?

Here is the route in the web.php file that works for the alphabetical-list page but although it gets you there for the search on the Home page does not return the full page:

Route::post('alphabetical-list', [AppHttpControllersAlphaSearchController::class, "search"])->name("search");

My apologies but I find the laravel routes confusing. This line in the controller file was incorrect:

    return view('public.search.search', [

The corrected line is

    return view('public.components.alphabetical-list', [

Now it is passing the country_code value to the alphabetical-list page/file but the problem of incomplete page remains.

2

Answers


  1. Chosen as BEST ANSWER

    The solution was unbelievably simple, counter-intuitive to what I was expecting.

    Alphabetical Listing is a perfectly complete/functioning search/results page. All that was necessary was to pass data from the search. I expected that a route and controller file were necessary.

    Not so in this case. The original form line:

    <form id ="search-by-country" method='post' action='/alphabetical-list'>
    

    Throwing a dart at it I first removed the id, no change, then the method-'post'. I don't know laravel well enough to understand why but that was it. The solution:

    <form action='alphabetical-list'>
    

    Thank you for the suggestions.


    • The user’s browser is navigating as designed.
      Therefore the form is submitting where intended.
    • The search results are displaying.
      Therefore the form is submitting what is intended, and the database is being queried as intended.

    Assuming you’re not using JS to reload the page, the forms on the /alphabetical-list and home routes must be returning different views, and therefore accessing different controllers. But to behave as you‘re expecting, they should use the same controller.

    An easy way to achieve that is to use the same method and target the same destination (the forms’ action attribute). Using Laravel’s tools for named routes (like route()) can help avoid this type of route-controller confusion.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search