I have a PHP file which displays data from a database. I'm using while() to display each of the records.
My PHP file:
<?php
$flightTicketsSQL = "SELECT * FROM `flightbookings` WHERE username='$user' AND cancelled='no'";
$flightTicketsQuery = $conn->query($flightTicketsSQL);
while($flightTicketsRow = $flightTicketsQuery->fetch_assoc()) { ?>
<tr>
<td class="tableElementTags text-center"><?php echo $flightTicketsRow["bookingID"]; ?></td>
<td class="tableElementTags text-center"><?php echo $flightTicketsRow["origin"]; ?></td>
<td class="tableElementTags text-center"><?php echo $flightTicketsRow["destination"]; ?></td>
<td class="tableElementTags text-center"><?php echo $flightTicketsRow["date"]; ?></td>
<td class="tableElementTags text-center"><?php echo $flightTicketsRow["mode"]; ?></td>
<td class="text-center"><span class="fa fa-remove tableElementTags pullSpan" id="deleteAccount"></span></td>
</tr>
<?php } ?>
If the user clicks on the last <td> (the one with the Font Awesome icon), I want the record with the particular bookingID be deleted from the database.
To do this I'll need to identify the value using jQuery .val()
My JS file:
var id = $("**WHAT DO I PUT HERE ?**").val();
$('#deleteAccount').click(function() {
$.ajax({
type: "POST",
url: 'cancelTicket.php',
data: { bookingID : id },
success : function() {
alert("Cancelled");
}
});
});
My cancel.php file:
<?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);
My question is how do I make jQuery identify which booking the user wants to be cancelled? I mean how do I assign an id to the <td> so that I can retrieve its value in the JS.
I know I could not frame this question properly. SORRY ABOUT THAT.
Thanks
id="deleteAccount"element that's actually being clicked?#deleteAccountelement. 2. Make sure that you're not duplicating thatidon each row. 3. Get theidfrom the button within the click handler using thethisreference to get the clicked element<span class="fa fa-remove tableElementTags pullSpan"></span>. I've not added it. I'll edit the question and add it.tdholds the value which should be sent in the request?on*attributes are an anti pattern which should be avoided. Use an unobtrusive event handler.