Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
1  
javascript runs on the client, php runs on the server. what you want is impossible unless you other methods, such as AJAX calls. –  Marc B Jul 15 '13 at 19:55
add comment

4 Answers

up vote 0 down vote accepted

javascript is a client side language. You cannot print out some php with javascript because it happens in the clients browser.

You need to do it on the server, so I think what you want is an ajax request (XMLHttpRequest) to a server-side php script which look like the content of your document.write.

A very simple and nice cross-browser way doing this is to use jquery.ajax. http://api.jquery.com/jQuery.ajax/

simple tutorial: http://viralpatel.net/blogs/jquery-ajax-tutorial-example-ajax-jquery-development/

share|improve this answer
add comment

You doin it wrong - the content of your JavaScript function is interpreted on the server and always the same. Your JavaScript function have to send a request to the Server and handle the response.

If you use JQuery this could look i.e. like this

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

and inside script.php, you could output the database results

share|improve this answer
add comment

Use Ajax to combine client and server side languages!

share|improve this answer
add comment

steven is right, here is how I would do with jquery:

In javascript:

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

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

In your-script.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;
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.