Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load PHP file into DIV by jQuery?

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?

like image 377
JudeJitsu Avatar asked Sep 21 '12 04:09

JudeJitsu


People also ask

How to use load function in PHP?

The DOMDocument::load() function is an inbuilt function in PHP which is used to load an XML document from a file. Parameters: This function accepts two parameters as mentioned above and described below: $filename: This parameter holds the path to the XML document.

How to load function in jQuery?

jQuery load() Method The load() method loads data from a server and puts the returned data into the selected element. Syntax: $(selector). load(URL,data,callback);

Is jQuery load asynchronous?

You can use jQuery to support both synchronous and asynchronous code, with the `$.

How do you check if a div is loaded in jQuery?

$(document). ready(function(){ if (jQuery) { // jQuery is loaded alert("Yeah!"); } else { // jQuery is not loaded alert("Doesn't Work"); } });


2 Answers

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);
  }
});
like image 198
Tom Avatar answered Oct 05 '22 23:10

Tom


add home.php page url instead of file name.

$(document).ready(function(){
  $("#contact").click(function(){
    $("#contents").load('url to home.php');
  });
});
like image 22
Mangala Edirisinghe Avatar answered Oct 05 '22 23:10

Mangala Edirisinghe