1

我正在尝试根据时间农场自动调用mysql查询内容以将其显示为一行显示mysql查询的内容

我能够制作计时器功能,但我无法将其植入 ajax 功能以自动调用 mysql 查询

我需要的是把我在loop()函数中创建的变量放在 ajax 函数中showUser(str)

这是我的带有 javascript 和 ajax 代码的 html:

<html>
<head>
<script>


var x=0;
function loop() {
    x= x+1;
if (x == 100) x = 1;
document.getElementById("timevar").innerHTML=x;    
setTimeout("loop()", 5000);     
}    loop(); 

function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true); \\ timevar should be instead of str
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

timevar 应该代替 str 或赋予其showUser功能的价值

这是页面上调用 getuser.php 的 php 代码:

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Comments</th>
<th>Clicks</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Comments'] . "</td>";
  echo "<td>" . $row['Clicks'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

我想做的是让每个名字的信息出现并在同一行翻转..我不希望它们出现在表格中..我试图让它更具动态性

任何帮助表示赞赏..非常感谢

4

3 回答 3

2

您可能会发现使用 jQuery 来处理您的 ajax 调用可能会让您的生活更轻松。

http://api.jquery.com/jQuery.get/

    $.get('ajax/callmySQL.php');
于 2013-01-29T22:51:09.297 回答
1

你真的应该看看jQuery。它使生活变得更加轻松。见http://api.jquery.com/jQuery.get/

jQuery 的 $.get 在请求完成时调用回调。您只需从它们更新文档并为下一个请求设置计时器。

就像是:

function update() {
    $.get(url, null, function(data, textStatus, jqXHR) {
            $('#txtHint').html(data);
            setTimeout(update, 5000);
        });
}
setTimeout(update, 5000);

顺便说一句,不需要将代码作为字符串传递,您可以直接输入函数名称:setTimeout(loop, 5000);

于 2013-01-29T22:54:32.610 回答
0

我找到了我想要的东西..这只是因为我在慢慢学习并且对 javascript 很陌生 :)

我一直都有答案,但我不知道怎么写,现在我知道了:)

timevar我定义了我知道它应该在那里的变量..现在我通过将它放在一个函数中来定义它,因为每 10 秒增加一个数字 go +1 然后我在间隔 10 秒后再次将它放入循环中

我想我写了太多我不需要的东西..但毕竟它可以找到:)

这是答案,也许它会在某个时候帮助某人

<html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true); // timevar should be instead of str
xmlhttp.send();
}

var setdelay=10000 //delay 10 seconds
var timevar=0;

function moveit(how){
if (how==1){ //cycle foward
if (timevar<100)
timevar++
else
timevar=0
}
else{ //cycle backward
if (timevar==0)
timevar=100
else
timevar--
}}

setInterval("moveit(1)",setdelay)

var x=0;
function loop() {
    x= x+1;
if (x == 100) x = 1;
showUser("timevar");
setTimeout("loop()", 10000);     
}    loop();
</script>
</head>
<body onload="loop()">

<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>
于 2013-01-31T08:35:22.243 回答