Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

7ibbe77s's avatar

How to retain the selected option from a dropdown menu on page load

I was able to retain the inputted text in the two text fields, but I can't seem to get it to work with the drop down menu.

0 likes
5 replies
jlrdw's avatar

code here just like going to github.

3 back ticks, new line, code, new line, 3 back ticks.

jlrdw's avatar

You probably just need to post your form, work out the answer in the controller, and display what you need to display on a new answer page.

Also I would highly suggest dropping the collective class and using regular blade and HTML. Many here has said the same including Jeffrey.

The code in that class just makes it harder to read.

7ibbe77s's avatar
7ibbe77s
OP
Best Answer
Level 1

<div class=" d-inline-block">
        <select class="form-control custom-select" name="operators" value="{{ Session::get('operators')}}">
                <option value="+" id="plus" name="plus" {{ Session::get('operators') == '+' ? 'selected' : '' }}>Addition (+)</option>
                <option value="-" id="minus" name="minus" {{ Session::get('operators') == '-' ? 'selected' : '' }}>Subtraction (-)</option>
                <option value="*" id="times" name="times" {{ Session::get('operators') == '*' ? 'selected' : '' }}>Multiplication (*)</option>
                <option value="/" id="divide" name="divide" {{ Session::get('operators') == '/' ? 'selected' : '' }}>Division (/)</option>
            </select>

Please or to participate in this conversation.