gpt4 book ai didi

javascript - 使用js出现php错误

转载 作者:行者123 更新时间:2023-12-02 16:14:48 25 4
gpt4 key购买 nike

嘿伙计们,我有一个数据库,正在尝试获取一些结果..我可以使用 php 成功获取结果..我需要的是在 3 秒后运行结果..所以我使用了 'setTimeout()`在 js 中,但没有成功..我的代码

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
@$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}


$sql = 'SELECT age FROM honey';
mysql_select_db('test');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo '<script>
var c = 3000;
setTimeout(function() { '<?php echo $row['age'];'}, c);
</script>';
}
mysql_close($conn);
?>

我得到的错误是解析错误:语法错误,意外的“?”在 C:\wamp\www\refresh\index.php 第 24 行 ..

我怎样才能在这个php代码中正确使用js..感谢帮助

最佳答案

您似乎没有理解客户端和服务器之间的区别。

完成此操作的最简单方法是使用 xmlhttprequest(查看 jquery 未链接)。

setInterval(function () {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log('ages: ' + xmlhttp.responseText);
}
}
xmlhttp.open("GET", "php_ajax_handler_url_here", true);
xmlhttp.send();
}, 3000);

现在您需要做的就是将“php_ajax_handler_url_here”替换为您的 php 文件名。在你的 php 中你需要做的就是回显你的结果:

$res = '';
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$res += $row['age'].' ';
}
echo $res;

那么这一切有什么作用呢?那么客户端会用你的js加载你的html文件。您的 js(3 秒后)将调用处理 ajax 响应的 php 文件。它将在您的数据库中搜索信息,并将其作为一个大字符串全部回显。

更新:

Demo without XHMLHttpRequest()

我为你创建了一个小图书馆。我已经测试过了,所以应该可以工作。

那么如何使用这个库呢?这真的很简单。

 var a = age('ajax_url_handler_here').load(function(){
console.log('done loading');
});

然后你就开始

a.start(function(age){
//do something with age
//The callback is called every 3 seconds and updates age with new age
});

然后停止

a.stop(); 
<小时/>
(function () {

var age = function (url) {
if (!(this instanceof age)) {
return new age();
}
if(!(typeof url === 'undefined')){
this.url = url;
}

this.ages = [];
this.counter = 0;
this.setIntervalId = -1;

}


age.fn = age.prototype = {
init: function () {}
};
//load unchainable
age.fn.load = function (callback) {

var xmlhttp = new XMLHttpRequest(),
_this = this;
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log('ages: ' + xmlhttp.responseText);

//creating an array
_this.ages = xmlhttp.responseText.split(' ');

if(!(typeof callback === 'undefined')){
callback();
}
}
};
xmlhttp.open("GET", this.url, true);
xmlhttp.send();
return this;
}
age.fn.start = function (callback) {
this.counter = 0;
if(this.ages.length > 0){
var _this = this;
this.setIntervalId = setInterval(function(){
console.log('age: ' + _this.ages[_this.counter]);
if(!(typeof callback === 'undefined')){
callback(_this.ages[_this.counter]);
}
_this.counter = (_this.counter + 1) % _this.ages.length;
}, 3000);
}

//chainable
return this;
};
age.fn.stop = function(){
if(this.setIntervalId > -1){
clearInterval(this.setIntervalId);
this.setIntervalId = -1;
}

//chainable
return this;
};
window.age = age;
})();

更新#2:

如果由于某种原因该库无法工作,请随意尝试这个简化版本。您所需要做的就是将 var url = "url_here" 替换为正确的 url。

如果您遇到任何错误,请告诉我。

var url = "url_here",
ages = [],
setAges = function (callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log('Response: ' + xmlhttp.responseText);

//creating an array
ages = xmlhttp.responseText.split(' ');
if (!(typeof callback === 'undefined')) {
callback();
}
} else {
console.log('An error occured');
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
};

var siId = -1,
si = function (callback) {
var counter = 0;
siId = setInterval(function () {
if (!(typeof callback === 'undefined')) {
callback(ages[counter]);
}
counter = (counter + 1) % ages.length;
}, 3000);
}


setAges(function(){
console.log('xmlhttprequest finished starting interval');
si(function(age){
//do something with res
console.log('age: ' + age);
});
});

关于javascript - 使用js出现php错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29777380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com