...

View Full Version : AJAX IE Form data loading problem



HoangLong
07-28-2007, 06:50 AM
Hello , i'm writing a comment system for website , file name is comments.php , wheni use Firefox to post a comment (the comments page loaded to 'noidung' DIV ), it works OK ! But if i try in IE , , comments is posted to the MySQL DB but is not display , so i need to go to 'comments.php' and refresh it , then go back , the comment is displayed . Anybody helps me .

StupidRalph
07-28-2007, 06:53 AM
Try posting your code. It might be the way you're implementing your code.




The correct order with IE is

open()
onreadystatechange
send()

If you have onreadystatechange before the open, IE will not work.

Eric

HoangLong
07-30-2007, 04:40 AM
function LoadPage(page,usediv) {

// Set up request varible
try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");} catch (e) { alert("Error: Could not load page.");}

document.getElementById('loading_screen').style.visibility='visible';
//Element.hide('loading_screen');
Effect.Appear('loading_screen');
//window.setTimeout('Effect.Appear(\'demo-effect-fade\', {duration:.10})',2500);
scroll(0,0);
xmlhttp.onreadystatechange = function(){
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
document.getElementById(usediv).innerHTML = xmlhttp.responseText;
//document.getElementById('loading_screen').style.visibility='hidden';
Effect.Fade('loading_screen');
}
}
xmlhttp.open("GET", page);
xmlhttp.send(null);

return false;

}
Comments.php action : comments.php then reload wait

<?php
if ($_POST["submit"]=='Post')
{
//Insert to DB Code
?>
<script language="Javascript">
LoadPage('comments.php','noidung');
</script>
<?php
}
?>

HoangLong
08-01-2007, 02:12 AM
I have used AJAX Form Submit to fix this problem . Here is the code i modified

function ajaxform(thisform,formhandler)
{
var formdata = "";
try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");} catch (e) { alert("Error: Could not load page.");}
for (i=0; i < thisform.length; i++)
{
//Build Send String
if(thisform.elements[i].type == "text"){ //Handle Textbox's
formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
}else if(thisform.elements[i].type == "textarea"){ //Handle textareas
formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
}else if(thisform.elements[i].type == "checkbox"){ //Handle checkbox's
formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].checked + "&";
}else if(thisform.elements[i].type == "radio"){ //Handle Radio buttons
if(thisform.elements[i].checked==true){
formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].value + "&";
}
}else{
//finally, this should theoretically this is a select box.
formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
}
}

//Send Ajax Request
xmlhttp.open("POST", formhandler);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xmlhttp.onreadystatechange = function(){
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
document.getElementById("noidung").innerHTML = xmlhttp.responseText;
xmlhttp.send(formdata);


}
}
//xmlhttp.open("POST", formhandler);

return false;
}
Are the bold text corrects AJAX order for Internet Explorer?

rwedge
08-01-2007, 05:25 AM
In your "Send Ajax Request" part of the code you have the xmlhttp.send(formdata) inside the onreadystatechange function

As to the question of where to put the xmlhttp.open, I have used the open after the onreadystatechange for a couple of years with IE 5.5 to IE 7 with no problem.

The current MSDN site has open after the onreadystatechange in their example code.
The explaination of the code states that the onreadystatechange sets the handler readyState ( readyState = 0) and then the open initializes it ( readyState = 1 )

It would seem then, the open should be after the onreadystatechange function, as MSDN2 has it.

http://msdn2.microsoft.com/en-us/library/ms534308.aspx
http://msdn2.microsoft.com/en-us/library/ms534361.aspx


You could shorten these lines a little:
formdata = formdata + thisform.elements[i].name
-to-
formdata += thisform.elements[i].name

HoangLong
08-02-2007, 03:36 AM
Thanks but anybody tell me why Internet Explorer sends data from form ( Post to database ) then reload the page ! The data isn't display ? Firefox and other browsers is OK! Sorry for my bad English :)

rwedge
08-02-2007, 08:25 AM
Did you move the xmlhttp.send(formdata) out of the onreadystatechang function?
How are to calling the script? If with a form, is the submit button type 'button'? You do not need depend on return false.
What response does you php page return?

Try checking the status of the post

xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
document.getElementById("noidung").innerHTML = xmlhttp.responseText;
} else {
document.getElementById("noidung").innerHTML = 'Error: '+xmlhttp.status;
}
}
}
xmlhttp.open("POST", formhandler, true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(formdata);

HoangLong
08-03-2007, 04:46 AM
Yes , it's works now . Thanks :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum