随机引用生成器与php,ajax和mysql

时间:2010-04-07 14:05:26

标签: php javascript jquery html ajax

我尝试使用this codethis制作随机引用生成器,但它不会显示任何内容。我的问题是:

  • 我的代码出了什么问题?

  • 在上面的tut中,引用是按下按钮生成的,我想 每个都要显示的随机引用 30分钟自动。我该怎么办 此

////////////////////////

quote.html:

<!DOCTYPE html>
<script src="ajax.js" type="text/javascript"></script>
<body>

<!–create the div for the quotes land–>
<div id="quote"><strong>this</strong></div>
<div><a style="cursor:pointer" onclick="run_query();">Next quote …</a></div>

</body>
</html>

/////////////////////

quote.php:

<?php
include 'config.php';

// 'text' is the name of your table that contains
// the information you want to pull from
$rowcount = mysql_query("select count(*) as rows from quotes");

// Gets the total number of items pulled from database.
while ($row = mysql_fetch_assoc($rowcount))
{
 $max = $row["rows"];
}

// Selects an item's index at random 
$rand = rand(1,$max)-1;
$result = mysql_query("select * from quotes limit $rand, 1");

$row = mysql_fetch_array($result);
$randomOutput = $row['storedText'];

echo '<p>' . $randomOutput . '</p>';

////////////

ajax.js:

var xmlHttp


function run_query() {
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ("This browser does not support HTTP Request");
return;
} // end if
var url="quote.php";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
} //end function

function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("quote").innerHTML=xmlHttp.responseText;
} //end if
} //end function

function GetXmlHttpObject() {
var xmlHttp=null;
try {
// For these browsers: Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
//For Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
} //end function

1 个答案:

答案 0 :(得分:1)

尝试打印$ max,$ rand和$ result的值。您可以使用print_r从php页面获取更多信息。

在浏览器上运行quote.php以查看是否获得输出。 然后转到ajax进行调试。

您可以使用ajax中的计时器每30分钟左右自动执行一次请求。使用javascript的settimeout函数。

HTH