473,395 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2337
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Wilfred Nilsen | last post by:
I am using the Mozilla XMLHttpRequest in multipart/x-mixed-replace mode such that the server can push data to the client. The problem that I encountered is that the onload event listener does...
1
by: Andy Wells | last post by:
I'm using VB.NET and I have an application that binds a schema to the main form's controls, and the user has the ability to load an XML file through the schema and into the bound form. My...
10
by: Peter Dragun | last post by:
I am generally new to programming under Unix, but know how to code under Windows. I have to create a simple program, that takes the following information from a file using redirection: 4 4 4 4 4...
9
by: MR | last post by:
I get the following Exception "The data at the root level is invalid. Line 1, position 642" whenever I try to deserialize an incoming SOAP message. The incoming message is formed well and its...
42
by: Greg | last post by:
Hi, I've designed a bookmark in Ajax / PHP that I will put soon on sourceforge.net. But I've got an very tricky bug. I try it on some computers with Internet Explorer/Windows, Firefox...
9
by: Phil_Harvey | last post by:
I am redoing my website and trying to get it to do something more exciting using Javascript. I did normal Java at university and code at work in VB.NET. I have got reasonably far into what I want...
5
by: Peter Michaux | last post by:
Hi, The FAQ correctly says the following: "Mozilla (NN6.2+, Firefox, Ice Weasle etc), Opera 7.6+, Safari1.2+, the Windows version of IE versions 5+, and some other browsers provide the XML...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
19
by: RossGK | last post by:
I'm a bit new to javascript - as will be obvious below. I'm using an XMLHttpRequest to get a bit of data from server (django), and it works nicely for single events. But my eventual outcome...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.