【问题标题】:how to select multiple checkbox and save into my database如何选择多个复选框并保存到我的数据库中
【发布时间】:2013-01-06 10:59:21
【问题描述】:

我在这里有我的代表.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 的预览。标签

【问题讨论】:

  • 能否请您添加presidents2.php的代码以及onClick总统值功能代码?我有类似的问题,我认为您的解决方案可能对代码有所帮助!

标签: php javascript mysql mysqli


【解决方案1】:

改变

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

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

改变

   if ($_POST['submit']){

   if ($_POST['representatives']){

什么都没有发生,因为根据您的情况,您正在检查是否设置了 $_POST['submit'],当您的 $_POST 上没有此类索引时,这怎么可能是真的。您刚刚给出了type='submit'value='submit'$_POST 的名称或索引在哪里。只需添加缺少的提交输入元素的名称即可。

【讨论】:

    【解决方案2】:

    使用您提供的代码,在 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 个学生,然后服务器将仅收到 2 个项目,即 c-27327c-7262 在数组从 $_POST["representatives"] 获取。

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 2018-05-08
      • 2015-10-25
      相关资源
      最近更新 更多