Javascript jQuery Ajax POST未使用PHP变量返回

Javascript jQuery Ajax POST未使用PHP变量返回,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有以下功能: $(window).scroll(function() { if(ready && labelstatus && $(window).scrollTop() + $(window).height() > $(document).height() - 100){ $('#bottom2').html(loading); ready = false; $.ajax({ url: 'scripts/nearbothome.

我有以下功能:

$(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;
        }
    }
  });    
}
});
$(窗口)。滚动(函数(){
if(ready&&labelstatus&&$(window.scrollTop()+$(window.height()>$(document.height()-100){
$('#bottom2').html(正在加载);
就绪=错误;
$.ajax({
url:'scripts/nearbothome.php',
数据:{“currentNumber”:botnumber,“usersid”:usersid},
键入:“post”,
成功:功能(数据){
botnumber=“”;
警报(“”);
$('#oldposts')。追加(数据);
$('#bottom2').html(底部);
就绪=正确;
labelstatus=数据;
如果(数据<1){
标签状态=假;
}
}
});    
}
});
除了将变量“botnumber”设置为一个新值外,这一切都可以正常工作并按预期执行。应该设置为的PHP变量应该由Ajax($uniquend)执行的.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;

?>

Ajax不是那样工作的。php脚本在服务器上的返回值作为响应发送到浏览器,然后将其放入success回调函数的数据参数中。您将看到,您的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;
        }
    }
  });    
}
});
$(窗口)。滚动(函数(){
if(ready&&labelstatus&&$(window.scrollTop()+$(window.height()>$(document.height()-100){
$('#bottom2').html(正在加载);
就绪=错误;
$.ajax({
url:'scripts/nearbothome.php',
数据:{“currentNumber”:botnumber,“usersid”:usersid},
键入:“post”,
成功:功能(数据){
botnumber=数据;
警报(数据);
$('#oldposts')。追加(数据);
$('#bottom2').html(底部);
就绪=正确;
labelstatus=数据;
如果(数据<1){
标签状态=假;
}
}
});    
}
});
评论后:


您可以将html放入变量中,而不是立即输出它。然后将html和id放入一个数组中,如

$html = "<div>";
// add more html into variable
$html .= "</div>;
$returnArray['html'] = $html; 
$returnArray['id'] = $uniqueend;
$html=”“;
//在变量中添加更多html
$html.=”;
$returnArray['html']=$html;
$returnArray['id']=$Uniquend;
在前端,您必须访问数据中的索引

$(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;
        }
    }
  });    
}
});
$(窗口)。滚动(函数(){
if(ready&&labelstatus&&$(window.scrollTop()+$(window.height()>$(document.height()-100){
$('#bottom2').html(正在加载);
就绪=错误;
$.ajax({
url:'scripts/nearbothome.php',
数据:{“currentNumber”:botnumber,“usersid”:usersid},
键入:“post”,
成功:功能(数据){
botnumber=$data['id'];
警报($data['id']);
$('#oldposts')。追加($data['id']);
$('#bottom2').html(底部);
就绪=正确;
//不知道从现在起你想做什么
labelstatus=数据;
如果(数据<1){
标签状态=假;
}
}
});    
}
});

Ajax不是这样工作的。服务器上php脚本的返回值作为响应发送到浏览器,然后将其放入成功回调函数的数据参数中。因此,您将看到,您的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;
        }
    }
  });    
}
});
$(窗口)。滚动(函数(){
if(ready&&labelstatus&&$(window.scrollTop()+$(window.height()>$(document.height()-100){
$('#bottom2').html(正在加载);
就绪=错误;
$.ajax({
url:'scripts/nearbothome.php',
数据:{“currentNumber”:botnumber,“usersid”:usersid},
键入:“post”,
成功:功能(数据){
botnumber=数据;
警报(数据);
$('#oldposts')。追加(数据);
$('#bottom2').html(底部);
就绪=正确;
labelstatus=数据;
如果(数据<1){
标签状态=假;
}
}
});    
}
});
评论后:


您可以将html放入变量中,而不是立即输出它

$html = "<div>";
// add more html into variable
$html .= "</div>;
$returnArray['html'] = $html; 
$returnArray['id'] = $uniqueend;
$html=”“;
//在变量中添加更多html
$html.=”;
$returnArray['html']=$html;
$returnArray['id']=$Uniquend;
在前端,您必须访问数据中的索引

$(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;
        }
    }
  });    
}
});
$(窗口)。滚动(函数(){
if(ready&&labelstatus&&$(window.scrollTop()+$(window.height()>$(document.height()-100){
$('#bottom2').html(正在加载);
就绪=错误;
$.ajax({
url:'scripts/nearbothome.php',
数据:{“currentNumber”:botnumber,“usersid”:usersid},
键入:“post”,
成功:功能(数据){
botnumber=$data['id'];
警报($data['id']);
$('#oldposts')。追加($data['id']);
$('#bottom2').html(底部);
就绪=正确;
//不知道从现在起你想做什么
labelstatus=数据;
如果(数据<1){
标签状态=假;
}
}
});    
}
});

这是一个非常常见的误解。在javascript中,您试图调用php代码,但是js在浏览器上运行,无法解析php或访问服务器端php变量。JQuery将返回数据变量中发送到浏览器的任何内容,您应该在那里访问它。为了做到这一点,您的代码将如下所示

$(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;
        }
    }
  });    
}
});
$(窗口)。滚动(函数(){
if(ready&&labelstatus&&$(window.scrollTop()+$(window.height()>$(document.height()-100){
$('#bottom2').html(正在加载);
就绪=错误;
$.ajax({
url:'scripts/nearbothome.php',
数据:{“currentNumber”:botnumber,“usersid”:usersid},
键入:“post”,
成功:功能(数据){
botnumber=数据;
警报(数据);
$('#oldposts')。追加(数据);
$('#bottom2').html(底部);
就绪=正确;
labelstatus=数据;
如果(数据<1){
标签状态=假;
}
}
});    
}
});
如果您还有其他信息要发送到浏览器,则需要使用不同的参数发出多个请求,或者发送回json响应并在javascript端对其进行解析。您可以通过json发送大量数据。如果通过json发送的数据太多,您可能需要发送多个请求。像我