Maram AlSaegh
Maram AlSaegh

Reputation: 69

Add value to select tag - HTML

I'm working on a Laravel application. In my view i have a form, what I'm trying to do is when submitting the form and one field is not valid, it returns the values of valid fields, so the user will have to edit the wrong field only, not the full form.

This is my form and an example. In here, the user didn't enter the url, so it shows an error message and return the values of the correct fields (client, domain provider, etc...) The problem i'm facing is this is not working for the avatar field and service field!

enter image description here

I'm returning the value by adding value="{{ old('client') }}" to the input tag , as shown in code below:

 <div class="form-group row">
     <label class="col-lg-3 col-form-label">Client:</label>
     <div class="col-lg-9">
         <input type="text" class="form-control" name="client" value="{{ old('client') }}">
         <small class="error">{{$errors->first('client')}}</small>
     </div>
  </div>

QUESTION: How to add it for avatar and select service fields?

Avatar: - currently its not returning the value

 <div class="form-group row">
     <label class="col-lg-3 col-form-label">Attach avatar:</label>
     <div class="col-lg-9">
         <input type="file" class="form-input-styled" data-fouc="" name="avatar" value="{{ old('avatar') }}">
         <small class="error">{{$errors->first('avatar')}}</small>
      </div>
  </div>

Select service:

<div class="form-group row">
    <label class="col-lg-3 col-form-label">Select Service:</label>
    <div class="col-lg-9">
        <select data-placeholder="Select service" class="form-control form-control-select2" data-fouc="" name="service" >
            <option></option>
            <option value="Website">Website</option>
            <option value="Application">Application</option>
         </select>
         <small class="error">{{$errors->first('service')}}</small>
     </div>
 </div>

Upvotes: 0

Views: 1089

Answers (2)

haffis asma
haffis asma

Reputation: 97

hi for select tag this work for me

   <div class="form-group row">
    <label class="col-lg-3 col-form-label">Select Service:</label>
    <div class="col-lg-9">
        <select data-placeholder="Select service" class="form-control form-control-select2" data-fouc="" name="service" >
            <option></option>
        @foreach($Listservice as $key)

            <option value ="{{$key->id}}"> {{$key->Website}} 
                 {{$key>Application}}</option>
        @endforeach
         </select>
     </div>
 </div>

and in your model add these $listservice = Service::get(); i hope it s help

Upvotes: 0

waseem Alhabahs
waseem Alhabahs

Reputation: 51

For the select tag ,
you must put a condition for every option tag ,
if the last submitted value is equal to this option value put selected attribute to this option

something like that ....

    <div class="form-group row">
    <label class="col-lg-3 col-form-label">Select Service:</label>
    <div class="col-lg-9">
        <select data-placeholder="Select service" class="form-control form-control-select2" data-fouc="" name="service" >
            <option></option>
            <option value="Website" {{ old('service') == "Website" ? "selected" : ""}}>Website</option>
            <option value="Application" {{ old('service') == "Application" ? "selected" : "">Application</option>
         </select>
         <small class="error">{{$errors->first('service')}}</small>
     </div>
 </div>

Upvotes: 1

Related Questions