繁体   English   中英

jQuery Ajax POST不返回PHP变量

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM