Kispy
Kispy

Reputation: 1

Javascript Jquery code just works the first time, next time it doesnt

I am a beginner in all Javascript stuff and the code is making me crazy. I'm using Jquery

I have a div:

<div id="dicesDiv"></div>

This div is load at the beginning directly with this code:

$("#dicesDiv").load("dices.php?dice1=<?php print ($lastDices[1])?>&dice2=<?php print
($lastDices[2])?>");

I have a link to reload this div when clicking it:

<a href = "javascript:void(0);" onclick = "javascript:rollDice();"><img src="../images
/tirar_dados.png"/></a>

This is all my javascript stuff:

<script>
......

function rollDice()
{
    <?php $lastDices[1] = rand(1,6);?>
    <?php $lastDices[2] = rand(1,6);?>
   $("#dicesDiv").load("dices.php?dice1=<?php print ($lastDices[1])?>&dice2=<?php print
   ($lastDices[2])?>"); 
}
</script>

When I click the first time, it works, but when I click later it doesn't work. It doesn't make sense to me.

Do you know what is happening??

Thanks ^^

Upvotes: 0

Views: 120

Answers (1)

SLaks
SLaks

Reputation: 887453

Your PHP code executes once, when you first load the page.

Therefore, every click uses the same numbers.
Instead, you should generate the numbers in Javascript by writing

1 + Math.floor(Math.random() * 6)

Upvotes: 3

Related Questions