2

I am making an auto-complete form, in which user enter only the id and the whole information related to that id comes in other field. I am using ajax and Laravel 5.4. The Data is coming in alert but how i can insert data in input fields of a form. Here is my script:

<script type="text/javascript">
 $('#submitid').on('click', function() {

    var vid = $( "#vid" ).val();
       //var id=$(this).data('id');

        $.ajax({
              url: '/inquiryVendor',
              type: "Get",
              data:{id:$("#vid").val()}, // the value of input having id vid
               success: function(response){ // What to do if we succeed

            alert(response); console.log(response);


        }
            });
    });
</script>

this is my controller:

public function VendorDetail(Request $request)
    {

    $id= $request->id;

      $data = DB::select(DB::raw("SELECT * from bp where id = '$id'"));
      echo json_encode($data);

    }

here is my console.log response:

[{"id":37,"card_name":"Maka Furniture Co.,Ltd ","trade_type":"Manufacturer","address":"Xinzhang development zones,Shengfang town,Hebei province.","fc":"121AC209","status":0,"created_at":"2018-10-10 10:02:27","user":"admin"}]

here is the screenshot of response:

enter image description here

Any help would be highly appreciable.

4 Answers 4

1

If you want all data in single input you can use this

<script type="text/javascript">
     $('#submitid').on('click', function() {

        var vid = $( "#vid" ).val();
           //var id=$(this).data('id');

            $.ajax({
                  url: '/inquiryVendor',
                  type: "Get",
                  dataType: 'json',//this will expect a json response
                  data:{id:$("#vid").val()}, 
                   success: function(response){ 
                        $("#inputfieldid").val(response);     
            }
                });
        });
    </script>

or if you want on different input field you can do like this

<script type="text/javascript">
 $('#submitid').on('click', function() {

    var vid = $( "#vid" ).val();
       //var id=$(this).data('id');

        $.ajax({
              url: '/inquiryVendor',
              type: "Get",
              dataType: 'json',//this will expect a json response
              data:{id:$("#vid").val()}, 
               success: function(response){ 
                    $("#inputfieldid1").val(response.id); 
                    $("#inputfieldid2").val(response.2ndcolumnName);
                    $("#inputfieldid").val(response.3rdcolumnName); 

        }
            });
    });
</script>
Sign up to request clarification or add additional context in comments.

12 Comments

by adding dataType json, How i Will return result in controller. right now i am returning in echo json_encode($data)
I am getting values by using $("#inputfieldid").val(response); but not $("#inputfieldid1").val(response.id);
sir i have given $("#inputfieldid1").val(response.id); this as a example put your column name instead of .id in val(response.id) here
Yes I know I have column name id in my table
provide your json data response screen shot
|
0

In your other fields you can do the following in success method:

success: function(response) { 
  $('.input1').val(response.input1);
  $('.input2').val(response.input2);
  $('.label1').html(response.label1);
}

I hope it would helpful.

Comments

0

add unique ID property to your input like this:

<input id="name" type="text">

Then fill this input with ajax result by this code:

<script type="text/javascript">
 $('#submitid').on('click', function() {

    var vid = $( "#vid" ).val();
       //var id=$(this).data('id');

        $.ajax({
              url: '/inquiryVendor',
              type: "Get",
              data:{id:$("#vid").val()}, // the value of input having id vid
               success: function(response){ // What to do if we succeed
                $('#name').val(JSON.parse(response)[0].name); //<---- This line,

        }
            });
    });
</script>

Do this for all your input base you logic.

Comments

0

Suppose you have input fields like this in your form

<input type="text" name="vendor_name" id="vendor_name">
<input type="text" name="vendor_mobile" id="vendor_mobile">
<input type="text" name="vendor_email" id="vendor_email">

You ajax code should be like this and I hope you get the parameters in your JSON response

<script type="text/javascript">
 $('#submitid').on('click', function() {

 var vid = $( "#vid" ).val();
   //var id=$(this).data('id');

    $.ajax({
          url: '/inquiryVendor',
          type: "Get",
          data:{id:$("#vid").val()}, // the value of input having id vid
           success: function(response){ // What to do if we succeed
            $('#vendor_name').val(response.vendor_name)
            $('#vendor_mobile').val(response.vendor_mobile)
            $('#vendor_email').val(response.vendor_email) 
           }
        });
});
</script>

If this can't help then could you please do console.log(response) and paste the debug data so it will be easy to help you more.

<script type="text/javascript">
 $('#submitid').on('click', function() {

 var vid = $( "#vid" ).val();
   //var id=$(this).data('id');

    $.ajax({
          url: '/inquiryVendor',
          type: "Get",
          data:{id:$("#vid").val()}, // the value of input having id vid
           success: function(response){ // What to do if we succeed
            console.log(response)
           }
        });
});
</script>

if you don't want response[0] then you need to change in your controller File like this.

$vendor = DB::table('bp')->where('id', '.$id.')->first();
return response()->json($vendor);

As per your need, this will help for sure.

4 Comments

HI Vinod I did it but no response. please see the console response. I have edited question
I am getting values by using $("#inputfieldid").val(response); but not $("#inputfieldid1").val(response.id);
Please update your code in ajax success like this it might help you. $('#card_name').val(response[0].card_name)
yes with this it is working. Can you tell me how i can do this without [0]??

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.