469,604 Members | 2,318 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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

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 19 '06 #1
5 2133
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/
Dec 19 '06 #2
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.

Dec 19 '06 #3
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 '06 #4
st*********@gmail.com 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/
Dec 20 '06 #5
On 19 Dec 2006 08:08:31 -0800, in comp.lang.javascript "libsfan01"
<mc******@googlemail.com>
<11*********************@a3g2000cwd.googlegroups.c omwrote:
>| 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.
| >
| >
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Dec 20 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Wilfred Nilsen | last post: by
10 posts views Thread by Peter Dragun | last post: by
5 posts views Thread by Peter Michaux | last post: by
1 post views Thread by geevaa | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.