Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AJAX IE Form data loading problem

    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 .

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Try posting your code. It might be the way you're implementing your code.

    Quote Originally Posted by A1ien51

    The correct order with IE is

    open()
    onreadystatechange
    send()

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

    Eric
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Internet Explorer Here is my JS AJAX Code

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

  • #4
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AJAX Form Submit

    I have used AJAX Form Submit to fix this problem . Here is the code i modified
    Code:
    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?

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    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:
    Code:
    formdata = formdata + thisform.elements[i].name
      -to-
    formdata += thisform.elements[i].name
    Last edited by rwedge; 08-01-2007 at 07:53 AM. Reason: shorten

  • #6
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why ?

    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

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    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
    Code:
        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);
    Last edited by rwedge; 08-02-2007 at 08:48 AM.

  • #8
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes , it's works now . Thanks
    Last edited by HoangLong; 08-04-2007 at 06:24 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •