Jump to content

Random statement script


Zola

Recommended Posts

Hello,

 

I have been using a random text script to display a useful tip to the user when they land on a certain page.

 

This is how the script looks:

 


      <?php
          $random_text = array("This is an example tip",
                                      
					"This is another example of a tip", 
								  
					"And another...", 
                                                        
                                                "And one more");
                                    
                                    $sizeof = count($random_text);
                                    $random = (rand()%$sizeof);
                                    print("$random_text[$random]");
                                    ?>
                                

 

 

At the moment the tip will display randomly everytime the page is loaded.

 

Is there any way I can add a small button that says "Give me another" or something, so that when they click it another random fact will come out?

 

If possible I would like to avoid them having to reload the page everytime.

 

Any help is greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/258984-random-statement-script/
Share on other sites

as PHP is run on the server, not the client, the only way you could do this would be to load the info into a javascript array and have an onClick event or use a form button and AJAX to reload the innerHTML of a div on your page.  once PHP has done processing that's it without a full page refresh.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.