2016-07-15 40 views
0

我有以下功能:jQuery的阿賈克斯POST不是PHP變量返回

$(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」設置爲新值。它應該設置的PHP變量應該由Ajax($ uniqueend)執行的.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 /內容數據它返回,所以我不知道如何適合或工作。

謝謝!

+0

因此,基本上你正在做一個ajax請求,在那裏你改變了一個變量,然後你正在期待'echo'語句來實際迴應出已更改的變量?這不是它的工作方式,'echo'語句在服務器上發生,在頁面發送到瀏覽器之前很久,甚至在ajax調用發生之前更長時間,稍後在服務器上更改該變量不會影響過去迴應了什麼。 – adeneo

+0

該代碼非常危險。你很容易受到[sql注入攻擊](http://bobby-tables.com)的影響,你只是假設查詢永遠不會失敗。兩者都是非常糟糕的事情。 –

+0

@MarcB我將在稍後驗證.php文件中的發佈數據。我只是想先讓我的功能爲我工作(我相信這是一個合理的解決方案,不確定,仍在學習)。 – Ben

回答

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; 
     } 
    } 
    });  
} 
}); 

評論後:

你可以把你的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; 
     } 
    } 
    });  
} 
}); 
+0

嗨,我已經添加了額外的代碼,如果我使用'數據'將返回。你能告訴我如何將兩者分開,並使用相同的請求返回。謝謝! – Ben

+0

你可以把你的html放入一個變量中,而不是立即輸出它。然後將html和id放入像'$ returnArray ['html'] = $ html; $ returnArray ['id'] = $ uniqueend; ',編輯答案 – Philipp

+0

感謝您的解決方案,看看我是否可以實現它的工作。順便說一句,你的評論後的最後一點是停止功能執行,如果它已經達到底部。當沒有更多的數據被返回時它達到底部。我想我只是把它改成'if($ data ['html'] <1){' – Ben

2

這是一個非常普遍的誤解,這些索引。在你的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; 
     } 
    } 
    });  
} 
}); 

如果您有其他的信息發送到瀏覽器,以及你要麼需要與不同參數的多個請求或發送回一個JSON響應,並分析它的JavaScript端。您可以通過json發送相當大量的數據。如果通過json發送的數據太多,您可能需要發送多個請求。就像我在頂部提到的那樣,您無法訪問javascript內部的php變量。

+0

Hiya,我已經添加了額外的代碼,如果我使用'數據'將返回。你能告訴我如何將兩者分開,並使用相同的請求返回。謝謝! – Ben

+0

@Ben不只是傳遞純html通過你應該做的是通過它作爲json。在PHP中,您可以創建一個數組,而不是使用echo將其打印到頁面,將其作爲數組中的值存儲,然後使用json_encode對其進行編碼。然後echo你的json並從json數組中獲取html。然後,您還可以將其他信息作爲數值存儲在您的數組中,並以相同方式獲取。 –

+0

這些鏈接應該涵蓋你所需要的。 [JSON編碼](http://php.net/manual/en/function.json-encode.php) 和[JQuery getJson](http://api.jquery.com/jquery.getjson/) –