Reputation: 47
A possible duplicate of this link
But I can't seem to solve this issue with the same logic from that forum.
I'm working on a booking module and it has a function whether to confirm or cancel a booking. Here is my PHP
code that tells if booking is confirmed, cancelled or not yet confirmed. Status==0
(Not Confirmed yet) - default one, Status==1
(Confirmed) and Status==2
(Cancelled)
<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0)
{
header('location:index.php');
}
else{
if(isset($_REQUEST['eid']))
{
$eid=intval($_GET['eid']);
$status="2";
$sql = "UPDATE tblbooking SET Status=:status WHERE id=:eid";
$query = $dbh->prepare($sql);
$query -> bindParam(':status',$status, PDO::PARAM_STR);
$query-> bindParam(':eid',$eid, PDO::PARAM_STR);
$query -> execute();
$msg="Booking Successfully Cancelled";
}
if(isset($_REQUEST['aeid']))
{
$aeid=intval($_GET['aeid']);
$status=1;
$sql = "UPDATE tblbooking SET Status=:status WHERE id=:aeid";
$query = $dbh->prepare($sql);
$query -> bindParam(':status',$status, PDO::PARAM_STR);
$query-> bindParam(':aeid',$aeid, PDO::PARAM_STR);
$query -> execute();
$msg="Booking Successfully Confirmed";
}
<?php $sql = "SELECT tblbooking.Status";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0){
foreach($results as $result){
?>
if($result->Status==0){
echo htmlentities('Not Confirmed yet');
} else if ($result->Status==1) {
echo htmlentities('Confirmed');
}
else{
echo htmlentities('Cancelled');
}
And this is the ORIGINAL code that will confirm/cancel the booking with basic Javascript
alert
<td><button class="btn btn-sm btn-primary"><a href="manage-bookings.php?aeid=<?php echo htmlentities($result->id);?>" onclick="return confirm('Do you really want to Confirm this booking')"> <i style="color: #fff;" class='fa fa-check'></i></a></button>
<button class="btn btn-sm btn-danger"><a href="manage-bookings.php?eid=<?php echo htmlentities($result->id);?>" onclick="return confirm('Do you really want to Cancel this booking')"> <i style="color: #fff;" class='fa fa-close'></i></a></button></td>
But I want to use sweetalert so I modified it to this (testing it first with just anchor)
<a href="manage-bookings.php?aeid=<?php echo htmlentities($result->id);?>" onclick="confirmation(event)">Confirm</a>
And this is the script
<script>
function confirmation(ev) {
ev.preventDefault();
var urlToRedirect = ev.currentTarget.getAttribute('href'); //use currentTarget because the click may be on the nested i tag and not a tag causing the href to be empty
console.log(urlToRedirect); // verify if this is the right URL
swal({
title: "Are you sure you want to tag this booking as confirmed",
text: "You will not be able to revert this!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
// redirect with javascript here as per your logic after showing the alert using the urlToRedirect value
if (willDelete) {
swal("Your booking as been confirmed!", {
icon: "success",
});
} else {
swal("You booking hasn't been confirmed.");
}
});
}
</script>
The problem is, the alert works. It creates a pop up confirmation but it doesn't confirm/cancel the booking at all although the
href
links says the same thing with the original but the original reloads the page and the tagging works.
Upvotes: 0
Views: 2054
Reputation: 326
It looks to me like you are never actually sending any data back to the server.
The built in javascript confirm dialog interrupts your hyperlink and then allows it to resume after you hit OK.
It appears that Sweetalerts don't do the same. So you are never redirecting to the page you need to get to. You could use javascript to redirect to the correct URL or, you could write a server-side API for the action you are trying to take, and call that API from AJAX inside of the 'willDelete' case.
Here's the former option:
<script>
function confirmation(ev) {
ev.preventDefault();
var urlToRedirect = ev.currentTarget.getAttribute('href'); //use currentTarget because the click may be on the nested i tag and not a tag causing the href to be empty
console.log(urlToRedirect); // verify if this is the right URL
swal({
title: "Are you sure you want to tag this booking as confirmed",
text: "You will not be able to revert this!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
// redirect with javascript here as per your logic after showing the alert using the urlToRedirect value
window.location.href = urlToRedirect;
} else {
swal("You booking hasn't been confirmed.");
}
});
}
</script>
Upvotes: 2