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?

7
  • Can you show us where you have set this up? Commented Sep 21, 2012 at 4:28
  • 2
    Do you get any errors? This code should work. Commented Sep 21, 2012 at 4:28
  • 3
    Your error is probably in home.php. Try using firebug (or Developer console) to watch the request and response. Commented Sep 21, 2012 at 4:29
  • 1
    Does home.php really exist where specified? Commented Sep 21, 2012 at 4: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. Commented Sep 21, 2012 at 4:44

4 Answers 4

18

add home.php page url instead of file name.

$(document).ready(function(){
  $("#contact").click(function(){
    $("#contents").load('url to home.php');
  });
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, for some weird reasons, it worked! But I am wondering why it doesn't work without the URL. It should be the same, right?
Yes, relative URL's should work. Try appending using /home.php?
how can you load two different pages in two different div in one click of a button?
after loading dose it takes the CSS style from the destination page?
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);
  }
});

Comments

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'); 
 }); 
});

2 Comments

dose it maintain the css style too?
@KamelLabiad Yes it does maintain because normally you load your CSS in the HEAD section so it's available in DOM already.
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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.