PHP文件包含从表中获取数据的表中的复选框?

时间:2016-05-05 03:53:03

标签: php html mysql

我希望每个查询都有一个复选框,当表格填充了来自数据库的数据时,还有一个复选框,用于确认每个查询结果的预订。

picture of what I wish to do

我还想知道如何创建一个脚本,一旦选中一个复选框,我点击提交,它会转到另一个显示值(route_no,to_city,from_city和price)的页面,基于所选的复选框和route_no对应于所选的复选框。 route_no是auto_incrementing并且是主键(route_no的值范围为0-49)。

这是我的html表单

<html>
<head>
<script>
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","display.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<form>
<select name="to_city" onchange="showUser(this.value)">
  <option value="">Select a person:</option>
  <option value="Sydney">Sydney</option>
  <option value="Brisbane">Brisbane</option>
  <option value="Adelaide">Adelaide</option>
  <option value="Newcastle">Newcastle</option>
  </select>
</form>
<br>

</body>
</html>

以下是我的php文件,其中包含显示我想要实现的内容的文本。该文本已被注释掉。

<!DOCTYPE html>
<html>
<head>
<style>
table {
    width: 100%;
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid black;
    padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$q = strval($_GET['q']);

$con = mysqli_connect('localhost','root','','mydb');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"flights");
$sql="SELECT * FROM flights WHERE to_city = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Route_no</th>
<th>to_city</th>
<th>from_city</th>
<th>price</th>
<th>Confirm</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['route_no'] . "</td>";
    echo "<td>" . $row['to_city'] . "</td>";
    echo "<td>" . $row['from_city'] . "</td>";
    echo "<td>" . $row['price'] . "</td>";
/// I want a checkbox here for each query that way when the table is populated with data from the db, there is also a checkbox to confirm that booking for each query result.
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

我将编辑php代码的正文部分。试试看

<body>

<?php
$q = strval($_GET['q']);

$con = mysqli_connect('localhost','root','','mydb');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"flights");
$sql="SELECT * FROM flights WHERE to_city = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Route_no</th>
<th>to_city</th>
<th>from_city</th>
<th>price</th>
<th>Confirm</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['route_no'] . "</td>";
    echo "<td>" . $row['to_city'] . "</td>";
    echo "<td>" . $row['from_city'] . "</td>";
    echo "<td>" . $row['price'] . "</td>";
echo "<td><input type=\"checkbox\" name=\"example\" class=\"radio\" value=\"example\"></td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>

这是jQuery部分:

$("input:checkbox").change(function(){
    var group = ":checkbox[name='"+ $(this).attr("name") + "']";
    if($(this).is(':checked')){
       $(group).not($(this)).attr("checked",false);
    }
});

答案 1 :(得分:1)

嗯,你已经在正确的位置注释了:

echo "<form>";
while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['route_no'] . "</td>";
    echo "<td>" . $row['to_city'] . "</td>";
    echo "<td>" . $row['from_city'] . "</td>";
    echo "<td>" . $row['price'] . "</td>";
    echo "<td><input type='checkbox' name='checkbox_id' value='" . $variable_that_has_id . "'> </td>";
    echo "</tr>";
}
echo "</form>";

当然,您必须将$variable_that_has_id替换为与您的数据库中的ID相关联的$row[]

之后,您可以创建一个提交按钮,然后使用该表单将数据发布到同一页面(或不同的页面),您可以使用$_POST变量来获取检查和提交的任何值。形式。

我强烈建议您熟悉在html中创建表单以及使用$ _POST,因为它与您正在做的事情相关。

如果您想让用户只选择一个选项,请将输入类型更改为"radio"