Nov 21, 2009

Update Part Of A Page Without Refreshing? - is it possible to refresh only part of the webpage not all

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > PHP Programming

Update Part Of A Page Without Refreshing? - is it possible to refresh only part of the webpage not all

mandla
hi all

I have been scratching me head for days now tryna find a way around this. basically i will try and explain as much as i can so its clear what I require. I built a website which is database based for a community internet radio station I'm trying to put together. because the djs are volunteers they do no have set time every week so writting a script to display the current deejay would no be right because another volunteer might turn up on the day. and it would mean correcting the script weekly. Instead I have collected their images and put these in a file. They have been give login names and when the log in to the site they change the status of their account to On Air and this means the MYSQL database will pick the name of the dj logged in and set to online because if we set automatic status change it would be wrong just incase they are logging in only to change the program synopsis or info in preperation for their next show. So this means they manually have to change their status to achieve this. With this process things work fine for anyone coming to the site after the SQL info has been changed other wise they are stuck with a picture of the previous dj on the screen.

(obvioulsy the picture only covers a quarter of the screen and theres info about the show playing. Say you tuned in at 950 and where listening and another dj come on at ten the image does not automatically change to the next. I know your thinking why dont he just use page refresh every say 10 minutes. Well I did think of that but then we have mini chat window on the home page and people use this to chat with fellow listeners so it would be an inconvenience for them to have to re-login because the META REFRESH has kicked in every 10 or so minutes.

I also thought I'd use a <iframe or framset and set that page to diplay the program info and picture and have that page refresh every 10 minutes which would have been perfect but when I used iframe everything after the area witht frameet /iframe disappeared. Is there a little script I can use to place inside the area to display picture and text from the database and have just info refresh every 10 minutes without affecting the rest of the page.

if anyone knows how please kindly let me know

thansk a lot in advance

 

 

 


Comment/Reply (w/o sign-up)

galexcd
Your best bet would be to use AJAX. Basically it is a way that javascript can send and receive information from a web-server without having to reload the page. It will let the image and information change without any visual indication to your users that the page is loading any data. What I would recommend is make a new file on your server, (PHP, ASP, or whatever server-side programing language you prefer). And have this file print out the information that you want ajax to check. For example, include the URL to the image, and whatever other data you might want to update, like a name.

If it's more than just a URL to an image we will need javascript to parse the information from the file, so I would recommend using some symbol that will not be part of the name, url, or any of the other data being sent. Usually when I do something like this, I like to use a semi-colin.

Once you've got that page up and running it should output something like this:
CODE
http://www.example.com/images/djays/jsmith.png;John Smith;John Smith is a good dj


Note how I have separated the image url, name, and description with semi-colins.


Now we'll want to create some tags around the data that we want to change so javascript can modify it easily. I personally love using div layers to do this, but any tag that can handle the id attribute should be fine.
We also want to add a name attribute to the image tag so we can change it on the fly as well.

For Example:
HTML
<img name="djimg" src="<? echo $dj_img; ?>">
...
<div id="djname"><? echo $dj_name; ?></div>
...
<div id="djdesc"><? echo $dj_desc; ?></div>


Now let's write some javascript. For now, I'm just going to make an empty function called getData. This is where the ajax will go. You want to have javascript run this function periodically (say about once or twice a minute), and replace the image, name, and description. I'm going to add a timeout so it calls itself every 500 milliseconds. I'm also going to make a new function called update that replaces the data in the div layer we created earlier. We will have the getData function call this one when there is new information.

CODE
function getData(){

  /* ajax will go here */
  
  return 0;
settimeout("getData()",500);
}
function update(value){
var data=value.split(";");
if(document.images) document["djimg"].src=data[0];
document.getElementById("djname").innerHTML=data[1];
document.getElementById("djdesc").innerHTML=data[2];
}
window.onload = getData;


Now let's write the ajax:
CODE
var page="http://www.example.com/data.php";
while(getv!=-1){}
if (window.XMLHttpRequest) {
    getv= new XMLHttpRequest();
    if (getv.overrideMimeType){
        getv.overrideMimeType("text/xml");
    }
}else if (window.ActiveXObject) {
     try {
        getv = new ActiveXObject("Msxml2.xmlHttp");
        getv.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }catch (e) {
        try {
            getv= new ActiveXObject("Microsoft.xmlHttp");
        }catch (e) {}
    }
}
ret=-1;
getv.onreadystatechange = function(){
        if (getv.readyState == 4) {
            ret=getv.responseText;
            update(ret);
            getv=-1;
        }
    };
    getv.open("POST", page);
    try{
        getv.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }
    catch(e){}
    getv.send("");
    return ret;



And now to put it all together:
HTML
<img name="djimg" src="<? echo $dj_img; ?>">
...
<div id="djname"><? echo $dj_name; ?></div>
...
<div id="djdesc"><? echo $dj_desc; ?></div>
...
<script type="text/javascript">
var getv=-1;
var page="http://www.example.com/data.php";

function getData(){

while(getv!=-1){}
if (window.XMLHttpRequest) {
getv= new XMLHttpRequest();
if (getv.overrideMimeType){
getv.overrideMimeType("text/xml");
}
}else if (window.ActiveXObject) {
try {
getv = new ActiveXObject("Msxml2.xmlHttp");
getv.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}catch (e) {
try {
getv= new ActiveXObject("Microsoft.xmlHttp");
}catch (e) {}
}
}
ret=-1;
getv.onreadystatechange = function(){
if (getv.readyState == 4) {
ret=getv.responseText;
update(ret);
getv=-1;
}
};
getv.open("POST", page);
try{
getv.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}
catch(e){}
getv.send("");
return ret;

return 0;
settimeout("getData()",500);
}
function update(value){
var data=value.split(";");
if(document.images) document["djimg"].src=data[0];
document.getElementById("djname").innerHTML=data[1];
document.getElementById("djdesc").innerHTML=data[2];
}
window.onload = getData;
</script>


Let me know if it doesn't work or if you have any questions.

 

 

 


Comment/Reply (w/o sign-up)

dolrich06
You could also try JQUERY.

Here is a sample script.

Link: http://docs.jquery.com/Ajax/jQuery.post

1. Include the jquery framework "
CODE
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"/>


2. Then you request like this

CODE
<script type="text/javascript">
  $(document).ready(function() {

        setTimeout(function() {

            $.post("get_dj.php", { dummy: "" },

               function(data){

                   $("#dj_image").html('<img src=\''+data.image+'\' />');

                   $("#dj_name").html(data.name);

                   $("#dj_description").html(data.description);

               },"json");

         }, 2000);

   }
</script>


3. For the html code

CODE
<div id="dj_image"> </div>

<div id="dj_name"> </div>

<div id="dj_description"> </div>


4. For the php code

CODE
<?php

  $data['dj_name'] = 'sample name';
  $data['dj_image'] = 'sample.jpg';
  $data['dj_description'] = 'sample description';

  echo json_encode($data);

?>


Ill add this later, will be late for my job. biggrin.gif
If you have any questions or need help in coding, just pm me.

Comment/Reply (w/o sign-up)

k_nitin_r
Hi!

As an alternative to using AJAX, you could also use IFRAMES. Using IFRAMES is simpler if all you want is to put a form on a page and display a message after the form has been submitted.

If you do decide to go the AJAX route, there are several Javascript libraries to pick from - the Prototype Javascript library is popular among older projects while jQuery is more popular among newer projects.

Comment/Reply (w/o sign-up)

contactskn
Thanks dear even I was searching for something related to this topic. And the information given by you dear friend is really good and fruitfull. But previously I was using the traditional way where it was clearly displayed on the screen that the data is being refreshed. But it is the technique even I was searching for. 

Thanks dear .



Comment/Reply (w/o sign-up)

(G)Andrew
Using your code, page does not refresh
Update Part Of A Page Without Refreshing?

I'm very interested in this AJAX/Javascript script. I duplicated it and it is loading the data correctly on the initial load but won't reload the data every 500ms. Is there anything you could think of that could be causing this?

-question by Andrew

Comment/Reply (w/o sign-up)

Raidation
You might want to use an iframe.
You can use javascript to refresh that iframe.
CODE
document.getElementById("iframeid").src=document.getElementById("iframeid").src;


Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords :

  1. Sending E-mails After News Update?
    Coding needed?!? (8)
  2. Concurrency Problem?
    update together in the same time (1)
    I have a problem in PHP. When more than one client update something in database, they won't
    work. so, how do I make out for it? example: at the same time, in real-time chatboard, 2 persons
    click CHAT, what can I do so that both of them actually chat? or at bidding website, when 2 persons
    bid at same time, how could I do that? thanks for answer.....
  3. Mysql Won't Update
    (5)
    Ok, so I'm making some arcade administration software so I don't have to add games the hard
    way, well anyway, one of the features I have is that on the homepage is a featured author and as
    such I just have it so it updates one line in the mySQL. Well, the page with the form passes on the
    varibles fine and the updating page recieves them because I echoed everything, then I build the
    query and I even echo the query and it comes out exactly how I want it to, but for some reason, the
    database itself won't update even though all the varibles are there. Here's....
  4. Php Update
    (4)
    Hey guys, I'm currently making my own online PHP game, and I'm smack into the middle of the
    coding. The game is an industry/economy type game where you start out with some initial resources
    and a building and you expand your business empire by building more buildings that produce more
    buildings, which can be bought and sold on the live market. Basically, you try and become the
    wealthiest player in the game by the time the round ends. My problem is, I need my game to update
    hourly and not hourly. Meaning, I have codes that update my MySQL database when a user bu....
  5. Edit .txt File In Ftp Via Webpage
    file on external ftp (2)
    Right Im new here and stuggling with a problem im having. I've currently got a Login Script to
    login to a external ftp and it displays the folders contents, however i need it so it echos a
    specific file (coursedata.cfg) into a formarea which you can then edit the file and when u click
    save it overwrites the file with the new one. Im really quiet getting annoyed with it xD as
    everything i do ends up trying to include the file from the webserver the script is hosted on and
    not the external ftp source. thanks Full Code Bellow Simple FTP Manager body { fon....
  6. Refresh Problem
    On Refreshing a Submitted Form (3)
    Hi, I have great problems about refreshing a page having a Form. When anyone refreshes a submitted
    form it gets INSERTED Twice in the DB. Is there any strong way to stop this. I know two ways to do
    it using SESSIONS or redirecting. But its not that Firm. In firefox it does send it again. Also my
    use is that , that the user may want to use the form again. Please help me. Thanks and have a good
    day.....
  7. Why Do I Have To Force A Refresh?
    (10)
    This may or may not be PHP related; I am THAT much of a newbie. It seems like I need to force my
    browser to NOT use a cahced view of pages I create using PHP. Is there any way I can make it sound
    refresh every time the page is loaded?....
  8. Mysql Update Everything
    Bit confusing (4)
    i need to make a script that will take a number from every record in a mysql table, takeaway 1 from
    them all then replace them all in the right places. so if it looked like Record 1 5 Record 2 8
    Record 3 9 then i exectue the script then it turns into: Record 1 4 Record 2 7 Record 3 8 i
    cant think of a way to do this so if you can help id be very greatful. Thanks in advance Andre....
  9. Refreshing Images
    (1)
    I've got a script that generates a graph by creating it as a .png file (I can't output the
    image directly to the browser because of the way I need to generate the data). This all works ok
    however, in order to keep the data managable I've set a limit so that it only displays 12 months
    data at a time. The problem I have is that when I click on the next button to display the next set
    of data, the graph remains the same until you refresh the browser. I've tried using unlink() to
    delete the image file forcing the script to re-generate it when the script is r....

    1. Looking for Update, Part, Of, A, Page, Without, Refreshing?

Searching Video's for Update, Part, Of, A, Page, Without, Refreshing?
See Also,
advertisement


Update Part Of A Page Without Refreshing? - is it possible to refresh only part of the webpage not all

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com