【发布时间】:2014-12-16 07:29:53
【问题描述】:
我制作了一个 html 页面,其中显示来自另一个数据库的数据,并且我添加了额外的“状态”字段,该字段对数据库中的每个学生都有两个选择选项。我的问题是如何从每个学生字段中获取选定的 ['Present' 或 'Absent'] 数据并将其插入数据库或将其放入变量中
我的代码在这里
http://s11.postimg.org/7s09i58pf/Untitled.png
这是我的数据库结构和 html 页面
【问题讨论】:
我制作了一个 html 页面,其中显示来自另一个数据库的数据,并且我添加了额外的“状态”字段,该字段对数据库中的每个学生都有两个选择选项。我的问题是如何从每个学生字段中获取选定的 ['Present' 或 'Absent'] 数据并将其插入数据库或将其放入变量中
我的代码在这里
http://s11.postimg.org/7s09i58pf/Untitled.png
这是我的数据库结构和 html 页面
【问题讨论】:
<option> 的 HTML 代码不正确。而不是其中的name,使用value。
要获取所选选项的值,您必须使用 Javascript:
var e = document.querySelector('select[name=status]')
var strUser = e.options[e.selectedIndex].value;
然后,使用 AJAX 将此值发送到您的 PHP,然后使用 SQL 将其添加到您的数据库中。
【讨论】:
change the select to capture array
<select name="status[<?php echo $list['stu_id']; ?>]">
instead of
<select name="status">
in the while loop
on submit, in the backend you will receive status as an array with index as student id
$statuses=$_POST['status']; // this will give the array of status val of all the students
foreach($statuses as $student_id=>$status_val){
// update the status for each student based on student id and status val
}
【讨论】: