如何选择多个复选框并保存到我的数据库中

时间:2013-01-23 02:53:08

标签: php javascript mysql mysqli

我在这里有我的representative.php代码,这些代码是从javscript函数在我的数据库中调用的。当我点击我的提交按钮时,它回复了我,因为我回复了被检查的人(代表)。只有我知道的是以静态形式获取多个复选框的值,但这一次它真的让我如此迷惑这个是动态的。另一件事是我如何将选定的值保存到我的数据库中,因为我仍然不知道应该如何保存它...请...

<?php session_start(); ?>
<?php

// server info
$server = 'localhost';
$user = 'root';
$pass = 'root';
$db = 'user';
// connect to the database
$mysqli = new mysqli($server, $user, $pass, $db);
// show errors (remove this line if on a live site)
mysqli_report(MYSQLI_REPORT_ERROR);
?>
<?php
if ($_POST['submit']){
    $check = $_POST['representatives'];
    foreach ($check as $ch){
        echo $ch."<br>";
    }

} ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
    <head>  
        <script type="text/javascript">
        function get_representatives_value()
        {
            for (var i=0; i < document.list.representatives.length; i++)

            {
                if (document.list.representatives[i].checked = true)
                {
                    var candidate = document.list.representatives[i].checked;

                }
                document.president.radio[i].value;
            }
        }
        }
        </script>
        <title></title>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link href="candidate.css" rel="stylesheet" type="text/css">
    </head>
    <body>
        <form name="list" action="president2.php" method="post"  onClick="get_president_value">
            <div id="form"> 
            <?php
            // get the records from the database
            if ($result = $mysqli->query("SELECT * FROM candidate_info WHERE position= 'representatives' ORDER BY cand_id"))
            {
            // display records if there are records to display
                if ($result->num_rows > 0)
                {
                    // display records in a table
                    echo "<table border='1' cellpadding='10'>";
                    // set table headers

                    echo "<tr><th>Student ID</th><th>Candidate ID</td><th>Course</th><th colspan = '3'>Name</th></tr>";
                    while ($row = $result->fetch_object())
                    {
                        // set up a row for each record
                        echo "<tr>";
                        echo "<td>" . $row->cand_studid . "</td>";
                        echo "<td>".$row->cand_id."</td>"; 
                        echo "<td>" . $row->course . "</td>";
                        echo "<td coslpan ='5'>" . $row->fname . " ". $row->mname ." ". $row->lname ." </td>";
                        echo "<td><input type ='checkbox' name='representatives[]' id='". $row->studid ."' value='" . $row->fname . " ". $row->mname ." ". $row->lname .  "'onchange='get_representatives_value()' /></td>";
                        echo "</tr>";
                    }
                    cho "</table>";
                }
                // if there are no records in the database, display an alert message
                else
                {
                    echo "No results to display!";
                }
            }
            // show an error if there is an issue with the database query
            else
            {
                echo "Error: " . $mysqli->error;
            }

            // close database connection
            $mysqli->close();
            echo "<input type='submit' value='Submit'/>";
            ?>
            </div>
        </form>
    </body>
</html> 

继承了我的输出代表的预览.php。标签

enter image description here

2 个答案:

答案 0 :(得分:2)

更改

    echo "<input type='submit' value='Submit'/>";

    echo "<input type='submit' name='submit' value='Submit'/>";

OR

更改

   if ($_POST['submit']){

   if ($_POST['representatives']){

没有任何事情发生,因为根据您的条件,您正在检查是否设置了$_POST['submit'],如果$_POST上没有此类索引,这怎么可能是真的。您刚刚提供了type='submit'value='submit'$_POST的名称或索引在哪里。只需添加缺少的提交输入元素的名称。

答案 1 :(得分:0)

使用您提供的代码,在PHP代码中,您可以使用$ _POST [“represent”]来获取所选复选框的学生ID数组。

示例PHP代码:

//dump out to see the student IDs of selected checkboxes
var_dump($_POST["representatives"]);

//print out the list of student IDs of selected checkboxes
foreach ($_POST["representatives"] as $studid)
  echo $studid.chr(32);

从此学生ID列表中,您可以执行查询以更新数据库。我不知道你想在数据库上做什么操作,所以我只能建议这么多。

请记住,只有选定的复选框才会提交给服务器。这意味着对于您提供的示例,前两个学生被选中,那么服务器将只收到2个 c-27327 c-7262 的项目。数组从$ _POST [“代表”]获得。