0

I have not been able to use JQuery/Ajax to load the content of my PHP file into a div tag.

Here is my page that is loading the file:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

<script>
function init() {
    reloadChat();
    setInterval (reloadChat, 5000);
}

function reloadChat() {
    $.ajax({  
        url: "chat.php",  
        cache: false,  
        success: function(){
            $("#chatmenu").load("chat.php");
        },  
    });  
}
</script>

<body onLoad='init()'></body>

<div id='chatmenu'></div>

The PHP file I am loading (chat.php) is in the same folder, and is just an echo statement:

<?php echo "test"; ?>

To make sure it was not a problem with my Javascript functions, I added an alert under the success function, and it did alert me every 5 seconds, so I think it is something with the load statement.

3
  • Why are you calling ajax twice, one as $.ajax and one as load("chat.php") in success? you can do one or the other. also you can use $(document).read(function(){ init();} instead of onload Commented Jul 15, 2012 at 15:53
  • try this: success: function(data) {$('#chatmenu').html(data);} Commented Jul 15, 2012 at 15:53
  • why do you use two Requests at the same time? Commented Jul 22, 2012 at 17:13

4 Answers 4

4

Use .load() straight away, no need to make an Ajax request first:

function reloadChat() {
    $("#chatmenu").load("chat.php");  
}

Update:

I notice that in your example code, you close your body-tag before your div element.

<body onLoad='init()'></body> <!-- This ain't right --> 
<div id='chatmenu'></div>

Try this instead:

<body onLoad='init()'>
    <div id='chatmenu'></div>
</body>
Sign up to request clarification or add additional context in comments.

7 Comments

That's not working either. Still, nothing shows up in the div on the page.
Yeah, just using .load() as your Ajax request method, skipping the $.ajax() syntax, should work. If this simple code isn't working, it's not the JavaScript in and of itself. What does your console tell you?
It says it's loading chat.php in the console, but it's not showing up in the div
So the GET is successful, but did you check the contents to see if they say "test"? Does the div have any styles on it that are keeping it hidden (visibility, display, size, hiding behind another with higher z-index)? You shouldn't need to rewrite!
The div is there, just no text in it.
|
2

Try this:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

<script>

function init() {
    reloadChat();
    setInterval (reloadChat, 5000);
}

function reloadChat() {
    $.ajax({  
        url: "chat.php",  
        cache: false,  
        success: function(response){
            $("#chatmenu").text(response);
        },  
    });  
}

</script>

<body onLoad='init()'></body>

<div id='chatmenu'>

</div>

Also, for the love of god, please use an up-to-date version of jQuery

5 Comments

This just seemed to lag my browser and my page became unresponsive.. Don't know how to fix that.
Get rid of the setInterval call...its causing an infinite loop.
Isn't it necessary for an 'instant' chat?
I think you meant html(response) or text(response) not load(response)
Yes, you're right. But I've given up on this question...I realized he's trying to do something way over his head.
1

It looks your first request from $.ajax will return 'test', and then you're using that as the URL for $("#chatmenu").load.

Try this:

function reloadChat() {
    $.ajax({  
        url: "chat.php",  
        cache: false,  
        success: function(data){
            $("#chatmenu").append(data);
        },  
    });  
}

Or, if you want to replace the contents of #chatmenu the method posted by Christofer Eliasson where you just call $("#chatmenu").load("chat.php") in reloadChat will work.

3 Comments

As with the .load() syntax suggestion, this one should work fine. It's not the script in and of itself. It's either an invalid URL after all, it's not returning what you think it is, or something else is bombing out in code we can't see. Check your console for errors.
No errors in the console, I guess I'll just try rewriting everything and hoping for the best
Am I implementing JQuery/Ajax correctly when I "import" the script?
0

Putting it all together:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div id="chatmenu"></div>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
      $(function() {
        setInterval (function() { 
           $("#chatmenu").load("chat.php");
        }, 5000);
      });
    </script>
  </body>
</html>

2 Comments

You don't want those scripts inside the chatmenu div.
I must be going crazy; I thought they were within that div. My apologies for my blindness.

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.