Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Why doesn't this work? (Javascript&PHP)

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Why doesn't this work? (Javascript&PHP)

Postby Derbstep » Wed Oct 03, 2012 1:49 pm

Why does it put the same number over and over, why doesn't it generate a new number on every interval?
It generates a new number when I refresh the page, but not a new one every interval.

Code: Select all
<?php
    function AddNewValue(){
        $random = rand();
        echo $random;
    }
?>

<script type="text/javascript">

function doChange(){
    var e = document.getElementById("test");
    e.innerHTML = e.innerHTML + "<?php AddNewValue(); ?><br />";
}
setInterval(doChange, 1000);
</script>

<div id="test"></div>
Derbstep
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed Sep 26, 2012 3:46 pm

Re: Why doesn't this work? (Javascript&PHP)

Postby kyle04 » Wed Oct 03, 2012 3:03 pm

The javascript part is looping only, not the php generated number.
You can do this with javascript only :

Code: Select all
<script language="javascript" type="text/javascript">
function randomString() {
   var chars = "0123456789";  // numbers only
   var string_length = 8;  // number string length
   var randomstring = '';
   for (var i=0; i<string_length; i++) {
      var rnum = Math.floor(Math.random() * chars.length);
      randomstring += chars.substring(rnum,rnum+1);
   }

var e = document.getElementById("test");
    e.innerHTML = e.innerHTML + randomstring + "<br>";

}

setInterval(randomString, 1000);

</script>
<div id="test"></div>
kyle04
New php-forum User
New php-forum User
 
Posts: 94
Joined: Sat Jul 07, 2012 1:36 pm

Re: Why doesn't this work? (Javascript&PHP)

Postby Derbstep » Wed Oct 03, 2012 3:08 pm

I realize there is a way to do it with only javascript, I was just wondering if there is a way I can make the php generated number change too?
Derbstep
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed Sep 26, 2012 3:46 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.