我有被調用一次在頁面加載和點擊後再次AJAX的會話。它沒有第二次做好現成的改變,所以我決定通過在函數中放置一個警告框來測試它。警告框顯示在頁面加載。但是點擊它不會顯示出來 - 即使PHP端執行,也可以得到它。更令人費解的是,如果我嘗試測量就緒狀態,它永遠不會出現(不是0,1,2,3或4 - 警告框甚至不會顯示),並且我沒有收到回覆文本。AJAX會話調用兩次。它並不顯示一個警告,第二次
我最終想實現是像它的頁面加載xmlhttp.responseText返回一個值。我錯過了什麼?
的JavaScript:
function ajaxSession()
{
alert();
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)
{
\t z = xmlhttp.responseText;
}
}
}
function stateCheck()
{
ajaxSession();
xmlhttp.open('POST', thisurl + '/favoritecheck.php',false);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttp.send("0=" + perform + "&1=" + thisplace + ",&2=" + thisusername);
}
function firstCheck()
{
perform = 0;
stateCheck();
\t if (z == 'found')
\t {
\t document.getElementById("favorite").src="http://www.********.com/images/favorite-on.png";
\t document.getElementById("favtext").innerHTML="This is a favorite!";
\t }
\t if (z == 'nouser')
\t {
\t perform = 1;
\t stateCheck();
\t }
}
function heartCheck()
{
perform = 2;
stateCheck();
\t if (z == 'added')
\t {
\t document.getElementById("favorite").src="http://www.********.com/images/favorite-on.png";
\t document.getElementById("favtext").innerHTML="This is a favorite!";
\t }
\t if (z == 'subtracted')
\t {
\t document.getElementById("favorite").src="http://www.********.com/images/favorite-off.png";
\t document.getElementById("favtext").innerHTML="Add to your favorites.";
\t }
}
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>');
}
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 . "';");
\t if (mysqli_num_rows($urow) > 0)
\t {
\t $favcheck = mysqli_query($conn, "SELECT Users.Favorites FROM Users WHERE Users.User='" . $usercrypt . "';");
\t $favcheck = mysqli_fetch_array($favcheck);
\t $favcheck = $favcheck[0];
\t \t if (strpos($favcheck, $place) !== false)
\t \t {
\t \t $answer = 'found';
\t \t }
\t \t else
\t \t {
\t \t $answer = 'notfound';
\t \t }
\t }
\t else
\t {
\t $answer = 'nouser';
\t }
return array($answer,$favcheck);
unset($answer);
}
\t
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];
\t if ($sendback[0] == 'found')
\t {
\t $currentnum--;
\t $change = str_replace($place,'',$sendback[1]);
\t mysqli_query($conn, "UPDATE Users SET Favorites='" . $change . "' WHERE User = '" . $usercrypt . "'");
\t mysqli_query($conn, "UPDATE Places SET Places.Favorites=" . $currentnum . " WHERE Places.PlaceID =" . $placeid);
\t $answer = 'subtracted';
\t }
\t if ($sendback[0] == 'notfound')
\t {
\t $currentnum++;
\t $change = $sendback[1] . $place;
\t mysqli_query($conn, "UPDATE Users SET Favorites='" . $change . "' WHERE User = '" . $usercrypt . "'");
\t mysqli_query($conn, "UPDATE Places SET Places.Favorites=" . $currentnum . " WHERE Places.PlaceID =" . $placeid);
\t $answer = 'added';
\t }
return $answer;
unset($answer);
}
unset($_POST);
?>
你能張貼您的HTML爲好,好嗎? –
只是一個說明:返回後的'unset'不會被執行,'global'只能在函數中使用。 – jcubic
HTML太長而無法添加到帖子中。這是一個標準的WordPress頁面。這是頁面文本的內容: '?' –
Ryan