tags:

views:

75

answers:

3

Hello.. I have my site on index.html made in html+css., at my index.html i have this div id "content", where you put in main content to the page, and there's a link at the menubar that src to page2.php. When you press on the link it goes to the page2.php, but i want it to display inside the < div "content". In page2 i have Hello this is a test, in a echo..

I dont want to use frames.. should i split up my design on index.php, and then on page2.php include top & footer? or is there another way

function test() {
var xhr = XMLHttpRequest();
xhr.onreadystatechange = function(){
   if(xhr.readystate==4 && xhr.status=200)
      document.getElementById('content').innerHTML = xhr.responseText;
};

xhr.open("GET", "start.php", false);
xhr.send(null);
}

<a href="start.php" onclick="test(); return false;">Hjem </a> | 

<div id="content" ></div>
+1  A: 

Use AJAX to post a HTTP GET request to page2.php and display the result(echo) in the div container.

Somewhat like following, (will not work in IE)

<script>
function test(){
   var xhr = XMLHttpRequest();
   xhr.onreadystatechange = function(){
      if(xhr.readystate==4 && xhr.status=200)
         document.getElementById('content').innerHTML = xhr.responseText;
   };

   xhr.open("GET", "page2.php", false);
   xhr.send(null);
}

</script>
Try AJAX online
N 1.1
Can you show an example, the "echo" i said was just an example.. on page2.php i have an mysql query that displays some information from a table in the mysql database
Karem
I gave that a function, named "test".. now i did <a href="page2.php" onclick="javascript: test();">click me</a> but it wont work, the page2.php just opens in a new page and not in content id
Karem
Yes ive did that now.. still nothing, just opens in a new..please check updated question for how it looks
Karem
I am using firefox.. The "Try AJAX Online" link you provided works fine..
Karem
@Azzyh: you need to write the javascript code in `<script>` tags.Updated the answer check it.
N 1.1
Install Firebug add-on for firefox.
N 1.1
Yes, i have it inside <script type="text/javascript"> and inside <head> too ..
Karem
@Azzyh: See what error you get on Firebug console (you need to enable it) or on Firefox's Error Console (ctrl+shift+j)
N 1.1
A: 

You can use XMLHttpRequest() and not only "Hello this is a test", but you can also set any data from your MySQL database to <div id="content"> innerHTML.

Syom
A: 

Like you suggested in your question, you should look into splitting the index.html into a header and a footer. This will allow your site to work without javascript, and you may find that it is easier or makes more sense.

There's some quite good tutorials around, such as this one, but googling for php header footer returns plenty of options.

Blair McMillan