I am having a problem in which the XMLHttpRequest is not redirecting the user to the URL that I have set when the user clicked on the notification. When I am using MySQLi, the XMLHttpRequest works fine. But, I am required to use PHP PDO instead of MySQLi so I tried to convert the MySQLi code to PHP PDO. The database is updated when I used the PDO code,but the user is not directed to the URL. Plus an alert came out but only says success such as below.
Here is the code for the XMLHttpRequest.
<?php
$sql = "SELECT COUNT(*) AS notificationCount FROM appointment
LEFT JOIN users AS lecturer ON appointment.lecturer_id = lecturer.username
LEFT JOIN course AS course ON appointment.course = course.course_id
WHERE appointment.lecturer_id = :user AND appointment_status='Pending';
";
$notificationURL = '../lecturer/lecturer-view-pending-appointment.php';
$stmt = $db->prepare($sql);
$stmt->execute(array('user' => $user));
$row = $stmt->fetch();
$notificationCount = $row [ 'notificationCount' ];
echo
"
<script type='text/javascript'>
function HandleNotificationsLecturer()
{
// AJAX codes
var xmlhttp
if ( window.XMLHttpRequest )
xmlhttp = new XMLHttpRequest()
else
{
// code for IE6, IE5
xmlhttp = new ActiveXObject ( 'Microsoft.XMLHTTP' )
}
xmlhttp.onreadystatechange = function()
{
if ( this.readyState == 4 && this.status == 200 )
{
if ( xmlhttp.responseText === 'Success' )
{
location.href = '$notificationURL'
}
else
alert ( xmlhttp.responseText )
}
}
xmlhttp.open ( 'POST', '../ajax/ajax-remove-notification-lecturer.php', true )
xmlhttp.setRequestHeader ( 'Content-type', 'application/x-www-form-urlencoded' )
xmlhttp.send ( 'lecturer_id=$user' )
}
</script>
";
?>
Here is the code when using MySQLi:
<?php
$lecturer_id = $_POST['lecturer_id'];
require_once ( '../database.php' );
$connection = OpenDatabase();
QueryDatabase ( $connection, "UPDATE appointment SET appointment_status='Notified' WHERE lecturer_id='$lecturer_id' AND appointment_status='Pending'" );
CloseDatabase ( $connection );
die ( 'Success' );
?>
And here is the code for the PHP PDO:
<?php
require ("../global-include.php");
if (isset($_POST['lecturer_id']))
{
$remove_notification_query = "UPDATE appointment SET appointment_status='Notified' WHERE lecturer_id=:lecturer_id AND appointment_status='Pending'";
$remove_statement= $db->prepare($remove_notification_query);
$remove_statement->bindParam(':lecturer_id', $_POST['lecturer_id']);
$remove_statement->execute();
die ( 'Success' );
}
?>
The database connection for MySQLi and for the PDO is different as well. Here is the database connection for MySQLi:
<?php
function OpenDatabase()
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpassword = '';
$database = 'stulec';
// open a connection to the database server
$connection = new mysqli ( $dbhost, $dbuser, $dbpassword, $database );
if ( $connection->connect_errno )
{
die ( 'Connect failed: ' . $connection->connect_error );
}
return $connection;
}
function QueryDatabase ( $connection, $query )
{
$query = str_replace ( '"', '`', $query );
$result = $connection->query ( $query );
if ( !$result )
{
die ( 'Error in query: ' . $connection->error );
}
return $result;
}
function GetNumRows ( $result )
{
return $result->num_rows;
}
function ReadField ( $result, $row, $field )
{
$result->data_seek ( $row );
$row = $result->fetch_assoc();
return $row [ $field ];
}
function GetLastInsertedID ( $connection )
{
return $connection->insert_id;
}
function CloseDatabase ( $connection )
{
$connection->close();
}
?>
And here is the database connection for the PDO:
<?php
class PDOConnection
{
private static $dbConnection = null; //static variables - doesn't change, when update, update for everyone
/**
*Return DB connection or create initial connection
*@return object (PDO connection)
*@access public
*/
public static function getConnection()
{
//create connection
if (!self::$dbConnection)
{
try
{
self::$dbConnection = new PDO ('mysql:host=localhost; dbname=stulec','root','');
self::$dbConnection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
return self::$dbConnection; //return the connection
}
} //end of class
?>
I have inspected and checked my Network response but nothing came out as well.