繁体   English   中英

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

[英]how to select multiple checkbox and save into my database

我在这里有我的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> 

这是我的输出Representative.php的预览。 标签

在此处输入图片说明

更改

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

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

要么

更改

   if ($_POST['submit']){

   if ($_POST['representatives']){

什么也没发生,因为在您的条件下,您正在检查$_POST['submit']是否已设置,如果$_POST上没有这样的索引,怎么$_POST 您刚刚给定了type='submit'value='submit' $_POST的名称或索引在哪里? 只需为缺少的该提交输入元素添加名称。

使用您提供的代码,在PHP代码中,您可以使用$ _POST [“ representatives”]来获取所选复选框的学生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个学生,然后服务器将仅接收从$ _POST [“ representatives”]获取的数组中的c-27327c-7262这2个项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM