Jump to content

Multiple divs reloads


Go to solution Solved by NoobLaPHP,

Recommended Posts

I am trying to reload multiple divs. for things like mail and notifications on the same page of a website. I want to call all the information from another page but am unsure where to go as i have only just started learning more about ajax.

I want to set it to call for the information just using something basic like <span id=notifications></span>

 

To which it will show up the text needed when on the website but i really don't know where else to start. Any help will be greatly appreciated  

Link to comment
https://forums.phpfreaks.com/topic/302470-multiple-divs-reloads/
Share on other sites

In the PHP, create an array to store the content destined for each div. Return the json-encoded array

<?php

// set up an array to hold the
// contents of the divs

    $results = [];
    
// get contents for div A and store in the array

    $results['A'] = "Contents of div A";

// get contents for div B and store in the array

    $results['B'] = "Contents of div B";

// JSON encode the array and send

    echo json_encode($results);
?>

In the AJAX call, populate the divs from the returned response

            // ajax call
            $.get (
                "my_ajax.php",
                function(data) {
                    $("#divA").html(data.A);         // put results into their
                    $("#divB").html(data.B);         // respective divs    
                },
                "json"
            )

  • Like 1
  • Solution

 

In the PHP, create an array to store the content destined for each div. Return the json-encoded array

<?php

// set up an array to hold the
// contents of the divs

    $results = [];
    
// get contents for div A and store in the array

    $results['A'] = "Contents of div A";

// get contents for div B and store in the array

    $results['B'] = "Contents of div B";

// JSON encode the array and send

    echo json_encode($results);
?>

In the AJAX call, populate the divs from the returned response

            // ajax call
            $.get (
                "my_ajax.php",
                function(data) {
                    $("#divA").html(data.A);         // put results into their
                    $("#divB").html(data.B);         // respective divs    
                },
                "json"
            )

Nice, this is doing exactly what i need. Now another question i have is, is there a way to get this to refresh using some sort of setInterval?

I managed to sort it. Not sure if it is correct but it works

 

var interval = setInterval(function() {

// ajax call
$.get (
"my_ajax.php",
function(data) {
$("#divA").html(data.A); // put results into their
$("#divB").html(data.B); // respective divs
},
"json"
)

},1000);

 

Cheers for your help.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.