tags:

views:

109

answers:

2

Hi,

I was wondering if someone could point me in the right direction for this:

I have a MySQL database with and id and a text string, i want to be able to display it, and with the click of a button display another random phrase without having to refresh the whole page.

I have look quite thoroughly and have no found no answer for this concrete question.

Is it possible to do it with PHP (i guess so).

Can somebody guide me towards the answer, I am kind of lost!

Thanks in advance!

Trufa

+1  A: 

First try it WITH refresh.

You'll need to select a random text from your database (hint, use RAND() in your mysql request).

Once you know how to do that, learn how to make Javascript talk to your php page so you no longer need refresh. It's called AJAX, you can look at JQuery ( http://jquery.com/ ) for a library that will help you with it and specifically this page :

http://api.jquery.com/jQuery.ajax/

Your javascript will do a Ajax call to your php page, will get some data back and then will be able to display it in your page.

Look at the example, you should be able to do it from there.

But first do it with refresh, it's a first step.

Loïc Février
I have all ready done it (with refresh), I had no problem with it. I will read the jQuery documentation! Thank you!
Trufa
A: 

If i were you i would use http://api.jquery.com/jQuery.get/ Create a page where you do the mysql query and then write a few lines of jquery to get the information from that specific page. You won't have to refresh the page and there are plenty of neat ways to change between the data you get from the database, with jquery

something like:

  $.get("the_separate_page.php", function(data){
   console.log('Your quote is : ' + data);
   //check your log
 });
andrei
I really like this idea thank you!! I will try it and comment later but I appreciate it!
Trufa
@Trufa np, just say if you have any more problems with it
andrei