469,289 Members | 2,215 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

How to renew information from MySql database without page refresh? using jQuery/AJAX

ilya Kraft
134 100+
I guess it is sort of simple question, but I'm not the AJAX guy, tried it, but I still don't understand it )))

Here is the situation:

1) I get one random url from database

Expand|Select|Wrap|Line Numbers
  1. $sql = ("SELECT url FROM url_table ORDER BY RAND() LIMIT 1");
  2.  
  3.        while($row = mysql_fetch_array($sql)){
  4.  
  5.              $url = $row["url"];
  6.  
  7.     }
2) Than I display website that has url = $url in iframe

Expand|Select|Wrap|Line Numbers
  1. <iframe src="<?php echo "$pagination"; ?>"></iframe>
3) Then I have
Expand|Select|Wrap|Line Numbers
  1. <button id="Next url">
which when clicked refreshes the website, so new random url is selected from database and displayed in <iframe>

What I want to do is same process, but without page refresh. Ideally process should go like this:

User clicks the "Next Button" >> New random $url get's selected from database >> after that new url is echoed in iframe (
Expand|Select|Wrap|Line Numbers
  1. <iframe src="<?php echo $url?>"
) >> iframe shows new website.

All of that without page refresh (using AJAX and jQuery if possible).And if possible could you please suggest how to show loading.gif image in iframe while information is getting renewed and loaded

Thank You )))
Oct 6 '11 #1
1 2839
johny10151981
1,059 1GB
use $.ajax(...) function

and on success do the required action.. here is an example

Expand|Select|Wrap|Line Numbers
  1. function getStaticAction(Url, parameter, resFunction)
  2. {
  3.     $.ajax(
  4.     {
  5.     url: Url, 
  6.     type: 'POST', 
  7.     dataType: 'html', 
  8.     data: parameter, 
  9.     timeout: 90000, 
  10.     success: function(data)
  11.     {                                 resFunction(data);
  12.                     }
  13.             });
  14.     }
  15. }
  16.  
  17.  
If you returned page have javascript then you should do this (an example of resFunction in above code)
Expand|Select|Wrap|Line Numbers
  1. function resFunction(text)
  2. {
  3.  $("#TargetDivOrId").html(text);
  4. }
  5.  
Oct 7 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by cocoalearner | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.