[英]jQuery Ajax POST not returning with PHP variable
我有以下功能:
$(window).scroll(function() {
if(ready && labelstatus && $(window).scrollTop() + $(window).height() > $(document).height() - 100){
$('#bottom2').html(loading);
ready = false;
$.ajax({
url: 'scripts/nearbothome.php',
data: {"currentNumber": botnumber, "usersid": usersid},
type: 'post',
success: function(data) {
botnumber = "<?php echo $uniqueend; ?>";
alert("<?php echo $uniqueend; ?>");
$('#oldposts').append(data);
$('#bottom2').html(bottom);
ready = true;
labelstatus = data;
if (data < 1) {
labelstatus = false;
}
}
});
}
});
所有這些都工作正常,並且除了將變量“ botnumber”設置為新值外,還可以按預期進行。 應該由Ajax($ uniqueend)執行的.php文件返回應該設置為PHP變量。
看這里:
<?php
//open a database connection
include("../db.php");
//receive value
$currNum = $_POST['currentNumber'];
$uidd = $_POST['usersid'];
$results7 = mysql_query("SELECT * FROM `uc_posts` WHERE `postinguser` IN (SELECT `followers` FROM `uc_users` WHERE `id` = $uidd) AND id < $currNum ORDER BY id DESC LIMIT 20");
sleep(1);
while ($row = mysql_fetch_array($results7)) {
echo '<div class="postfeed2">';
$color='#ababab';
$id = $row['id'];
$page = $row['page'];
$postinguser = $row['postinguser'];
$displayname=mysql_fetch_array(mysql_query("SELECT `display_name` FROM `uc_users` WHERE `id` = $postinguser LIMIT 1"));
$username=mysql_fetch_array(mysql_query("SELECT `user_name` FROM `uc_users` WHERE `id` = $postinguser LIMIT 1"));
$checkiffav=mysql_fetch_array(mysql_query("SELECT * FROM `uc_posts` WHERE find_in_set($uidd,`likedby`) AND `id` = $id"));
if ($checkiffav) {
$color='#FF5733';
}
echo '<table><tr><td style="vertical-align: baseline;"><b><img src="blank-user-medium.png" width="50px" height="50px" style="vertical-align: text-top;margin-right: 8px;margin-top: 0px;border: 0px solid #D0D0D0;border-radius: 5px 5px 5px 5px;"></b></td>';
echo '<td style="display: block;word-break: break-word;"><b>' . $displayname[display_name] . '</b>';
echo ' <span style="color:#ababab;">@' . $username[user_name] . '</span>';
echo '<br>' . $page . '';
echo '<br><a href="javascript:void(0)" onclick="javascript:return like(' . $id . ',' . $uidd . ');"><i class="fa fa-heart fa-lg" id="heart' . $id . '" style="margin-top: 5px;color:' . $color . '"></i></a></td></tr></table>';
echo '</div>';
}
$uniqueend2 = mysql_fetch_array(mysql_query("(SELECT * FROM `uc_posts` WHERE `postinguser` IN (SELECT `followers` FROM `uc_users` WHERE `id` = $uidd) AND id < $currNum ORDER BY id DESC LIMIT 20) ORDER BY id ASC"));
$uniqueend = $uniqueend2['id'];
echo $uniqueend;
?>
我可以看到它回顯為:184。但是在警報中,我添加到jQuery以驗證其中沒有內容。
我會使用json,但我會返回大量的php / content數據,因此我不確定該如何安裝或工作。
謝謝!
這是一個非常普遍的誤解。 在您的JavaScript中,您嘗試調用php代碼,但是js在瀏覽器上運行,並且無法解析php或訪問服務器端php變量。 jQuery將在data變量中返回發送到瀏覽器的所有內容,您應該在那里訪問它。 為了做到這一點,您的代碼將如下所示。
$(window).scroll(function() {
if(ready && labelstatus && $(window).scrollTop() + $(window).height() > $(document).height() - 100){
$('#bottom2').html(loading);
ready = false;
$.ajax({
url: 'scripts/nearbothome.php',
data: {"currentNumber": botnumber, "usersid": usersid},
type: 'post',
success: function(data) {
botnumber = data;
alert(data);
$('#oldposts').append(data);
$('#bottom2').html(bottom);
ready = true;
labelstatus = data;
if (data < 1) {
labelstatus = false;
}
}
});
}
});
如果您還有其他要發送到瀏覽器的信息,則需要使用不同的參數發出多個請求,或者發送回json響應並在javascript端進行解析。 您可以通過json發送大量數據。 如果某種程度上有太多數據無法通過json發送,則您可能需要向其發送多個請求。 就像我在頂部提到的那樣,您無法訪問javascript內的php變量。
Ajax不能那樣工作。 服務器上php腳本的返回值作為響應發送到瀏覽器,然后將其放入成功回調函數的data參數中。 因此,您將看到,您的ID位於數據上,因此您將不得不使用它。
像這樣嘗試:
$(window).scroll(function() {
if(ready && labelstatus && $(window).scrollTop() + $(window).height() > $(document).height() - 100){
$('#bottom2').html(loading);
ready = false;
$.ajax({
url: 'scripts/nearbothome.php',
data: {"currentNumber": botnumber, "usersid": usersid},
type: 'post',
success: function(data) {
botnumber = data;
alert(data);
$('#oldposts').append(data);
$('#bottom2').html(bottom);
ready = true;
labelstatus = data;
if (data < 1) {
labelstatus = false;
}
}
});
}
});
評論后:
您可以將html放在變量中,而不是立即輸出。 然后將html和id放入數組
$html = "<div>";
// add more html into variable
$html .= "</div>;
$returnArray['html'] = $html;
$returnArray['id'] = $uniqueend;
然后在前端,您必須訪問數據中的那些索引
$(window).scroll(function() {
if(ready && labelstatus && $(window).scrollTop() + $(window).height() > $(document).height() - 100){
$('#bottom2').html(loading);
ready = false;
$.ajax({
url: 'scripts/nearbothome.php',
data: {"currentNumber": botnumber, "usersid": usersid},
type: 'post',
success: function(data) {
botnumber = $data['id'];
alert($data['id']);
$('#oldposts').append($data['id']);
$('#bottom2').html(bottom);
ready = true;
// don't know what you are trying to do from here on
labelstatus = data;
if (data < 1) {
labelstatus = false;
}
}
});
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.