NoobLaPHP Posted November 5, 2016 Share Posted November 5, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302470-multiple-divs-reloads/ Share on other sites More sharing options...
Jacques1 Posted November 5, 2016 Share Posted November 5, 2016 This is simply too vague. What do you know about Ajax? Are you using plain JavaScript (which I don't recommend) or a framework like jQuery? Are you able to make a request and process the response with a callback function? Quote Link to comment https://forums.phpfreaks.com/topic/302470-multiple-divs-reloads/#findComment-1538964 Share on other sites More sharing options...
Barand Posted November 5, 2016 Share Posted November 5, 2016 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" ) 1 Quote Link to comment https://forums.phpfreaks.com/topic/302470-multiple-divs-reloads/#findComment-1538965 Share on other sites More sharing options...
Solution NoobLaPHP Posted November 5, 2016 Author Solution Share Posted November 5, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/302470-multiple-divs-reloads/#findComment-1538966 Share on other sites More sharing options...
NoobLaPHP Posted November 5, 2016 Author Share Posted November 5, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302470-multiple-divs-reloads/#findComment-1538967 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.