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

L

libsfan01

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
 
M

Martin Honnen

libsfan01 said:
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.
 
L

libsfan01

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.",";
}
?>
 
S

stellstarin

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();
}
 
R

Randy Webb

(e-mail address removed) 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.
 
J

Jeff North

| 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)) ?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top