Sunday, April 16, 2017

Dynamic route path in form action based on the selected option in Laravel 5

I have a form like this in laravel 5.4 view:

<form action="#" method="POST" class="form-inline" role="form">
        
            <div class="form-group">
                <select id="tdcategory" class="selectpicker show-tick show-menu-arrow" onchange="this.form.submit()">
                  @foreach($categories as $category)
                        <option value=""></option>
                  @endforeach
                </select>
            </div>
            <div class="form-group">
            <select id="tdlocation" class="selectpicker show-tick show-menu-arrow" onchange="this.form.submit()">
                @foreach($locations as $location)
                    <option value=""></option>
                @endforeach
            </select>
            </div>
</form>

I have two questions:

1) How can I dynamically set the form action to something like

categories/{category->id}/locations/{location->id}

Where, both category->id and location->id will be based on the value of the option the user has currently selected

2) Since the form gets submitted as the user changes the option, the page is then reloaded and resets both of the <select> tag. How do I keep track of previously selected option and as the page loads, I can show the user which one did he/she select before the form was submitted?



via Eisenheim

Advertisement