У меня есть сеанс AJAX, который вызывается один раз на загрузку страницы и снова после щелчка. Во второй раз это не изменяет readistate, поэтому я решил протестировать его, поставив окно предупреждения в функцию. Появится окно предупреждения о загрузке страницы. Но по клику он не появляется - получите это, даже если сторона PHP выполняется. Еще более озадачивающим является то, что если я попытаюсь измерить readistate, он никогда не появится (не 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. Это содержание текста страницы: ' Php include (' connect.php '); $ place = 63; включают ('placelist.php'); ?> ' – Ryan