php - 如何使用post in循环访问表单数据

标签 php mysql forms

我正在尝试从动态创建的表单字段访问数据并将其存储到数据库中。我不知道该怎么做。我尝试了很多方法但没有奏效。请帮助我。谢谢阅读这……

    <html lang="en">
    <head>
    <title>toggle</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     <?php
    $link = mysql_connect('localhost', 'root', '');
    if (!$link) {
        die('Could not connect to MySQL server: ' . mysql_error());
    }
    $dbname = 'attendance';
    $db_selected = mysql_select_db($dbname, $link);
    if (!$db_selected) {
        die("Could not set $dbname: " . mysql_error());
    }
    $res = mysql_query('select * from student', $link);
    if(isset($_POST["submit"]))
    {
        $name=$_POST['student'];
        echo $name;
    }
    ?>
    <script type="text/javascript">
    function change(obj) {
    var tr=obj.parentNode; // this may change depending on the html used
    tr.style.backgroundColor=(obj.checked)? 'green' : 'red';
    }
    </script>
    <style type="text/css">
    .nochange, tr {background-color:green;} 
    </style>
    </head>
    <body onLoad="change(this)">
    Report Attendance
    <table border="1" cellspacing="2" cellpadding="5" summary="">
    <form name="myform" action="" method="post">
    <?php while ($row = mysql_fetch_assoc($res)){
      echo "<tr class='nochange'><td><input type='checkbox' name='student' value='" . $row['stu_id'] . "' checked style='background:#f00;' onClick='change(this);'>" . $row['stu_name'] . "<br />"."</td></tr>";
    }?>
    <input type="submit" name="submit" value="submit"/>
    </form>
    </table>
    </body>
    </html>

最佳答案

复选框设为数组

<html lang="en">
<head>
<title>toggle</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Could not connect to MySQL server: ' . mysql_error());
}
$dbname = 'attendance';
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
    die("Could not set $dbname: " . mysql_error());
}
$res = mysql_query('select * from student', $link);
if(isset($_POST["submit"]))
{
    //Here goes array
    for($i=0;$i<count($_POST['student']);$i++)
    {
        $name=$_POST['student'][$i];
        echo $name;
    }
}
?>
<script type="text/javascript">
function change(obj) {
var tr=obj.parentNode; // this may change depending on the html used
tr.style.backgroundColor=(obj.checked)? 'green' : 'red';
}
</script>
<style type="text/css">
.nochange, tr {background-color:green;} 
</style>
</head>
<body onLoad="change(this)">
Report Attendance
<table border="1" cellspacing="2" cellpadding="5" summary="">
<form name="myform" action="" method="post">
<?php while ($row = mysql_fetch_assoc($res)){
  echo "<tr class='nochange'><td><input type='checkbox' name='student[]' value='" . $row['stu_id'] . "' checked style='background:#f00;' onClick='change(this);'>" . $row['stu_name'] . "<br />"."</td></tr>";
}?>
<input type="submit" name="submit" value="submit"/>
</form>
</table>
</body>
</html>

关于php - 如何使用post in循环访问表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35266516/

相关文章:

java - 如何以可扩展的方式仅将唯一记录添加到数据库中

html - 如何在透明 `<label>`后面对齐 `&lt;input&gt;`

forms - 带有选择输入和带有生成器的实体标签的 Rails Scaffold 引用

jquery - 我如何在 django 中编写自己的自定义表单小部件

php - 创建允许用户注册并使用同一引擎创建更多内容的动态站点

php - HTTP 请求的 keep_alive 和 persistent 选项有什么区别?

mysql - 无法在具有外部卷的kubernetes中运行mysql pod

php - SQL 数据库中可变列数

php - MySQL查询不包含返回今天购买的商品

mysql - ORDER BY FIELD 子句在具有多个 UNION clouse 的复杂 mysql 查询末尾