2014-02-23 67 views
1
![enter image description here][1]![image of output of twitter_display.php][2] //htmlsearch.html file 
<!doctype html> 
<html> 
<head> 
<title>Twitter</title> 
<meta charset="utf-8"> 
<script> 
window.onload = function() { 
// set up the click handler for the form button 
var button = document.getElementById("submit"); 
button.onclick = getTweets; 
} 

// when you click "Get Tweets" we call this function 
function getTweets() { 
// set up a new XHR request 
var xhr = new XMLHttpRequest(); 
// we're calling search.php and passing in a query string 
var url = "twitter_display.php?query="; 
var query = document.getElementById("query").value; 
if (!query) { 
    query = "html5"; 
} 
// we encode the query to handle any special characters properly 
url += encodeURIComponent(query); 

// this is the function that is called when the XHR request 
// to our search.php script is handled, and a response sent back 
xhr.onload = function() { 
     if (!xhr.status == 200) { 

     var errorDiv = document.getElementById("error"); 
     errorDiv.innerHTML = "Error getting tweets: " + xhr.status; 
    } 
}; 
// make the request! 
xhr.open("GET", url,true); 
xhr.send(null); 
} 

</script> 
</head> 
<body> 
<form> 
Query: <input type="text" id="query"> 
<input type="button" id="submit" value="Get Tweets"> 
</form> 
<div id="error"></div> 
<ul></ul> 
</body> 
</html> 


//twitter_display.php file 
<?php 

// Get constants for tweet display 
require_once("twitter_display_config.php"); 

// Get the HTML structure 
$tweet_page = file_get_contents('tweet_list_template.txt'); 

// Fill in the most recent individual tweets 
$tweet_page = str_replace('[tweets]',require_once('get_tweet_list.php'), $tweet_page); 

// Fill in the constants and strings needed by site.js after the page loads 
$tweet_page = str_replace('[new_count_refresh]', NEW_COUNT_REFRESH, $tweet_page); 
$tweet_page = str_replace('[ajax_url]', AJAX_URL, $tweet_page); 
$tweet_page = str_replace('[more_button]', MORE_BUTTON, $tweet_page); 

// Return the results as HTML 
print $tweet_page; 
?> 

//added new code part for more info i.e get_tweet_list.php 
<?php 

require_once('twitter_display_config.php'); 
require_once('display_lib.php'); 
require_once('db_lib.php'); 
$oDB = new db; 

$query = 'SELECT profile_image_url, created_at, screen_name, 
name, tweet_text, tweet_id 
FROM tweets '; 

// Query string of last=[tweet_id] means that this script was called by site.js 
// when the More Tweets button was clicked 
if (isset($_GET['last'])) { 
$query .= 'WHERE tweet_id < "' . $_GET['last'] . '" '; 
} 

$query .= 'ORDER BY tweet_id DESC LIMIT ' . TWEET_DISPLAY_COUNT; 
$result = $oDB->select($query); 

// Use the text file tweet_template.txt to construct each tweet in the list 
$tweet_template = file_get_contents('tweet_template.txt'); 
$tweet_list = ''; 
$tweets_found = 0; 
while (($row = mysqli_fetch_assoc($result)) 
&&($tweets_found < TWEET_DISPLAY_COUNT)) { 

++$tweets_found; 

// create a fresh copy of the empty template 
$current_tweet = $tweet_template; 

// Fill in the template with the current tweet 
$current_tweet = str_replace('[profile_image_url]', 
$row['profile_image_url'], $current_tweet); 
$current_tweet = str_replace('[created_at]', 
twitter_time($row['created_at']), $current_tweet);   
$current_tweet = str_replace('[screen_name]', 
    $row['screen_name'], $current_tweet); 
$current_tweet = str_replace('[name]', 
$row['name'], $current_tweet);  
$current_tweet = str_replace('[user_mention_title]', 
USER_MENTION_TITLE . ' ' . $row['screen_name'] . ' (' . $row['name'] . ')', 
$current_tweet); 
$current_tweet = str_replace('[tweet_display_title]', 
TWEET_DISPLAY_TITLE, $current_tweet); 
$current_tweet = str_replace('[tweet_text]', 
linkify($row['tweet_text']), $current_tweet); 

// Include each tweet's id so site.js can request older or newer tweets 
$current_tweet = str_replace('[tweet_id]', 
$row['tweet_id'], $current_tweet); 

// Add this tweet to the list 
$tweet_list .= $current_tweet; 
} 

if (!$tweets_found) { 
if (isset($_GET['last'])) { 
$tweet_list = '<strong>No more tweets found</strong><br />'; 
} else { 
$tweet_list = '<strong>No tweets found</strong><br />'; 
} 
} 

if (isset($_GET['last'])) { 
// Called by site.js with Ajax, so print HTML to the browser 
print $tweet_list; 
} else { 
// Called by twitter_display.php with require(), so return the value 
    return $tweet_list; 
} 

上述代碼單獨運行twitter_display.php,但是當我將它嵌入到我的html代碼中時,它不顯示所需的推文。 當我填寫文本框,使一些關鍵字,然後點擊獲取鳴叫按鈕,它提供了一個錯誤 「未捕獲的SyntaxError:意外的標記< htmlsearch1.html:46個 displayTweets htmlsearch1.html:46 xhr.onload htmlsearch1.html:33」 您能否請建議更改代碼?請回復HTML代碼鏈接php代碼獲取顯示儲存的推文不工作

請仔細閱讀我已添加的新文件。希望它能幫助您。

+0

能否請您在提供樣本twitter_display.php的輸出 –

+0

在等待twitter_display.php的輸出時,可以將'if(!xhr.status == 200){''更改爲'if(xhr.status!= 200){' –

+0

也是您的代碼只處理失敗請求,沒有處理成功的請求(其中xhr.status == 200) –

回答

0

鑑於twitter_display.php的結果是一些HTML這將做到:

的Javascript:

window.onload = function() { 
    // set up the click handler for the form button 
    var button = document.getElementById("submit"); 
    button.onclick = getTweets; 
} 

// when you click "Get Tweets" we call this function 
function getTweets() { 
    // set up a new XHR request 
    var xhr = new XMLHttpRequest(); 
    // we're calling search.php and passing in a query string 
    var url = "twitter_display.php?query="; 
    var query = document.getElementById("query").value; 
    if (!query) { 
     query = "html5"; 
    } 
    // we encode the query to handle any special characters properly 
    url += encodeURIComponent(query); 

    // this is the function that is called when the XHR request 
    // to our search.php script is handled, and a response sent back 
    xhr.onload = function() { 
     if (xhr.status == 200) { 
      var resultDiv = document.getElementById("result"); 
      resultDiv.innerHTML = this.responseText; 
     } else { 
      var errorDiv = document.getElementById("error"); 
      errorDiv.innerHTML = "Error getting tweets: " + xhr.status; 
     } 
    }; 
    // make the request! 
    xhr.open("GET", url, true); 
    xhr.send(); 
} 

HTML:

<form>Query: 
    <input type="text" id="query"> 
    <input type="button" id="submit" value="Get Tweets"> 
</form> 
<div id="error"></div> 
<div id="result"></div> 

這裏是在它的jsfiddle。 http://jsfiddle.net/93ypB/

如果結果是JSON,您將不得不在解析處理中做一些解析。

此外,我會考慮加入jQuery的,因爲這樣做你正在嘗試做更容易

編輯:

你的SQL沒有按:現在還包括關於如何解決PHP代碼的建議不包括查詢,即問題。 您可能需要更改代碼以這樣的事:

$query = 'SELECT profile_image_url, created_at, screen_name, 
name, tweet_text, tweet_id 
FROM tweets WHERE tweet_text LIKE \'%' . $_GET['query'] . '%\' '; 

// Query string of last=[tweet_id] means that this script was called by site.js 
// when the More Tweets button was clicked 
if (isset($_GET['last'])) { 
    $query .= 'AND tweet_id < "' . $_GET['last'] . '" '; 
} 

(多年的PHP代碼的改變可能含有語法錯誤,還沒有使用PHP)

+0

非常感謝您爲寶貴的時間。它正在工作。如果我更改文本框中的關鍵字它顯示相同的推文,它不會根據文本框值更新。如何解決此問題? – user3238174

+0

這聽起來像twitter_display.php的問題。我向JSFiddle添加了一個url提示:http://jsfiddle.net/93ypB/1/它顯示查詢添加了正確的網址 –

+0

我需要更多關於PHP代碼的信息才能給出建議如何解決這個問題。正如我從您提供的PHP代碼中可以看到的那樣,沒有什麼地方可以查看「查詢」參數中的內容。 –