Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-05-2008, 02:33 AM   PM User | #1
birdbrain24
New Coder

 
Join Date: Jul 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
birdbrain24 has a little shameless behaviour in the past
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>
birdbrain24 is offline   Reply With Quote
Old 02-05-2008, 03:10 AM   PM User | #2
mjlorbet
Regular Coder

 
mjlorbet's Avatar
 
Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
mjlorbet will become famous soon enough
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
mjlorbet is offline   Reply With Quote
Old 02-05-2008, 03:18 AM   PM User | #3
birdbrain24
New Coder

 
Join Date: Jul 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
birdbrain24 has a little shameless behaviour in the past
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
birdbrain24 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:15 PM.


Advertisement
Log in to turn off these ads.