1

Hye everyone! First of all, my coding all about dynamic dropdown value, the issues now is the second dropdown value is not save inside the database. Which part did I missed or wrong?

View:-

<div class="col-md-3">
    <select class="shiftPatternID" name="inputShiftPatternID" id="inputShiftPatternID" required style="width: 100%">
        <option value="" hidden disabled selected>Please Select</option>
        @foreach ($shiftpattern as $singleshiftpattern)
            <option value="{{ $singleshiftpattern->id }}">{{ $singleshiftpattern->id }} - {{ $singleshiftpattern->code }}</option>
        @endforeach
    </select>
</div>

<div class="col-md-3">
    <select class="sapCode" name="inputSapCode" id="inputSapCode" required style="width: 100%">
        <option value="0" disabled="true" selected="true">Please Select</option>
    </select>
</div>

View for second dropdown using jquery script:-

$(document).ready(function() {
    $(document).on('change','.shiftPatternID',function() {
        var cat_id=$(this).val();
        var div=$(this).parent().parent().parent();
        var op=" ";

        $.ajax({
            type: 'get',
            url: '{!! URL::to('findSapCode') !!}',
            data: {
                'id':cat_id
            },
            
            success: function(data) {
                op+='<option value="0" selected disabled>Please Select</option>';
                for (var i=0;i<data.length;i++) {
                    op += '<option value="'+data[i].id+'">'+data[i].code+' - '+data[i].description+'</option>';
                }

                $('.sapCode').html('') ; 
                $('.sapCode').append(op);
            },
            error: function() {

            }
        });
    });
});

Controller:-

public function store(Request $req)
{
    $var_shift_pattern_id = $req ->inputShiftPatternID;
    $var_sap_code = $req ->inputSapCode;

    $usp_var = new UserShiftPattern;
    $usp_var-> shift_pattern_id = $var_shift_pattern_id;
    $usp_var-> sap_code = $var_sap_code;
    $usp_var->save();
    $execute = UserHelper::LogUserAct($req, "User Work Schedule Management", "Create User Work Schedule " .$req->inputUserID);
    $feedback_text = "Successfully created User Work Schedule ".$req->inputUserID.".";
    $feedback_title = "Successfully Created";

    return redirect(route('usp.index', [], false))
        ->with([
            'feedback' => true,
            'feedback_text' => $feedback_text,
            'feedback_title' => $feedback_title
        ]);
}

Routes:-

Route::get('/findSapCode','Admin\UserShiftPatternController@findSapCode');
Route::get('/admin/usershiftpattern', 'Admin\UserShiftPatternController@index')->name('usp.index');
Route::post('/admin/usershiftpattern/add', 'Admin\UserShiftPatternController@store')->name('usp.store');
Route::post('/admin/usershiftpattern', 'Admin\UserShiftPatternController@index')->name('usp.index');
7
  • can you provide blade template code Commented Jun 11, 2021 at 3:29
  • @John Lobo already edit my question Commented Jun 11, 2021 at 4:23
  • can you show db structure as well or try to dd($req->all()) Commented Jun 11, 2021 at 4:28
  • where should i put dd($req->all())? Commented Jun 11, 2021 at 5:01
  • before $var_shift_pattern_id = $req ->inputShiftPatternID; this Commented Jun 11, 2021 at 5:02

1 Answer 1

3

The issue is with option value wrongly passed in jquery.It should be data[i].code instead of data[i].id

op+='<option value="'+data[i].code+'">'+data[i].code+" "+data[i].description+'</option>'

Also you might need to update query as well to show description

$data=ShiftPattern::select('code','id','description')->where('id',$request->id)->take(100)->get();
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.