如何从html中选择选项<select>&amp;插入数据库?</select>

时间:2014-12-16 07:29:54

标签: html mysql forms select

我创建了一个html页面,其中数据显示在另一个数据库中,并且我添加了额外的“Status”字段,该字段对数据库中的每个学生都有两个选择选项。我的问题是如何从每个学生字段中获取所选['Present'或'Absent']数据并插入数据库或将其转换为变量

我的代码在这里

http://s11.postimg.org/7s09i58pf/Untitled.png

这是我的数据库结构和html页面

http://s1.postimg.org/glc705uzj/image.png

2 个答案:

答案 0 :(得分:0)

<option>的HTML代码不正确。而不是name,请使用value

读取:<option> | MDN

要获取所选选项的值,您必须使用Javascript:

var e = document.querySelector('select[name=status]')
var strUser = e.options[e.selectedIndex].value;

然后,使用AJAX将此值发送到PHP,然后使用SQL将其添加到数据库中。

答案 1 :(得分:0)

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
}
相关问题