【问题标题】:How to get the selected option from a html <select> & insert into database?如何从 html <select> 中获取选定的选项并插入数据库?
【发布时间】:2014-12-16 07:29:53
【问题描述】:

我制作了一个 html 页面,其中显示来自另一个数据库的数据,并且我添加了额外的“状态”字段,该字段对数据库中的每个学生都有两个选择选项。我的问题是如何从每个学生字段中获取选定的 ['Present' 或 'Absent'] 数据并将其插入数据库或将其放入变量中

我的代码在这里

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

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

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

【问题讨论】:

    标签: html mysql forms select


    【解决方案1】:

    &lt;option&gt; 的 HTML 代码不正确。而不是其中的name,使用value

    阅读:&lt;option&gt; | MDN

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

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

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

    【讨论】:

      【解决方案2】:
      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
      }
      

      【讨论】:

      • 什么是 $student_id 和 $status_val。它是 student_id=$list['stu_id'] 以及 $status_val 存储什么价值
      • status val 是学生的状态值,从选择框出现或缺席
      • 我是如何得到这个值的。如果你不介意你能提供代码吗。拜托它会对我有很大帮助
      • 您是否使用表单来保存详细信息?如果是,则必须将此代码放入表单操作页面中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多