Bolli
Bolli

Reputation: 5274

Reload div with JavaScript containing PHP content

I have a PHP page with this structure:

<?php
include 'header.php';
include 'content.php';
include 'footer.php';
?>

In the header.php, I have a function that counts some rows in a database.

$show =$mysql->count('someparam', $foo['bar']);
   if ($show > 0) {
       echo $show;
   }

That's all good. Now, in the content.php file, the user can do operations that changes this $show value in the header, but I need to reload the page to see the updated $show number.

I want to solved this with JavaScript, but can't figure out how to do it.

I tried solving it with a JavaScript reload on timer, like this:

<script>
function render (){
    $('.customer-database').html(.customer-database)
}
window.setInterval(render, 500);
</script>

the $show counter is inside the div class costumer-database. This is not working because I need to put the HTML code in after the HTML, but I don’t want to put HTML code in. I simply want to reload it. Is this possible?

I am open to any suggestions in both JavaScript and PHP.

Upvotes: 0

Views: 817

Answers (4)

Armel Larcier
Armel Larcier

Reputation: 16037

this should work:

function render (){
    $('.customer-database').parent().load('myscript.php .customer-database');
    ^                              ^ ^     ^          ^ ^                ^ ^
    |   select the container in    | |     |   url    | | select content | |
    |   which to reload content    | |     | to your  | |  to put in the | |
    |______________________________| |     |   page   | |    container   | |
                                     |     |__________| |________________| |
                                     | send ajax call and replace content  |
                                     |_____________________________________|
}

Upvotes: 1

Hank
Hank

Reputation: 731

As the others have described, you want to use ajax. And jQuery is very good at this. With jQuery it would be as easy as

//whatever you do to change the $show value as a trigger, maybe a click?
$(".show_value_button").click(function(){
  $("#header_div_id").load("header.php");
});

Upvotes: 0

kristopher
kristopher

Reputation: 156

if you know what the name/id of the content area you want to replace is it is fairly simple to insert a jquery ajax request to reload only a portion of your page. Assuming you already have your jquery library included you can just use this javascript chunk

path = "/path/to/my/action/"
$.ajax({
    url : path,
    success : function(response) {
        $('.customer-database').replaceWith(response);
    },
    error : function(xhr) {
        alert('Error!  Status = ' + xhr.status);
    }
}); 

then you could write an Action that basically only gave you your count, and reload that Action into your element - the action responds with whatever count+html you like

Upvotes: 1

Oritm
Oritm

Reputation: 2120

What you can do is a AJAX request to a separate PHP file. The separate PHP file has the code that returns a number like your code:

$show =$mysql->count('someparam', $foo['bar']);
   if ($show > 0) {
       echo $show;
   }

See http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/ for help when creating AJAX calls with jQuery.

Upvotes: 1

Related Questions