如何使用post in loop

时间:2016-02-08 09:48:29

标签: php mysql forms

我正在尝试从动态创建的表单字段访问数据并将其存储到db.I不知道该怎么做。我尝试了很多方法但没有工作。请帮助我。谢谢你阅读此.....

    <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>

2 个答案:

答案 0 :(得分:0)

您需要创建一系列复选框,然后在$_POST中循环显示它。

HTML:

<?php while ($row = mysql_fetch_assoc($res)){
      echo "<tr class='nochange'><td><input type='checkbox' name='student[".$row['stu_id']>"]' value='" . $row['stu_id'] . "' checked style='background:#f00;' onClick='change(this);'>" . $row['stu_name'] . "<br />"."</td></tr>";
    }?>

在PHP发布的文件中,

if (! empty($_POST['student'])) {
 foreach ($_POST['student'] as $stu_id -> $student) {
  echo '<br/> Student Id: ' . $stu_id;
 }
}

此外,永远不会弃用用户 mysql _ * 函数,并且将在PHP的其他版本中将其删除。

答案 1 :(得分:0)

checkbox成为array

<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>