-1

I want to add other value as one of the options and show a new input field to write new value doesn't exist in options.

This is the value of my options ON my Seed part.

public function run()
{
    Model::unguard();

    //delete  table records
    DB::table('providers')->delete();
    //insert some dummy records
    DB::table('providers')->insert(array(
        array('id'=>'1','name'=>' private'),
        array('id'=>'2','name'=>' public'),
        array('id'=>'3','name'=>' semi private'),
        array('id'=>'4','name'=>'other'),
    ));
}

and this is my code in the form

<div class="form-group">
    <label for="provider_id"> Provider Type </label><br>
    <select name="provider_id" class="form-control" style="width:50%" required>
        <option></option>
        @foreach($providers as $provider)
            <option value="{{$provider->id}}">{{$provider->name}}</option>
        @endforeach
    </select>
    <input name="provider" style="width:50%" id="provider_id4"  hidden="hidden" />
</div>

And this is javascript code

$("#provider_id").change(function () {
    var selected_option = $('#provider_id').val();
    if (selected_option === '4') {
        $('#provider_id4').attr('pk','1').show();
    }
    if (selected_option != '4') {
        $("#provider_id4").removeAttr('pk').hide();
    }
})

I want to store old value(1,2,3) and new if the user chose 4 in provider_id column in database which I store this column in my controller

$excuse->provider_id = $request->input('provider_id');
Jeroen
  • 1,168
  • 1
  • 12
  • 24
shrooq
  • 89
  • 1
  • 2
  • 8
  • Which parts of your requirements are already solved using that code? What is missing? What have you tried to fill the missing pieces? – Nico Haase Nov 21 '19 at 12:39

1 Answers1

0
  1. You'll have to use Javascript or jQuery to accomplish this.
  2. Remove this if condition and take the label and text box outside the select tag. @if ($provider->id== 4)

  3. Default hide label and select box.

  4. When user selects "other" option, use jQuery or javascript and show the label and text box.

  5. Done. It should work.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nikhil G
  • 2,096
  • 14
  • 18
  • Where I can add JavaScript? and how I called it in this part? – shrooq Nov 21 '19 at 07:30
  • $("#provider_id").change(function () { var selected_option = $('#provider_id').val(); if (selected_option === '4') { $('#provider_id4').attr('pk','1').show(); } if (selected_option != '2') { $("#provider_id4").removeAttr('pk').hide(); } }) This is Javascript code .. where I put it? – shrooq Nov 21 '19 at 07:31
  • You can add at the end of your page. You will have to call the javascript function on change of select box. – Nikhil G Nov 21 '19 at 07:31
  • I edited my question .. please check what is the problem? – shrooq Nov 21 '19 at 08:58