dsa122870 2019-04-16 14:25
浏览 103

在用户单击通知时执行PHP PDO查询后,XMLHttpRequest不会重定向到URL

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.

enter image description here

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.

  • 写回答

1条回答 默认 最新

  • dongzhi8984 2019-04-16 15:41
    关注

    UPDATE:

    I finally got an answer from a friend's help. He told me that it is because of the die('Success'); The die('Success') got unnecessary spaces. So he advised to use

    if ( xmlhttp.responseText.indexOf('Success') >= 0)
    

    instead of

    if ( xmlhttp.responseText == 'Success' )
    

    indexof will check if the string contains the word Success regardless of anything else.

    Credit to that friend and anyone else who is thinking on how to help me.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?