12

I am developing a website and I have this on the side menu :

<a href="#" class="leftMenu" id="contact">Contact Us</a>

then I have this script

$(document).ready(function(){
  $("#contact").click(function(){
    $("#contents").load('home.php');
  });
});

and I have this DIV inside my page :

<div class="contentWrapper" id="contents"></div>

Obviously, what I am trying to do is to load home.php when I click on the Contact Us hyperlink, which doesn't work. What is wrong with my code?

JudeJitsu
  • 730
  • 1
  • 10
  • 17
  • Can you show us where you have set this up? – techie_28 Sep 21 '12 at 04:28
  • 2
    Do you get any errors? This code should work. – Blender Sep 21 '12 at 04:28
  • 3
    Your error is probably in home.php. Try using firebug (or Developer console) to watch the request and response. – Ben D Sep 21 '12 at 04:29
  • 1
    Does home.php really exist where specified? – m90 Sep 21 '12 at 04:30
  • Sorry, I cannot show where this is set up due to customer request. However, I am very sure that my jQuery is working as the toggling of link display works. However, when I click on the Contact Us link, it doesn't load up the contents inside the div as it should. – JudeJitsu Sep 21 '12 at 04:44
  • I've been testing this all morning, and can't get $.load('file.html') NOT to work. It seems to get it always. Can you tell about your folder structure, do you include files from subfolders and such. – Tom Sep 21 '12 at 06:53
  • Is the `` closure inside `#contents`? – Mike H. Oct 16 '13 at 12:31

4 Answers4

18

add home.php page url instead of file name.

$(document).ready(function(){
  $("#contact").click(function(){
    $("#contents").load('url to home.php');
  });
});
Mangala Edirisinghe
  • 1,111
  • 2
  • 15
  • 32
11

You can use $.load() like this, to get more data of whats happening. When you see the error message, you probably can solve it yourself ^^

$("#contents").load("home.php", function(response, status, xhr) {
  if (status == "error") {
      // alert(msg + xhr.status + " " + xhr.statusText);
      console.log(msg + xhr.status + " " + xhr.statusText);
  }
});
Tom
  • 3,009
  • 1
  • 18
  • 23
3

@user2805663 I know this post is pretty old but though let me post the solution it might help someone else, as it helped me.

Thanks to @Mangala Edirisinghe

by following method you can load two separate files in two different DIVs with single click(Link).

$(document).ready(function(){ 
 $("#clickableLink").click(function(){ 
  $("#contents").load('url/file1.php');
  $("#contents2").load('url/file2.php'); 
 }); 
});
TechYogi
  • 371
  • 3
  • 12
1

You have to use the path of "home.php" from index dir and not from script dir.

If you site is :

/
  index.php  
  scripts
     /script.js 
     /home.php

You have to modify your parameter passing "scripts/home.php"

Code Lღver
  • 15,573
  • 16
  • 56
  • 75