【发布时间】:2018-03-19 20:50:01
【问题描述】:
我有一个引导模式,如果有要显示的数据行,我会用数据库中的数据填充它。 (名字、姓氏、班级排名)。
我可以在保存数据之前添加和删除行,并且 我希望能够从表中收集数据行并创建一个可以在保存时传递到后端的数据对象。我目前的代码如下:
HTML
<?php foreach ($athletes as $i => $row) { ?>
<?php $i == 0 ? $rownum = 1 : $rownum = $i+1; ?>
<tr id="<?php echo $i; ?>">
<td><?php echo $rownum; ?><input type="hidden" name="data[id]" value="<?php echo $row['id']; ?>" ></td>
<td><input type='text' name="data[first_name]" class="form-control" value="<?php echo $row['first_name']; ?>" ></td>
<td><input type='text' name="data[last_name]" class="form-control" value="<?php echo $row['last_name']; ?>" ></td>
<td><input type='text' name="data[classrank]" class="form-control" value="<?php echo $row['class']; ?>" ></td>
<td><button type="button" id="remScnt" class="btn btn-default">Remove Row</button></td>
</tr>
<?php } } ?>
jQuery 保存函数
$("button#saveaths").click(function() {
var user_id = $("#user_id").val();
var ath_data = [];
$("tr").each(function() {
if (data[first_name] != '') {
ath_data.push(this.data[first_name]);
ath_data.push(this.data[last_name]);
ath_data.push(this.data[classrank]);
ath_data.push(user_id);
}
});
});
我在 if 语句中遇到错误。
在这个阶段,我只是尝试构建可以传递给 codeigniter 控制器以插入到我的数据库中的数据对象。
此外,如果有更好的方法可以做到这一点,我愿意接受建议。
提前致谢!
【问题讨论】:
-
如果你的错误是在 jQuery if 语句上,那么用于生成的 PHP 是无关紧要的。重要的是从 PHP 生成的 HTML 的样子。另外,一个建议是,当你在这个问题中做内联 PHP 时,你可以使用 endforeach 之类的东西 - stackoverflow.com/questions/4600419/endforeach-in-loops
标签: php jquery codeigniter