簡體   English   中英

如何從數據庫中選擇行並使用復選框顯示它們?

[英]how to select rows from a database and display them with checkboxes?

有一個具有id問題和選擇以及一個答案(多項選擇)的數據庫,我想顯示數據庫中的所有內容,並在其左側有一個復選框。 最后,我將有一個提交按鈕,所有要檢查的問題都想顯示在表格下。 我的嘗試。 必須有一個更簡單的方法。 謝謝!

    $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加入其中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM