AJAX readyState为空

时间:2015-04-16 22:37:51

标签: javascript php ajax mysqli

我正在尝试将变量传递回xmlhttp.responseText。我有两个独立的功能来执行此操作。第一个是在页面加载并成功时调用的。如果我在AJAX函数中插入一个警告来检查readyState没有警告弹出(但它将与第一个一起),那么第二个调用点击并在PHP端正确执行(它在我的MYSQL数据库中进行更改) 。它也永远不会在xmlhttp.responseText中接收从PHP返回的变量。

即使PHP端的函数正在执行,有什么可以使readyState第二次为空?

PHP:

<?php

include('connect.php');

$salt = "*************";

$perform = $_POST[0];
$place = $_POST[1];
$user = $_POST[2];
$usercrypt = crypt(md5($user),$salt);
$placeid = trim($place,",");

function checkNow()
{
    global $usercrypt;
    global $place;
    global $conn;
    $urow = mysqli_query($conn, "SELECT Users.User FROM Users WHERE Users.User='" . $usercrypt . "';");
    if (mysqli_num_rows($urow) > 0)
        {
            $favcheck = mysqli_query($conn, "SELECT Users.Favorites FROM Users WHERE Users.User='" . $usercrypt . "';");
            $favcheck = mysqli_fetch_array($favcheck);
            $favcheck = $favcheck[0];
            if (strpos($favcheck, $place) !== false)
                {
                    $answer = 'found';
                }
            else
                {
                    $answer = 'notfound';
                }
        }
    else
        {
            $answer = 'nouser';
        }
    return array($answer,$favcheck);
    unset($answer);
}

if ($perform == "0")
    {
        $sendback = checkNow();
        echo $sendback[0];
        unset($sendback);
    }

if ($perform == "1")
    {
        global $usercrypt;
        global $conn;
        mysqli_query($conn, "INSERT INTO Users (User) VALUES ('" . $usercrypt . "')");
    }

if ($perform == "2")
    {
        $sendback = checkNow();
        global $place;
        global $placeid;
        global $usercrypt;
        global $conn;
        $currentnum = mysqli_query($conn, "SELECT Places.Favorites FROM Places WHERE Places.PlaceID=" . $placeid);
        $currentnum = mysqli_fetch_array($currentnum);
        $currentnum = $currentnum[0];
        if ($sendback[0] == 'found')
            {
                $currentnum--;
                $change = str_replace($place,'',$sendback[1]);
                mysqli_query($conn, "UPDATE Users SET Favorites='" . $change . "' WHERE User = '" . $usercrypt . "'");
                mysqli_query($conn, "UPDATE Places SET Places.Favorites=" . $currentnum . " WHERE Places.PlaceID =" . $placeid);
                $answer = 'subtracted';
            }
        if ($sendback[0] == 'notfound')
            {
                $currentnum++;
                $change = $sendback[1] . $place;
                mysqli_query($conn, "UPDATE Users SET Favorites='" . $change . "' WHERE User = '" . $usercrypt . "'");
                mysqli_query($conn, "UPDATE Places SET Places.Favorites=" . $currentnum . " WHERE Places.PlaceID =" . $placeid);
                $answer = 'added';
            }
        return $answer;
        unset($answer);
    }

unset($_POST);

?>

JavaScript的:

function ajaxSession()
{
    xmlhttp = undefined;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            z = xmlhttp.responseText;
        }
    }
}

function stateCheck()
{
    ajaxSession();
    xmlhttp.open('POST', thisurl + '/favoritecheck.php',true);
    xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    xmlhttp.send("0=" + perform + "&1=" + thisplace + ",&2=" + thisusername);
}

function firstCheck()
{
    perform = 0;
    stateCheck();
    if (z == 'found')
    {
        document.getElementById("favorite").src="http://www.****************.com/images/favorite-on.png";
        document.getElementById("favtext").innerHTML="This is a favorite!";
    }
    if ( z == 'nouser')
    {
        perform = 1;
        stateCheck();
    }
}

function heartCheck()
{
    perform = 2;
    stateCheck();
    if (z == 'added')
    {
        document.getElementById("favorite").src="http://www.****************.com/images/favorite-on.png";
        document.getElementById("favtext").innerHTML="This is a favorite!";
    }
    if (z == 'subtracted')
    {
        document.getElementById("favorite").src="http://www.****************.com/images/favorite-off.png";
        document.getElementById("favtext").innerHTML="Add to your favorites.";
    }
}



if (loggedin == 1)
{
document.writeln('<img id="favorite" style="cursor: pointer;" onclick="heartCheck()" src="http://www.****************.com/images/favorite-off.png" alt="Favorite" />'
+ '<br />'
+ '<div id="favtext" style="color: #D20425;">'
+ 'Add to your favorites.'
+ '</div>');
firstCheck();
} else if (loggedin == 0)
{
document.writeln('<img id="favorite" style="cursor: pointer;" src="http://www.****************.com/images/favorite-off.png" alt="Favorite" />'
+ '<br />'
+ '<div id="favtext" style="color: #D20425;">'
+ '<a style="color: #800000; font-weight: bold;" href="' + thisurl + '/wp-login.php">Log in</a> to add favorites.'
+ '</div>');
}

0 个答案:

没有答案