XMLHttpRequest gets data to display in element but data gets appended not replaces previous!

Discussion in 'Javascript' started by libsfan01, Dec 19, 2006.

  1. libsfan01

    libsfan01 Guest

    Hi all

    Im trying to write a script that pulls data from another page (which is
    getting data from a db). The contents displayed on the db handling page
    (display.php) gets transferred through XMLHttpRequest continuously,
    however when setting the value using getElementById the value of the
    element is not being replaced, it is being appended each time.

    Here is my code, how can i modify the code to replace the contents of
    the element (showmsgs) each time data is requested and sent from the db
    handling page (display.php):

    <html>
    <head>
    <script type="text/javascript">

    var get; //object to get from db
    var data; //contents of response

    function getdata() {

    get = new XMLHttpRequest();
    get.onreadystatechange = processdata;
    get.open("GET", "display.php");
    get.send(null);

    }

    function processdata() {



    if (get.readyState == 4) {
    data = get.responseText;

    document.getElementById("showmsgs").value = data;

    getdata();
    }


    }

    </script>
    </head>

    <body>

    <input type="button" value="button" onClick="getdata()"><br>
    <textarea id="showmsgs">
    </textarea>

    </body>
    </html>



    Thanks

    Marc
     
    libsfan01, Dec 19, 2006
    #1
    1. Advertising

  2. Re: XMLHttpRequest gets data to display in element but data getsappended not replaces previous!

    libsfan01 wrote:

    > Here is my code, how can i modify the code to replace the contents of
    > the element (showmsgs) each time data is requested and sent from the db
    > handling page (display.php):


    > if (get.readyState == 4) {
    > data = get.responseText;
    >
    > document.getElementById("showmsgs").value = data;
    >
    > getdata();


    > <input type="button" value="button" onClick="getdata()"><br>
    > <textarea id="showmsgs">
    > </textarea>


    I don't see anything in that code that would append or concatenate the
    response sent. Make sure your display.php is only sending the updated data.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 19, 2006
    #2
    1. Advertising

  3. libsfan01

    libsfan01 Guest

    Thats strange Martin

    Here is the code on the other page (display.php):

    <?
    mysql_connect("host", "user", "password");
    mysql_select_db("requestdb");
    $result = mysql_query("select * from convo ") or die(mysql_error());
    while ($row = mysql_fetch_array($result)) {
    extract($row);
    echo $message.",";
    }
    ?>



    > I don't see anything in that code that would append or concatenate the
    > response sent. Make sure your display.php is only sending the updated data.
    >
    >
     
    libsfan01, Dec 19, 2006
    #3
  4. libsfan01

    Guest

    hi there,

    In the function processdata(), the data fetched from some other page is
    assigned by the statement,
    document.getElementById("showmsgs").value=data;

    The thing is at the very first time,
    document.getElementById("showmsgs").value will NOT contain any
    data.Once u assign the value for the first time, it will be there.Hence
    for the second time u reuse that field with the same id the new data is
    appended with the previously assigned value.

    Solution:

    Before assigning the value in data to the element,"showmsgs"
    by the statment,
    document.getElementById("showmsgs").value=data;
    u initialise the value in the showmsgs to blankspace.




    function processdata() {
    if (get.readyState == 4) {
    data = get.responseText;
    document.getElementById("showmsgs").value = data;
    getdata();
    }



    libsfan01 wrote:
    > Hi all
    >
    > Im trying to write a script that pulls data from another page (which is
    > getting data from a db). The contents displayed on the db handling page
    > (display.php) gets transferred through XMLHttpRequest continuously,
    > however when setting the value using getElementById the value of the
    > element is not being replaced, it is being appended each time.
    >
    > Here is my code, how can i modify the code to replace the contents of
    > the element (showmsgs) each time data is requested and sent from the db
    > handling page (display.php):
    >
    > <html>
    > <head>
    > <script type="text/javascript">
    >
    > var get; //object to get from db
    > var data; //contents of response
    >
    > function getdata() {
    >
    > get = new XMLHttpRequest();
    > get.onreadystatechange = processdata;
    > get.open("GET", "display.php");
    > get.send(null);
    >
    > }
    >
    > function processdata() {
    >
    >
    >
    > if (get.readyState == 4) {
    > data = get.responseText;
    >
    > document.getElementById("showmsgs").value = data;
    >
    > getdata();
    > }
    >
    >
    > }
    >
    > </script>
    > </head>
    >
    > <body>
    >
    > <input type="button" value="button" onClick="getdata()"><br>
    > <textarea id="showmsgs">
    > </textarea>
    >
    > </body>
    > </html>
    >
    >
    >
    > Thanks
    >
    > Marc
     
    , Dec 20, 2006
    #4
  5. libsfan01

    Randy Webb Guest

    Re: XMLHttpRequest gets data to display in element but data getsappended not replaces previous!

    said the following on 12/20/2006 12:49 AM:
    > hi there,
    >
    > In the function processdata(), the data fetched from some other page is
    > assigned by the statement,
    > document.getElementById("showmsgs").value=data;
    >
    > The thing is at the very first time,
    > document.getElementById("showmsgs").value will NOT contain any
    > data.


    True.

    > Once u assign the value for the first time, it will be there.


    True again.

    > Hence for the second time u reuse that field with the same id the
    > new data is appended with the previously assigned value.


    Can you post sample HTML/Script that demonstrates setting the value of
    something appends the value instead of replacing it? Setting the value
    of an element doesn't add anything, it replaces it. Something else
    somewhere in the code is causing it to be added.

    Setting the value to "" is a hack around a problem, not a solution to
    the problem.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Dec 20, 2006
    #5
  6. libsfan01

    Jeff North Guest

    On 19 Dec 2006 08:08:31 -0800, in comp.lang.javascript "libsfan01"
    <>
    <> wrote:

    >| Thats strange Martin
    >|
    >| Here is the code on the other page (display.php):
    >|
    >| <?
    >| mysql_connect("host", "user", "password");
    >| mysql_select_db("requestdb");
    >| $result = mysql_query("select * from convo ") or die(mysql_error());
    >| while ($row = mysql_fetch_array($result)) {
    >| extract($row);


    You're not doing anything with this data.


    >| echo $message.",";


    Where does this come from? Is it supposed to be the result from the
    previous operation (extract($row)) ?

    >| }
    >| ?>
    >|
    >|
    >|
    >| > I don't see anything in that code that would append or concatenate the
    >| > response sent. Make sure your display.php is only sending the updated data.
    >| >
    >| >

    ---------------------------------------------------------------
    : Remove your pants to reply
    ---------------------------------------------------------------
     
    Jeff North, Dec 20, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    5
    Views:
    475
    John Harrison
    Jul 12, 2004
  2. Stefan Mueller
    Replies:
    5
    Views:
    541
    Steven Saunderson
    Jul 10, 2006
  3. Al Cholic
    Replies:
    18
    Views:
    290
    Al Cholic
    Jul 28, 2007
  4. Replies:
    0
    Views:
    209
  5. André Hänsel

    Appended input node is not connected to form

    André Hänsel, May 26, 2009, in forum: Javascript
    Replies:
    3
    Views:
    158
    David Mark
    May 27, 2009
Loading...

Share This Page