简体   繁体   English

php / javascript无法在线工作

[英]php/javascript not working online

I have some code in php and javascript which is working on my computer, localhost, but is not working online. 我在php和javascript中有一些可在我的计算机localhost上运行的代码,但无法在线运行。 I use Godaddy and I called them. 我用Godaddy打电话给他们。 They said everything looks good on their end. 他们说一切看起来都很好。 They said they can't help with coding problems. 他们说他们无法解决编码问题。 I have a database containing some quotes. 我有一个包含一些引号的数据库。 The function I'm trying to perform is to get a random quote from the database and display it on my webpage then change it to a new random quote every 10 seconds. 我要执行的功能是从数据库中获取随机报价并将其显示在我的网页上,然后每10秒将其更改为新的随机报价。 I don't know anything about php or javascript. 我对php或javascript一无所知。 I paid someone to write this code and they said the code is good. 我付钱给别人写这段代码,他们说这段代码很好。

The javascript is: JavaScript是:

var qouteobj=createRequestObject();
function getqoute(){
qouteobj.open("GET","php/randomquote.php",true);
qouteobj.send(null);
qouteobj.onreadystatechange=function(){
if(qouteobj.readyState==4 && qouteobj.status==200){
var q_rec=qouteobj.responseXML;
var rq=q_rec.getElementsByTagName('qoute');
var txt="";
var i;
for (i=0;i<rq.length;i++){
txt=txt + rq[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById('random-quote').innerHTML=txt;
setTimeout('getqoute()',10000);
setTimeout('getqoute()',10000);
}
}

The randomquote.php file is: randomquote.php文件是:

<?xml version='1.0' ?>
<?php
include "connect.php";
header("Content-Type: text/xml; charset=utf-8");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );

$sql = "SELECT quote FROM quotes where Rand() Limit 1";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$xml="<root>";
$xml.="<qoute>".$row['quote']."</qoute>";

$xml.="</root>";
echo $xml;
//echo $row['quote'];
mysqli_close($conn);
?>

The html is simply: 的HTML是简单的:

<p id="random-quote"></p>

I know it's connecting to the database because I have other php code on the site that is working, pulling data from the database and displaying it on the page. 我知道它正在连接到数据库,因为我在网站上有其他正在运行的php代码,它们从数据库中提取数据并将其显示在页面上。 The page is at: www.bestmoviequote.com Thanks for looking into this for me. 该页面位于: www.bestmoviequote.com感谢您对我的关注。

I guess the problem comes from the fact you write things before sending headers. 我猜问题出在你发送头之前写东西的事实。

try with that : 试试看:

<?php
    include "connect.php";

    $sql = "SELECT quote FROM quotes where Rand() Limit 1";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    mysqli_close($conn);

header("Content-Type: text/xml; charset=utf-8");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
$xml="<?xml version='1.0' ?>";
$xml.="<root>";
$xml.="<qoute>".$row['quote']."</qoute>";
$xml.="</root>";
echo $xml;

If there is still problems, please add ini_set('display_errors', 1) before the include of the connect script, and give us the errors 如果仍然有问题,请在包含连接脚本之前添加ini_set('display_errors', 1) ,并向我们提供错误

We got it working. 我们让它工作了。 We also had a problem with the original code not displaying a truly random quote. 我们还遇到了一个问题,即原始代码没有显示真正的随机报价。 It was favoring quotes with a higher amount of likes (I had another page where users could like the quotes.) The following solution fixed both problems. 它喜欢带有更多点赞的报价(我在另一个页面上用户喜欢报价。)以下解决方案解决了这两个问题。

Javascript: Javascript:

var qouteobj=createRequestObject();
function getqoute(){
qouteobj.open("GET","/php/randomquote.php",true);
qouteobj.send(null);
qouteobj.onreadystatechange=function(){
if(qouteobj.readyState==4 && qouteobj.status==200)
{
var q_rec=qouteobj.responseText;
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(q_rec,"text/xml");
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(q_rec);
}
var rq=xmlDoc.getElementsByTagName("qoute");
var txt="";
var i;
for (i=0;i<rq.length;i++){
txt=txt + rq[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById('random-quote').innerHTML=txt;
setTimeout('getqoute()',10000);
}
}
}

randomquote.php: randomquote.php:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "moviequotes";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
header("Content-Type: text/xml; charset=utf-8");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
$sql = "SELECT COUNT(*) FROM quotes";
$result = mysqli_query($conn,$sql);
$rows=$result->fetch_row();
//$rows = mysqli_num_rows($result);
$rndm=rand(1,$rows[0]);
$sql = "SELECT quote FROM quotes where id='".$rndm."' Limit 1";
$result = mysqli_query($conn,$sql);
if($result)
{
$row = mysqli_fetch_assoc($result);
$xml='<root>';
$xml.='<qoute>'.htmlspecialchars($row['quote']).'</qoute>';
$xml.='</root>';
echo $xml;
}
else
{
echo "Not working";
}
mysqli_close($conn);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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