duanmo6937 2013-01-23 02:53
浏览 56

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

i have here my representative.php codes which the data are called in my database from a javscript function. when i click my submit button it returns me nothing as i echo the checked persons(representatives). only i know is getting values of multiple checkbox in a static form, but this time it really makes me so confuse for this one is a dynamic. and another thing is how could i save the selected values to my database for i still have no idea of how should i save it.. pls...

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

heres the preview of my output representative.php. the tab

enter image description here

  • 写回答

2条回答 默认 最新

  • dongtangu6889 2013-01-23 04:29
    关注

    With the code you provided, in the PHP code, you can use $_POST["representatives"] to get an array of student IDs of the selected checkboxes.

    Example PHP code:

    //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);
    

    From this list of student IDs, you can do queries to update your database. I don't know what operations you want on the database so I can only suggest this much.

    Remember that only selected checkboxes are submited to server. That means for the example you provided in question, the first 2 students are selected, then the server will receive only 2 items which are c-27327 and c-7262 in the array obtain from $_POST["representatives"].

    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源