(edited after initial version)
I'm facing an issue where my AJAX call is returning the header.php page instead of just the $result value which should be 0 or 1. The AJAX function calls generateTicket.php, where I want to generate tickets only if no other ticket exists. If the generation is successful, it should return 1, otherwise 0.
However, currently it's returning the header.php along with the response (0 or 1) at the end. How can I prevent it from returning header.php?
AJAX:
function generateTicket() {
$.ajax({
url: 'generateTicket.php',
type: 'post',
datatype: "json",
data: {
showid: <?php echo $showid ?>,
seats: <?php echo $seats_json ?>
},
success: function(response) {
alert(response);
}
});
}
generateTicket.php:
<?php
include('includes/header.php');
//Login check
if (!$loggedIn) {
header('Location: login.php');
die();
}
//Variable initialization
$userid = $_SESSION['userid'];
$showid = $_POST['showid'];
$seats = $_POST['seats'];
$result = 1;
//Generate a ticket for each seat
foreach($seats as $seat){
//Load ticket from database
$query = "SELECT * FROM ticket_uebersicht WHERE Sitzplatz LIKE ? AND VorstellungID = ? LIMIT 1";
$statement = $pdo->prepare($query);
$statement->execute(array($seat, $showid));
$row = $statement->fetch();
//Check if ticket exists
if(!$row){
$code = guidv4();
$query = "INSERT INTO tickets (code, UserID, VorstellungID, Sitzplatz) VALUES (?,?,?,?)";
$statement = $pdo->prepare($query);
$statement->execute(array($code, $userid, $showid, $seat));
continue;
}else{
if($row['marktplatz'] != 0){
//Adjust existing ticket
$code = guidv4();
$marketplace = '0';
$query = "UPDATE tickets SET UserID = ?, code = ?, marktplatz = ? WHERE Sitzplatz = ? AND VorstellungID = ? AND NOT marktplatz = 0 LIMIT 1";
$statement = $pdo->prepare($query);
$statement->execute(array($userid, $code, $marketplace, $seat, $showid));
//Process refund
if(!($row['UserID'] == -1)){
$betrag = $row['preis'] - 2;
$query = "INSERT INTO rueckerstattung (UserID, Betrag) VALUES (?,?)";
$statement = $pdo->prepare($query);
$statement->execute(array($row['UserID'], $betrag));
}
continue;
}
$result = 0; //Error
}
}
echo json_encode($result);
//Generate Ticket ID (GUID)
function guidv4($data = null) {
// Generate random data
$data = $data ?? random_bytes(16);
assert(strlen($data) == 16);
// Set version and variant bits
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
// Format UUID
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
exit;
?>
header.php:
<?php
include('db_connect.php');
include('righthandler.php')
?>
<!-- Header -->
<!DOCTYPE html>
<html lang="de">
<head>
... Head content goes here ...
</head>
<!-- Website Header with Navigation Bar-->
<!-- Navbar Code goes here -->
AJAX success response: (my index.php)
<!DOCTYPE html>
<html lang="de">
<head>
... This section contains the head content ...
</head>
<!-- Website Header with modified Navigation Bar and success message. -->
<!-- Updated navbar info displayed here. -->
1