Novice_29 Novice_29 - 7 months ago 15
PHP Question

PHP file containing checkboxes in a table that fetches data from a database?

I want a checkbox 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.

picture of what I wish to do

I also want to know how to create a script that once a single checkbox has been selected,I click on submit and it takes me to another page that displays values (route_no,to_city,from_city and price) based on the checkbox selected and route_no corresponding to that checkbox selected. route_no is auto_incrementing and is a primary key(values of route_no range from 0-49).

this is my html form

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


and below is my php file, which has text that shows what I want to acheive. The text has been commented out.

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

Answer

I'll edit the body section of your php code. Try and see

<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\" value=\"example\"></td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>