samedi 10 novembre 2018

how to autocomplete form fields with ajax laravel

I trying to autofill my form with cnic which i set as unique. If Cnic exists then all fields against the entered cnic with autofill. how i will do that? I have uploaded my code jquery and controller. If you need more data to understand you can ask.

    $("#cnic").focusout(function(e){ 
        // alert($(this).val());
        var cnic = $(this).val();
        $.ajax({
            type: "POST",
            url: "",
            data: {'cnic':cnic},
            dataType: 'json',
            success : function(e) {
                if(e===0){
                    $('.flash-message').html('Data not found');
                    $('#cnic').val('');
                }
                else {
                    $('.flash-message').html('');
                    r = $.parseJSON(e); //convert json to array
                    $('#name').val(r.name); //assign name value
                    $('#mobile_number').val(r.mobile); //assign email value
                    $('#party_joining_year').val(r.party_joining_year); //assign department value
                    $('#qualification').val(r.qualification); //assign department value
                    $('#party_position').val(r.party_position); //assign department value
                    $('#profession').val(r.profession); //assign department value
                    $('#district').val(r.district); //assign department value
                }
            }
        });
    });

</script>

My Controller:

 public function getAllFields(Request $request)
{
    $getFields = Member::where('cnic', $request->get('cnic'))->get(['name','mobile','party_joining_year','qualification','party_position','profession','district']);
    return json_encode($getFields);
}

Route:

  Route::post('/get_fields', 'MemberController@getAllFields')>name('get.all.fields');



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire