0

I'm trying to make auto update on my page using JavaScript and php code to read the data from the server

    <html>
<head>
<script type="text/javascript">
function timeMsg()
{
var myVar=setInterval(function(){myTimer()},5000);
}
function myTimer()
{
document.write("<?php 
                session_start();
                $userdata = $_SESSION['views'];
                $name =  $userdata[1]; 
                $mysql_host = 'localhost';
                    $mysql_user = 'root';
                    $connection = mysql_connect($mysql_host,$mysql_user);
                    mysql_select_db("twitter2", $connection);
                    $query = "SELECT * FROM `logindata`";
                    $result =mysql_query($query) or die(mysql_error());
                            while($row = mysql_fetch_array($result))
                    {
                    echo $row['loginname'];
                    }
                        echo "<br>";echo "<br>";
?>");
}
</script>
</head>
<body>
<form>
<input type="button" value="Display alert box in 3 seconds"
onclick="timeMsg()" />
</form>
</body>
</html>

this code works fine but only display what found in the DB first time but if I add more rows in the DB this new rows doesn't appear on the page.

thanks All I changed some of my code to be like this

        <html>
<head>
<script type="text/javascript">
function timeMsg()
{
var myVar=setInterval(function(){myTimer()},1000);
}
function myTimer()
{
var xmlhttp;
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","phptest.php",true);
xmlhttp.send();
}
</script>

</head>
<body>
<form>
<input type="button" value="Display alert box in 3 seconds"
onclick="timeMsg()" />
<div id="txtHint"></div></p>
</form>
</body>
</html>

and the php file

<?php 

                $mysql_host = 'localhost';
                    $mysql_user = 'root';
                    $connection = mysql_connect($mysql_host,$mysql_user);
                    mysql_select_db("twitter2", $connection);
                    $query = "SELECT * FROM `logindata`";
                    $result =mysql_query($query) or die(mysql_error());
                            while($row = mysql_fetch_array($result))
                    {
                    echo $row['loginname'];
                    }

?>

and it works fine to me. Thanks for your help

4

4 回答 4

1

你做错了——你的 JavaScript 函数的内容在服务器上被解释并且总是相同的。您的 JavaScript 函数必须向服务器发送请求并处理响应。

如果您使用 JQuery,这可能看起来像这样

$.get('ajax/script.php', function(data) {
  $('.result').html(data);
});

在 script.php 中,您可以输出数据库结果

于 2013-07-15T19:57:12.927 回答
0

史蒂文是对的,这是我对 jquery 的处理方式:

在 JavaScript 中:

function myTimer()
{
$.ajax({
    url:'path/to/your-script.php',
    type:"POST",
    data:"ajax=true",
    success:callBackMyTimer
});
}

function callBackMyTimer(data)
{
alert(data);
}

在你的脚本.php

<?php
if(isset($_POST['ajax']) && $_POST['ajax'] == 'true')
{
    session_start();
    $userdata = $_SESSION['views'];
    $name =  $userdata[1]; 
    $mysql_host = 'localhost';
    $mysql_user = 'root';
    $connection = mysql_connect($mysql_host,$mysql_user);
    mysql_select_db("twitter2", $connection);
    $query = "SELECT * FROM `logindata`";
    $result =mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result))
    {
        echo $row['loginname'];
    }
    echo "<br>";echo "<br>";
}
exit;
?>
于 2013-07-15T19:59:52.613 回答
0

javascript是一种客户端语言。您无法使用 javascript 打印出一些 php,因为它发生在客户端浏览器中。

您需要在服务器上执行此操作,因此我认为您想要的是对服务器端 php 脚本的 ajax 请求(XMLHttpRequest),该脚本看起来像您的 document.write 的内容。

一个非常简单和不错的跨浏览器方法是使用 jquery.ajax。 http://api.jquery.com/jQuery.ajax/

简单教程:http: //viralpatel.net/blogs/jquery-ajax-tutorial-example-ajax-jquery-development/

于 2013-07-15T19:49:37.833 回答
0

使用 Ajax 结合客户端和服务器端语言!

于 2013-07-15T19:56:43.553 回答