javascript - 在 Laravel 中通过 Ajax 获取表单中的数据
问题描述
我正在制作一个自动完成表单,其中用户只输入 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>
这是我的控制器:
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"}]
这是响应的屏幕截图:
任何帮助都将是非常可观的。
解决方案
如果您想要单个输入中的所有数据,您可以使用它
<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>
推荐阅读
- automation - 如何在敏捷开发生命周期中适应自动化(系统或 E2E)测试?
- typo3 - 将在线视频添加到“textmedia”元素
- c# - 在 tcp 流关闭之前,TcpClient 不会向 Logstash 发送数据
- sql - 没有第一个表ID的表连接?
- java - 确保将实体添加到休眠搜索索引
- c# - MSI 安装程序(Visual Studio 安装程序项目)未更新使用中的文件
- octobercms - 为使用 relationExtendManageWidget() 添加的字段定义默认值
- javascript - JEE Servlet / JS:document.cookie 在 Chrome 和 IE 之间没有返回相同的东西
- javascript - Chrome 扩展在两个选项卡上工作
- react-admin - 我的所有路由都在资源之前的主机之后包含一个标签,host.com/#/resource,知道我可能做了什么导致这种情况吗?