Home > Back-end >  How to get select menu session data in another page laravel 8
How to get select menu session data in another page laravel 8

Time:03-02

I can successfully get input session data on another page but I can't get select menu selected data in another page.

This is my first form

<form action="{{ route('admin.create.step.one.post') }}" method="POST">
    @csrf

    <label for=""> Student Name </label>
    <input type="text" name="student_name" value="{{ $report->student_name ?? '' }}">
    <br>

    <label for=""> Email </label>
    <input type="text" name="student_email" value="{{ $report->student_email ?? '' }}">
    <br>

    <label for=""> Phone number </label>
    <input type="text" name="student_phone" value="{{ $report->student_phone ?? '' }}">

    <label for=""> Group </label>
    <select name="group" id="">
        <option value="1" {{ $report->group ?? '' }}> Science </option>
        <option value="2" {{ $report->group ?? '' }}> Arts </option>
    </select>

    <button type="submit"> Preview </button>

</form>

I want to get session data in this page.

I can't get select menu session data.

<form action="">
    @csrf
    <table>

        <td> {{ $report->student_name }} </td>
        <td> {{ $report->student_email }} </td>
        <td> {{ $report->student_phone }} </td>
        <td> {{ $report->group }} </td>

    </table>
</form>

CodePudding user response:

As far as I understood from your question you want to select the item within your select input.

You are just missing this line:

{{ $report->group == 1 ? 'selected' :'' }}
<form action="{{route('admin.create.step.one.post')}}" method="POST">
    @csrf
    <label for="student_name">Student Name</label>
    <input type="text" name="student_name" id="student_name" value="{{ $report->student_name}}">
    <br>
    <label for="student_email">Email</label>
    <input type="text" name="student_email" id="student_email" value="{{ $report->student_email}}">
    <br>
    <label for="student_phone">Phone number</label>
    <input type="text" name="student_phone" id="student_phone" value="{{ $report->student_phone}}">
    <br>
    <label for="group">Group</label>
    <select name="group" id="group">
        <option value="1" {{ $report->group == 1 ? 'selected' :'' }}>Science</option>
        <option value="2" {{ $report->group == 2 ? 'selected' :'' }}>Arts</option>
    </select>
    <button type="submit">Preview</button>
</form>

Now, if you need are storing in the session() something and not getting it please update your question with your Controller and I will update my answer to help you further.

CodePudding user response:

I see that you want to get the selected item when you submitted and an error appeared so if you want to get the old value you can do so using old() method in your blade.

{{ old('group') == 1 ? 'selected' :'' }}
<form action="{{route('admin.create.step.one.post')}}" method="POST">
    @csrf
    <label for="student_name">Student Name</label>
    <input type="text" name="student_name" id="student_name" value="{{ $report->student_name}}">
    <br>
    <label for="student_email">Email</label>
    <input type="text" name="student_email" id="student_email" value="{{ $report->student_email}}">
    <br>
    <label for="student_phone">Phone number</label>
    <input type="text" name="student_phone" id="student_phone" value="{{ $report->student_phone}}">
    <br>
    <label for="group">Group</label>
    <select name="group" id="group">
        <option value="1" {{ old('group') == 1 ? 'selected' :'' }}>Science</option>
        <option value="2" {{ old('group') == 2 ? 'selected' :'' }}>Arts</option>
    </select>
    <button type="submit">Preview</button>
</form>
  • Related