| | |
Rookie having problem with 2 consecutive Ajax calls: 1st message lost
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 35
Reputation:
Solved Threads: 0
Hello, I'm new at Ajax and Javascript. Can anyone help me on this?
I'm calling 2 Ajax funcions, one after the other, with a delay time in between.
The first call executes a script that inserts a row in a MySQL database.
The second call execustes a cript that lists what's in the database.
What I'm losing is the response from the first script saying a row has been inserted, for instance. It should appear in a div named "message".
Seems the response from the second script overrrides it, although it goes to another div named "rows".
What I'm trying now is storing the response from the first call in a variable named "reply" and then showing it in the div, along with the reply from the second call. But it's not working (shows as "undefined"), and I don't know why... any help very appreciated.
I'm calling 2 Ajax funcions, one after the other, with a delay time in between.
The first call executes a script that inserts a row in a MySQL database.
The second call execustes a cript that lists what's in the database.
What I'm losing is the response from the first script saying a row has been inserted, for instance. It should appear in a div named "message".
Seems the response from the second script overrrides it, although it goes to another div named "rows".
What I'm trying now is storing the response from the first call in a variable named "reply" and then showing it in the div, along with the reply from the second call. But it's not working (shows as "undefined"), and I don't know why... any help very appreciated.
javascript Syntax (Toggle Plain Text)
var xmlhttp; var reply; function insertList(str) { insert(str); setTimeout("list()", 100); } function list() { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser too old"); return; } var url="list.php"; url=url+"?sid="+Math.random(); xmlhttp.onreadystatechange=stateChangedList; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function insert(str) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser too old"); return; } var url="insert.php"; url=url+"?nom="+str; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChangedInsert; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChangedLlist() { if (xmlhttp.readyState==4) { document.getElementById("rows").innerHTML=xmlhttp.responseText; <------ THIS MESSAGE SHOWS OK. document.getElementById("message").innerHTML=reply; <---- THIS IS WHAT I'M TRYING NOW BUT IT'S NOT WORKING. SHOWS AS "undefined" } } function stateChangedInsert() { if (xmlhttp.readyState==4) { document.getElementById("message").innerHTML=xmlhttp.responseText; <----- THIS MESSAGE IS LOST. reply=xmlhttp.responseText; <----- THIS IS WHAT I'M TRYING NOW BUT IT'S NOT WORKING. } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; }
The simple, and conventional, solution would be to make just one AJAX call with one response containing all the info you need to display as feedback to the user (ie a confirmation message that the insertion has been made, plus the list of what's in the database).
Two calls with two responses seems to add unnecessary complexity and uncertainty.
You may need to use JSON to encode your data server-side and to decode client-side. That's what JSON does. Google "JSON" for a more detailed description/tutorial.
Javascript is easily capable of handling the two parts of the decoded response separately - eg. displaying one part in one div and the other part in another div.
Airshow
Two calls with two responses seems to add unnecessary complexity and uncertainty.
You may need to use JSON to encode your data server-side and to decode client-side. That's what JSON does. Google "JSON" for a more detailed description/tutorial.
Javascript is easily capable of handling the two parts of the decoded response separately - eg. displaying one part in one div and the other part in another div.
Airshow
50% of the solution lies in accurately describing the problem!
![]() |
Similar Threads
- problem with understanding ajax basics (JavaScript / DHTML / AJAX)
- ajax server-returned message? (JavaScript / DHTML / AJAX)
- Ajax calls wont work in FireFox! (JavaScript / DHTML / AJAX)
- simple ajax script problem (JavaScript / DHTML / AJAX)
- Simulate Mouse Move (C++)
- Problem accessing User Accounts (no error message) (Windows NT / 2000 / XP)
- Page Cannot Be Displayed & javascript:doNetDetect() Errors (Viruses, Spyware and other Nasties)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Generated Table Rows
- Next Thread: Issues with javascript in IE6
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array browser captcha captchaformproblem cart child class close codes column css date debugger decimal dependent design disablefirebug dom download editor element embed engine enter error events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe index java javascript javascripthelp2020 jquery jsf jsp jump libcurl listbox maps masterpage math media menu mp4 object onerror onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php position post problem programming prototype rated rating redirect safari scale scriptlets scroll search security select software star starrating stars synchronous toggle unicode variables w3c web webservice \n





