javascript - 在 Laravel 中通过 Ajax 获取表单中的数据

标签 javascript php jquery ajax laravel

我正在制作一个自动完成表单,用户只输入 ID,与该 ID 相关的所有信息都在其他字段中。我正在使用 ajax 和 Laravel 5.4。数据正在警报中,但我如何在表单的输入字段中插入数据。这是我的脚本:

<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>

这是我的 Controller :

public function VendorDetail(Request $request)
    {

    $id= $request->id;

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

    }

这是我的 console.log 响应:

[{"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"}]

这是响应的屏幕截图:

enter image description here

任何帮助将不胜感激。

最佳答案

如果你想在一个输入中输入所有数据,你可以使用这个

<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>

或者如果你想要不同的输入字段,你可以这样做

<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>

关于javascript - 在 Laravel 中通过 Ajax 获取表单中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53993509/

相关文章:

javascript - JS 脚本 - 值相关(多个下拉列表)

php - 在显示 HTML 之前检测搜索页面的 Ajax 请求何时返回 0 个结果

php - 如何将列从多维数组转换为逗号分隔的字符串?

javascript - 如何根据第一个 <select> 选项填充第二个 <select>

javascript - 滚动期间从底部粘性侧边栏

php fatal error : Cannot use lexical variable $eventName as a parameter name in

javascript - 如何使用 FF 解除/阻止键盘(键 #93)的上下文菜单?

javascript - 基于正则表达式的字符串到数组

javascript - ExtJS:将网格面板添加到表格布局会弄乱表格列宽

javascript - 检查内容是否在 iframe 中?