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 3 of 3

Thread: Help please...

  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    92
    Thanks
    4
    Thanked 1 Time in 1 Post

    Help please...

    Hi,

    this is probably the most asked question of all those Ajax n00b's of which I gladly am one, but I really dont know what I am doing wrong here. I copied this example code and tried to use it but I am probably still doing something wrong.

    Anyway, here is my index.php :
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <
    title>Untitled Document</title>
    </
    head>

    <
    body>
    <
    script type="text/javascript" language="javascript">
        function 
    createRequestObject() {
        
           var 
    req;
        
        
    // Check whether or not the window.XMLHttpRequest object 
        // is recognised by the browser
           
    if(window.XMLHttpRequest){
              
    // Firefox, Safari, Opera...
              
    req = new XMLHttpRequest();
           } else if(
    window.ActiveXObject) {
              
    // Internet Explorer 5+
              
    req = new ActiveXObject("Microsoft.XMLHTTP");
           } else {
              
    // There is an error creating the object,
              // just as an old browser is being used.
              
    alert('Problem creating the XMLHttpRequest object');
           }
        
           return 
    req;
        
        }
        
        
    // Make the XMLHttpRequest object
           
    var http createRequestObject();
           
        
        function 
    sendRequestGet(act) {
                
            
    // Open PHP script for requests
            
    http.open('get''test.php?act='+act);
            
    http.onreadystatechange handleResponse;
            
    http.send(null);
        }
        
        
    // Start the PHP script using the POST method
        
    function sendRequestPost(act) {
            
    // Open PHP script for requests
               
    http.abort;
               
    http.open('post''test.php');
           
    http.setRequestHeader('Content-Type''application/x-www-form-
              urlencoded'
    );
           
    http.send('act='+act);
        }
        
        function 
    handleResponse() {
        
           if(
    http.readyState == && http.status == 200){
        
              
    // Text returned FROM the PHP script
              
    var response http.responseText;
        
              if(
    response) {
                 
    // UPDATE ajaxTest content
                 
    document.getElementById("ajaxTest").innerHTML response;
                 
    setTimeout(ajaxTest,20); 
              }
        
           }
        
        }

        function 
    displayAPSpilotID ){
            
    sendRequestGet('pilotID');
        }

        
    displayAPS);
    </script>
    <div id="ajaxTest">This is the test message that should get replaced
    </div>
    </body>
    </html> 
    Now I am trying to display the output of test.php into this ajaxTest div, this obviously doesn't work.... otherwise I won't be posting this.

    My test.php does the following (very complicated code, only PHP experts will understand it.

    PHP Code:
    <?php
    echo("test");
    ?>
    If I understood it all correct, the div's default text should get replaced by the output of the php script.

    Can someone help me here?
    Nemesis War
    Online browser based space combat game.
    It's free, it's great, you'll love it.
    Come join us at http://www.nemesiswar.net/

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    When you run it in the browser are you getting JavaScript errors. Use Friefox and install http://www.getFirebug.com

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New Coder
    Join Date
    Sep 2010
    Location
    New Jersey
    Posts
    21
    Thanks
    3
    Thanked 1 Time in 1 Post
    Ever heard of JQuery. Try using it and AJAX becomes more manageable.


  •  

    Posting Permissions

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