#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    32
    Rep Power
    10

    posting text to a page element


    Hello. I am in need of some help. I am trying to display a line of text in a specific location on my page, and them read a new line of text form my database, and replace the old text with the new text.. and on until the end of the table is reached. I am not having a problem getting the data.. but i need to know how, using javascript.. that i can place the text into that one element, without refreshing the web page. There are other things going on in the web page, and i only want that one single element updated with the new text each time the database is read. The scrip i am posting, is included in the page, and is only a portion of what is going on.

    Here is the code so far..And please remember, this is a include file, not the entire set of code..

    PHP Code:
    $queryname = "SELECT tsid FROM text_save WHERE myprog='$textname'";
    $resultname = mysql_query($queryname) or exit(mysql_error());        
    if($rowname = mysql_fetch_array($resultname, MYSQL_ASSOC)) {  
    $_SESSION['textid'] = $rowname['tsid'];
    }
                                        
    $tsid = $_SESSION['textid']-1;
    ?>
                                        
    <span id="textline">&nbsp;</span>
                                                                        
                                        
    <?php
    $query 
    "SELECT * FROM text_lines WHERE tid='$tsid'";
    $result mysql_query($query) or exit(mysql_error());
    $timepause 6;
                                        
                                        
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)) {  
                                        
    $textline $row['text'];
    ?>
    <script>
    document.getElementById("textline").innerHTML = $textline;
    </script>
    <?php
                                                
     
    }    } ?>
    I still haven't gotten the timer worked out yet, but i need to be able to set a delay time in seconds, and only ready and display that line of text every X number of seconds. I need to display the text in the <span> element. I need ONLY this element to be updated, and the rest of the webpage to not be disturbed, thusly I am looking at javascript to update only that one <span>.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,288
    Rep Power
    190
    You need AJAX to get the data from the server-side page. Once you have created an AJAX routine; you then will add it to a function. Then you will add a setTimeout or a setInterval to the function (this will be your timing event / timer).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    32
    Rep Power
    10
    "need AJAX to get the data from the server-side page"? I have no experience with ajax. Also, I was able to do this twice before without "ajax", however, that was too long ago and i cant find my examples. If you can show me what you mean, maybe i can understand. However, as I said, I was able to do it before without ajax, but I have no memory of how i did it.
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,288
    Rep Power
    190
    Not sure how you did it twice before without AJAX, especially if it was a long time ago, but you need AJAX for what your wanting to do. Maybe that is what you had before; you just did not know what it was called , because AJAX is JavaScript
    ..., its just HTTP interaction specific. At any rate..., just do an online search for "AJAX" and you will get plenty of detailed examples of what it is, how it works and how to implement it.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    938
    Rep Power
    281
    the OP probably used the XMLHttpRequest method....which is AJAX [before the AJAX term was "coined"]

    I'm wondering why the OP isn't opting to use jQuery to accomplish all of this? [so much less to write]
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    32
    Rep Power
    10

    how i did it before?


    Ok, lets see.. Here is an example..

    Code:
    <html>
    
    <head>
    
    <title>CLOCK TEST</title>
    <script src="includes/clock.js" type="text/javascript"></script>
    </head>
    
    <body onload="updateClock(); setInterval('updateClock()', 1000 )">
    <span id="clock">&nbsp;</span>
    </body>
    
    </html>
    And here is the javascript..

    Code:
    // JavaScript Document
    window.onload = init;
    
    function init() {
    updateClock();
    }
    
    function updateClock() {
        var currentTime = new Date ( );
        
        var currentHours = currentTime.getHours ( );
        var currentMinutes = currentTime.getMinutes ( );
        var currentSeconds = currentTime.getSeconds ( );
        
        currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
        currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
        
        var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
        currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
        currentHours = ( currentHours == 0 ) ? 12 : currentHours;
        
        var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + "" + timeOfDay;
        
        document.getElementById("clock").firstChild.nodeValue = currentTimeString;
    }
    As far as i know, that's not ajax, is it?

    Now, I need to do the same thing as in the clock example, except, I don't want to load and reload a page.. I simply want to write to the document element.
  12. #7
  13. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,288
    Rep Power
    190
    You using the date() object, in the example above. This is a global object, that gets the current time/date from your computer. What your wanting to do; is have JavaScript interact with a server-side page... and... for this you need to use code that will communicate between front-end and back-end code. There are a few methods of getting this to occur, but in my opinion and more then likely.. , many other developers would agree with me; in saying that AJAX is the overall best way to implement, what your wanting to accomplish.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    32
    Rep Power
    10

    learn rather than be helped..


    Originally Posted by web_loone08
    You using the date() object, in the example above. This is a global object, that gets the current time/date from your computer. What your wanting to do; is have JavaScript interact with a server-side page... and... for this you need to use code that will communicate between front-end and back-end code. There are a few methods of getting this to occur, but in my opinion and more then likely.. , many other developers would agree with me; in saying that AJAX is the overall best way to implement, what your wanting to accomplish.
    So.. in other words.. "Sorry, we wont help you, go spend a month to a year learning a new language, and do it yourself"..

    Thanks for the help.

    Comments on this post

    • hexman disagrees : Read the whole thread man, I think you need some understanding on the REAL-WORLD. No offense, just sayin ;)
  16. #9
  17. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,288
    Rep Power
    190
    Actually that's not at all what I said. I did help you; I pointed you in the right direction. I am not paid to write code for you; I volunteer my time free of charge here to help people. I am not going to do it for you; you need to take the initiative to do it on your own. Welcome to "Real World 101"; life is not about handouts. It would not take you more then a day; to try to learn how to do this; but instead of putting forth the effort to do a little research; you want to come here looking for someone else to do your work. Make an honest effort at trying to learn something and do not rely on others to do your work. I have guided you with the info you need; what you chose to do with that info is up to you.

    - Good Luck

    Comments on this post

    • ManiacDan agrees
    • Nilpo agrees : Brilliant!
    • hexman agrees : Good sayin' man!
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    32
    Rep Power
    10
    Allright then. I will see about deleting my account from this forum, and will advise everyone to stay away form here.
  20. #11
  21. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,288
    Rep Power
    190
    That might be for the best.
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    Jakarta, Indonesia.
    Posts
    279
    Rep Power
    38
    Originally Posted by iceregent
    Allright then. I will see about deleting my account from this forum, and will advise everyone to stay away form here.
    That won't be fair, iceregent. He had given you a push, and you gave this in return? Read again this sticky; you might have forgotten it.

    Here's my best resource that will get you started:
    https://developer.mozilla.org/en-US/...etting_Started
    Last edited by hdewantara; August 1st, 2014 at 12:54 AM.
  24. #13
  25. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,884
    Rep Power
    6356
    We were just discussing this type of behavior in the shoutbox chatroom.

    IceWhatever, you are asking experts for free work. Do you know how much some of the members would charge you if you called their business phone? Since you're not paying the experts you're utilizing, the amount of help you get is determined by those experts. They appear perfectly willing to discussing techniques, explain your misunderstandings, even describe entirely new methods of interacting with a computer application, and link you out to more resources.

    The thing they will not do for you is your work. Yes, you will have to learn AJAX in order to do real-time interactions between an HTML page and its corresponding web server. Too bad for you, but this is your project and nobody else's. If you had been cordial, you may have been informed that AJAX is a subset of javascript, and it would take an absolute AJAX novice maybe 15 minutes to get an AJAX example working if they already knew JavaScript (like you do).

    If you do decide to warn your friends, at least tell the whole story. This isn't poor customer service. You need to actually sacrifice something of your own to become a customer.

    Comments on this post

    • hexman agrees
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  26. #14
  27. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,320
    Rep Power
    1503
    Originally Posted by ManiacDan
    and it would take an absolute AJAX novice maybe 15 minutes to get an AJAX example working if they already knew JavaScript (like you do).
    You probably don't even need to know JavaScript. A simple Google search like this very quickly returned this gem.

    Best of all, I won't even charge for the four and a half seconds it took for me to find that. (It actually took longer to write this post.)

    iregent, you need to understand moving forward that we are offering our services here pro bono. That means that we reserve exclusive right to tell you kiss off any time that we feel like it. If you're not happy with that arrangement, feel free to offer to pay a professional to do it for you.

    I've been volunteering my expertise across a dozen forum sites for over a decade. I'm very happy to give my knowledge in order to help the thousands of others who have wished to learn. However, every once in awhile a blood sucking vampire like you comes along and makes me second guess what I do. Please take a minute to realize how selfish you are being.

    Take a look at the link I've given you and you should be on your way. If you get stuck, we're here to help out. But please come back with a different attitude. You're always welcome to be a productive member of our community.
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!

IMN logo majestic logo threadwatch logo seochat tools logo