Siby Xavier February 2016

how to access form data using post in loop

I am trying to access data from form field which is dynamically created and store it to db.I don't know how to do that.I tried many ways but didn't worked.please help me.Thank you for reading this.....

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

Answers


Pupil February 2016

You need to create array of checkboxes and then loop over it in $_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>";
    }?>

In PHP posted file,

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

Also, never user mysql_* functions are they are deprecated and will be removed in PHP's further releases.


Gaurav Rai February 2016

Make checkbox an 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>

Post Status

Asked in February 2016
Viewed 2,748 times
Voted 11
Answered 2 times

Search




Leave an answer