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: Add AJAX?

  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Add AJAX?

    How could i add AJAX to this page so that when the POST methos page is changed it will just get the new include instead of having to reload the page?

    Also if it can not work how my page is set up i am willing to change my page to make it work! So if anyone could help please do so!

    Thanks!

    PHP Code:
    <html>
    <head>
    <title>RACE</title>
    <link rel="stylesheet" href="style.css" type="text/css">
    <script type="text/javascript" src="javascript.js"></script>
    </head>
    <body>
    <?php
    # Common functions!
    session_start();
    include 
    'functions.php';
    connect();
    sessioncheck();
    lastaction();
    ?>
    <table align="center" cellspacing="5">
      <tr>
        <td align="right" width="175" style='vertical-align: top; padding: 0px;'><?php require_once 'menu.php'?></td>
        <td align="center" style='vertical-align: top; padding: 0px;'><?php if(isset($_GET['page'])): 
                                  
                                  
    $page $_GET['page'];
                                  
    $file_to_include $page ".php";
      
                                  if(
    file_exists($file_to_include)): 
      
                                  include(
    $file_to_include);

                                  else:
      
                                  
    header('Location: index2.php?page=news');

                                  endif;

                                  else:
      
                                  
    header('Location: index2.php?page=news');

                                  endif; 
    ?></td>
        <td align="left" width="175" style='vertical-align: top; padding: 0px;'><?php require_once 'stats.php'?></td>
      </tr>
    </table> 
    </body>
    </html>

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    There are a couple options you could explore. You could either add tags to your php so that when the server side code detects a post back event that it will only return the code within the body tag and on the client side just use document.body.innerHTML = myRequest.responseText in javascript or you could make a second page which is the "post" taget that returns the code to insert between the body tags.

    Either way this is not a typical form postback that you need to do. An example for what your looking for is as follows

    Javascript:
    Code:
    <script type="text/javascript">
       var myRequest = null;
       function doPostBack(){
           myRequest = new XMLHTTPRequest(); // There are several implementations of this object, you can lookup the detection script pretty much anywhere on here
           var myData = "myField1=" + $get("myField1").value + "&myField2=" + $get("myField2").value; // perform some url encoding on this puppy to fix the values to make sure they're safe
           myRequest.onreadystatechange = Repopulate;
           myRequest.open("POST", URLtoPostTo, true);
           myRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
           myRequest.setRequestHeader("Content-length", myData.length);
           myRequest.setRequestHeader("Connection", "close");
           myRequest.send(myData);
       }
       function Repopulate(){
           if((myRequest.readyState == 4) && (myRequest.status == 200))
           {
                document.body.innerHTML = myRequest.responseText;
           }
       }
    </script>
    Granted this way isn't really the best way to do it (actually, as printed it probably won't work for what you're doing at all, but it is the framework to accomplish anything like you described)
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #3
    New Coder
    Join Date
    Jul 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks i give it i try but i don't think ajax will work for what i wants to so i think i am just going to use frames


  •  

    Posting Permissions

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