Fash Footwear
Fash Footwear

Reputation: 39

PHP - Get random number by clicking button

i got two php pages, one generating random numbers and another displaying on refresh page.

i want to get current random value by clicking not refreshing.

we could use javascript / ajax / jquery for that please help me any best way

1.php

<?php
 include("2.php");

 $getnumber=$_SESSION['randomnumber'];

 ?>

 <button class="button" type="button" name="buttonpassvalue" >Get Random Number</button> 

2.php

 <?php
  $rannumber=rand();

   $_SESSION['randomnumber']=$rannumber;
   ?>

thanks for your help.

Upvotes: 1

Views: 7178

Answers (2)

BlackHatSamurai
BlackHatSamurai

Reputation: 23493

You can do something with JQuery/Javascript, which won't refresh the page by doing:

<script>
        jQuery(document).ready(function($){
             $("#random").click(function(){
             var number = 1 + Math.floor(Math.random() * 6);//Change the 6 to be the number of random numbers you want to generate. So if you want 100 numbers, change to 100
                 $("#number").text(number); 

        });

        });


</script>


            <button class="button" type="button" name="buttonpassvalue" id="random" >Get Random Number</button> 
        <div id="number"></div>//<---This is where the random number would be placed

I have tested this and it works :)

Example is here: http://jsfiddle.net/Q5uKe/

Upvotes: 0

zerzer
zerzer

Reputation: 602

this could be used without any php coding, whenever want something to happen without refreshing the page , javascript is your buddy

<div id="randomNum">0</div>
<button onclick="generate()">Randomize</button>
<script>
function generate(){
    var x=document.getElementById("randomNum");
    x.innerHTML=Math.floor((Math.random()*10)+1);
}
</script>

i suggest you take a look over here http://www.w3schools.com/jsref/jsref_random.asp

Upvotes: 2

Related Questions