Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 链接php代码的HTML代码,用于显示存储的tweet不起作用_Javascript_Php_Jquery_Html_Twitter - Fatal编程技术网

Javascript 链接php代码的HTML代码,用于显示存储的tweet不起作用

Javascript 链接php代码的HTML代码,用于显示存储的tweet不起作用,javascript,php,jquery,html,twitter,Javascript,Php,Jquery,Html,Twitter,上面的代码针对twitter_display.php单独运行,但当我将其嵌入html代码时,它不会显示所需的tweet。 当我用一些关键字填充文本框并单击GetTweets按钮时,它给出了一个错误 未捕获的语法错误:意外标记

上面的代码针对twitter_display.php单独运行,但当我将其嵌入html代码时,它不会显示所需的tweet。 当我用一些关键字填充文本框并单击GetTweets按钮时,它给出了一个错误 未捕获的语法错误:意外标记
浏览我添加的新文件。希望它能帮助您。

鉴于twitter\u display.php的结果是一些HTML,这可以:

Javascript:

![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;
}
HTML:

这里是它的中间部分。

如果结果是JSON,则必须在resonse处理中对此进行一些解析

我也会考虑添加jQuery,因为这样会使你更容易做的事情

编辑:现在还包括关于如何修复PHP代码的建议:

SQL不包含查询,这就是问题所在。 您可能需要将代码更改为以下内容:

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

PHP代码更改可能包含语法错误,已经多年没有使用PHP了

能否请您提供twitter_display.PHP的输出示例在等待twitter_display.PHP的输出时,如果!xhr.status==200{to if xhr.status!=200{而且你的代码只处理失败的请求,没有处理成功的请求,其中xhr.status==200我已经附加了一个twitter_display.php的输出图像。我想将我的第一个html页面链接到这个页面以获得上面显示的输出。我如何实现它?我不知道我应该为xhr.status==200提供什么代码。请建议我不要这样做查看任何附加的屏幕转储。非常感谢您宝贵的时间。它正在工作。但是,如果我更改文本框中的关键字,它将显示相同的tweet,并且不会根据文本框值进行更新。如何解决此问题?这听起来像是twitter_display.php的问题。我在JSFiddle:中添加了一个url警报,这表明查询是added correct添加到URL我需要更多关于PHP代码的信息,以便就如何解决这个问题给出建议。正如我从您提供的PHP代码中所看到的,没有任何地方可以查看查询参数中的内容。抱歉,但是我没有得到您关于JSFIDLE的信息。我第一次听到它。它对我有什么用处?您能建议我如何使用它吗我应该修改twitter_display.php以获得所需的结果吗?我应该提供更多对您有用的文件吗?
<form>Query:
    <input type="text" id="query">
    <input type="button" id="submit" value="Get Tweets">
</form>
<div id="error"></div>
<div id="result"></div>
$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'] . '" ';
}