javascript - 使用foreach在数据库laravel javascript中双重输入每个输入类型的文本
问题描述
我的数据库中有这个。
人 | 地点
学生1 旧金山
学生 2 旧金山
纽约学生1
学生2纽约
我的控制器有问题。这是代码:
foreach((array)$request->stud as $person){
foreach((array)$request->studlocation as $location){
$hm = new HouseMate;
$hmid = rand();
$hm->hmid=$hmid;
$hm->rId_fk=$renterid;
$hm->person=$person;
$hm->location=$location;
$hm->save();
}
$hm->delete();
}
在我看来:
$(document).ready(function() {
$("#dropdown").change(function() {
var selVal = $(this).val();
$("#textboxDiv").html('');
$("#schoolloc").html('');
if(selVal > 0) {
for(var i = 1; i<= selVal; i++) {
$("#textboxDiv").append('<input type="hidden" value="student'+i+'" name="stud[]" />');
$("#schoolloc").append('<input type="text" name="studlocation[]" id="studlocation'+i+'" />');
var input = document.getElementById('studlocation'+i+'');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.addListener('place_changed', function () {
var place = autocomplete.getPlace();
});
}
}
});
解决方案
这是你想要的吗?
代码 php:
$students = (array)$request->stud;
$locations = (array)$request->studlocation;
for ($i = 0; $i < count($studens); i++){
$hm = new HouseMate;
$hmid = rand();
$hm->hmid=$hmid;
$hm->rId_fk=$renterid;
$hm->person=$students[i];
$hm->location=locations[i];
$hm->save();
$hm->delete();
}
推荐阅读
- javascript - 用户登录后如何在 React 中保存用户信息
- linux - 调用 0x16 时出现分段错误
- javascript - 如何将 javascript 数组发送到 php 文件并将其与数据库进行比较?
- java - Prototype 和 Valued Prototype Vs just Valued Prototype
- machine-learning - 训练集的颜色配置文件在 CNN 的模式识别中是否起作用?
- python - 在pytorch中使用交叉熵损失时我应该使用softmax作为输出吗?
- python-3.x - 在 pandas 数据框中的行之间添加随机数据
- sql - 如何在 SQL 中编写查询当前年度成本和上一年成本?
- jenkins - 清空 etc/default/jenkins 文件。如何更改詹金斯端口?
- css - 如何使字体全高