【问题标题】:how to select rows from a database and display them with checkboxes?如何从数据库中选择行并用复选框显示它们?
【发布时间】:2014-12-22 19:50:19
【问题描述】:

有一个数据库,其中包含一个 id 问题和 choiecs 以及一个答案(多项选择),我想显示数据库中的所有内容,并在其左侧带有一个复选框。最后,我将有一个提交按钮,所有检查过的问题都将显示在表格下方。我的尝试。必须有一个更简单的方法。谢谢!

    $result = mysqli_query($con,"SELECT id,question,choiceA,choiceB,choiceC,choiceD,answer FROM q_and_a ");

echo "<table border='1'>
<tr>
<th>Add</th>
<th>#</th>
<th>Question</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>Answer</th>
</tr>";

echo '<form  method="POST" action="makeTest.php">'; 

while($row = mysqli_fetch_array($result))
{ 

 echo "<tr>";
echo '<td><input type="checkbox" name="questions[]" value="yes"></td>';
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['question'] . "</td>";
echo "<td>" . $row['choiceA'] . "</td>";
echo "<td>" . $row['choiceB'] . "</td>";
echo "<td>" . $row['choiceC'] . "</td>";
echo "<td>" . $row['choiceD'] . "</td>";
echo "<td>" . $row['answer'] . "</td>";
echo "</tr>";
}
echo "</table>";

?> 


【问题讨论】:

  • 您是否对涉及 jQuery 的解决方案感兴趣?

标签: php html mysql database checkbox


【解决方案1】:

我建议将 jQuery 加入其中。

将 HTML 排除在 PHP 代码之外,只用它来查询数据库。

然后使用 AJAX/JSON 更新 DOM。

更新 - 这已经过测试(验证工作),并在此过程中修复了一些错误。

ajax.php:

<?php

    $action = $_POST["action"];

    if ($action == "getQuestions") {
        echo getQuestions();
    }

    function getQuestions() {
        try {
            $db = new PDO("mysql:host=localhost;charset=utf8", "root", "root");
            $cmd = $db->prepare("
                SELECT id, question , choiceA ,choiceB ,choiceC ,choiceD , answer 
                FROM pl2.questions;
            ");
            $cmd->execute();
            $rows = array();
            while($r = $cmd->fetch(PDO::FETCH_ASSOC)) { $rows[] = $r; }
            $json = json_encode($rows);
            return($json);
        } catch (PDOException $e) { echo $e->getMessage(); return; }
    }

?>

questions.html

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script language="javascript">

        var questionsJson = '';

        $(document).ready(function() {
            getQuestions();
        });

        function getQuestions() {
            $.post('ajax.php', { action: 'getQuestions' }, function(data) { 
                questionsJson = data;
                displayQuestions();
            },'json');
        }

        function displayQuestions() {
            var colAry = ['id', 'question', 'choiceA', 'choiceB', 'choiceC', 'choiceD', 'answer'];
            for (var i = 0; i < questionsJson.length; i++) {
                var q = questionsJson[i];
                var row = '<tr><td><input type="checkbox" name="questions[]" value="yes"></td>';
                for (var j = 0; j < colAry.length; j++) {
                    row += '<td>' + q[colAry[j]] + '</td>';
                }
                $('#mainTable').append(row + '</tr>');
            }
        }

    </script>
</head>
<body>
    <table id="mainTable" border="1">
        <tr>
            <th>Add</th>
            <th>#</th>
            <th>Question</th>
            <th>A</th>
            <th>B</th>
            <th>C</th>
            <th>D</th>
            <th>Answer</th>
        </tr>
    </table>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    相关资源
    最近更新 更多