How to get select menu session data in another page laravel 8 - laravel

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>

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.

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

How to add <select> element in Laravel jetstream / livewire code

I'm trying to add element to user profile form, there is no problem in the registration form but I don't know how to add it in user profile update page
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="name" value="{{ __('Name') }}" />
<x-jet-input id="name" type="text" class="mt-1 block w-full" wire:model.defer="state.name" autocomplete="name" />
<x-jet-input-error for="name" class="mt-2" />
</div>
<!-- Email -->
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="email" value="{{ __('Email') }}" />
<x-jet-input id="email" type="email" class="mt-1 block w-full" wire:model.defer="state.email" />
<x-jet-input-error for="email" class="mt-2" />
</div>
I tried to add this but it doesn't work
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="gender" value="{{ __('Gender') }}" />
<select id="gender" class="block mt-1 w-full" name="gender">
<option value="m" {{ $this->user->gender== 'm' ? 'selected' : '' }} >
Male
</option>
<option value="f" {{ $this->user->gender== 'f' ? 'selected' : '' }}>
Female
</option>
</select>
</div>
Laravel Livewire: Input select, default option selected
Livewire will auto select option, just use wire:model
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="gender" value="{{ __('Gender') }}" />
<select id="gender" class="block mt-1 w-full" wire:model="gender">
<option value="m">
Male
</option>
<option value="f">
Female
</option>
</select>
</div>

how to add Old data to select inputs inform

this code is ok with text inputs
<input value="{{ old('YOM') }}" id="YOM" class="form-control" type="year" name="YOM" id="">
but want add old data to select tag s and also radio buttons
<select id="condition" name="condition" class=" form-control" >
<option value="" >Select Condition</option>
<option value="Brand New" >Brand New</option>
<option value="Recondition" >Recondition</option>
<option value="Used" >Used</option>
</select>
<input type="checkbox" name="SaleOrRent" value="Sell">Sell <span></span>
<input type="checkbox" name="SaleOrRent" value="Rent">Rent
<input type="checkbox" name="SaleOrRent" value="Sell or Rent">Sell or Rent
You could do this.
<select id="condition" name="condition" class=" form-control" >
<option value="" >Select Condition</option>
<option value="Brand New" {{ old('condition') === 'Brand New' ? 'selected' : '' }} >Brand New</option>
<option value="Recondition" {{ old('condition') === 'Recondition' ? 'selected' : '' }} >Recondition</option>
<option value="Used" {{ old('condition') === 'Used' ? 'selected' : '' }} >Used</option>
</select>
<input type="checkbox" name="SaleOrRent" value="Sell" {{ old('SaleOrRent') === 'Sell' ? 'checked' : '' }} >Sell <span></span>
<input type="checkbox" name="SaleOrRent" value="Rent" {{ old('SaleOrRent') === 'Rent' ? 'checked' : '' }} >Rent
<input type="checkbox" name="SaleOrRent" value="Sell or Rent" {{ old('SaleOrRent') === 'Sell or Rent' ? 'checked' : '' }} >Sell or Rent
<option value="{{old('condition'}}" >Select Condition</option>
and for the radio:
<input type="checkbox" name="SaleOrRent" value="{{old('saleOrRent)}}">Sell <span></span>

How to get old input array in blade laravel

I have some code like this
<div class="form-group">
<label for="tag">Tag</label><br>
<input type="text" data-role="tagsinput"
class="form-control form-control-lg #error('tag') is-invalid #enderror"
id="tag" name="tag[]" value="{{old('tag')}}" placeholder="Enter tag">
#error('tag') <div class="text-danger"> {{ $message }} </div> #enderror
</div>
how to get old value array in laravel blade, in this case i want to get old value of tag?
use dot notation with index
as suggested here https://laracasts.com/discuss/channels/laravel/input-old-and-array
<input type="text" data-role="tagsinput"
class="form-control form-control-lg #error('tag') is-invalid #enderror"
id="tag" name="tag[]" value="{{old('tag.0')}}" placeholder="Enter tag">
...
<input type="text" data-role="tagsinput"
class="form-control form-control-lg #error('tag') is-invalid #enderror"
id="tag" name="tag[]" value="{{old('tag.1')}}" placeholder="Enter tag">
I think the better solution for this is to do it with javascript if you have one input so you need to store the array in javascript variable then just add the value to the input.
<select name="groups[]" class="multi-default" id="groups" placeholder="Groups" multiple>
<option value="" placeholder>Groups</option>
#foreach ($groups as $group)
<option value="{{ $group->id }}" title="{{ $group->id }}"
{{is_array(old('groups',$groups??[]))&&in_array($group->id,old('groups',$group_data??[]))?'selected':null}}</option>
#endforeach
</select>
It will definitely work
#if (!is_null(old('product_highlights')))
#foreach (old('product_highlights') as $highlight)
{{$highlight}}
#endforeach
#endif

How to add permission for a user on specific page

I have view, edit and delete permissions to a specific page, so I need to get these values:
page_id, user_id, edit, view, delete
I have a form with page name and 3 checkboxes for each page
<div class="container">
<form action="/test" method="POST">
#csrf
<div class="form-group">
#foreach( $tmp as $key=>$val )
<li>
#if( !isset($val['children']) && empty($var['children']))
<b>{{ $val['data']['name'] }}</b>
#else
<b>{{ app()->getLocale() == 'ar' ? $val['data']->ar_name : $val['data']->display_name }}</b>
#endif
#if( isset($val['children']) and !empty($val['children']))
<ul>
#foreach( $val['children'] as $c=>$child)
<li>
{{ $child->name }} :
<div class="form-group">
<input type="hidden" name="user_id" value="{{ $user_id }}">
<input type="hidden" name="page_id" value="{{ $child->name }}">
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="view">View
</label>
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="edit">Edit
</label>
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="delete">Delete
</label>
</div>
</li>
#endforeach
</ul>
#endif
</li>
#endforeach
<br>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
but when i sent data, i can't get the correct page_id, is there anything else can i do
I solved it by adding $child->id
<div class="form-group">
<input type="hidden" name="user_id" value="{{ $user_id }}">
<input type="hidden" name="page_id[{{$child->id}}]" value="{{ $child->id }}">
<label class="checkbox-inline">
<input type="checkbox" name="view[{{$child->id}}]" value="1">View
</label>
<label class="checkbox-inline">
<input type="checkbox" name="edit[{{$child->id}}]" value="1">Edit
</label>
<label class="checkbox-inline">
<input type="checkbox" name="del[{{$child->id}}]" value="1">Delete
</label>
</div>

Get the value of my dropdownlist after an error of validation

I have 2 fields sexe and adresse, my variable sexe is a dropdownlist.
I select an item for example tha value homme. Then, the value of the adresse is Rue du Lac 15.
In my example, the validation is wrong...
Why, the item of my variable sexe is empty? Where is my value?
I don't understand where is my problem?
<fieldset class="form-group {{ $errors->has('sexe') ? 'has-error' : '' }}">
<label for="company-content">Sexe</label>
<select name="sexe" id="sexe" class="form-control" required="required" value="{{ old('sexe')}}"/>
<option value="">Choix sexe</option>
<option>Femme</option>
<option>Homme</option>
{!! $errors->first('sexe', '<span class="help-block">:message</span>') !!}
</select>
</fieldset>
<fieldset class="form-group {{ $errors->has('adresse') ? 'has-error' : '' }}">
<label for="form-group-input-1">Adresse</label>
<input type="text" name="adresse" id="adresse" class="form-control" required="required" value="{{ old('adresse')}}"/>
{!! $errors->first('adresse', '<span class="help-block">:message</span>') !!}
</fieldset>
Your sexe options don't have any value. Edit your code:
<option value="femme">Femme</option>
<option value="homme">Homme</option>

Resources