我有一个 PHP文件,显示数据库中的数据.我正在使用while()来显示每条记录.
我的PHP文件:
$flightTicketsSQL = "SELECT * FROM `flightbookings` WHERE username='$user' AND cancelled='no'";
$flightTicketsQuery = $conn->query($flightTicketsSQL);
while($flightTicketsRow = $flightTicketsQuery->fetch_assoc()) { ?>
<?php echo $flightTicketsRow["bookingID"]; ?><?php echo $flightTicketsRow["origin"]; ?><?php echo $flightTicketsRow["destination"]; ?><?php echo $flightTicketsRow["date"]; ?><?php echo $flightTicketsRow["mode"]; ?>如果用户点击最后一个< td> (带有Font Awesome图标的那个),我希望从数据库中删除具有特定bookingID的记录.
为此,我需要使用jQuery .val()来识别值.
我的JS档案:
var id = $("**WHAT DO I PUT HERE ?**").val();
$('#deleteAccount').click(function() {
$.ajax({
type: "POST",
url: 'cancelTicket.php',
data: { bookingID : id },
success : function() {
alert("Cancelled");
}
});
});
我的cancel.php文件:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user = $_SESSION["username"];
$id = $_POST["bookingID"];
$cancelFlightBookingsSQL = "UPDATE `flightbookings` SET cancelled='yes' WHERE bookingID='$id'";
$cancelFlightBookingsQuery = $conn->query($cancelFlightBookingsSQL);
我的问题是如何让jQuery识别用户想要取消的预订?我的意思是如何为< td>分配ID这样我就可以在JS中检索它的值.
我知道我无法正确地构建这个问题.对于那个很抱歉.
谢谢