根据点击的链接更改隐藏div的内容

I'm building a website where the admin can make settings for the website. I would like the admin settings page to have a similar "feel" as the rest of the website, which has some nice looking jQuery features.

On the admin site there's a hidden div, which is shown when one of six links has been clicked. I'd like the content of the hidden div to change content before showing itself. I'm not sure how to do this. I could have a div box for every link on the page. But this becomes pretty cumbersome since I'd need to repeat my css and jquery for every link. I imagine that this, somehow, can be done with some javascript/jquery code that determines which link that was click and then decides which php function to call inside the hidden div. Then the php could "echo" out the content of the div, which then could be shown.

How could one do this?

My HTML/jQuery code is as follows:

--- The html links ---

<table id="settings">
<tr>    
<td>
<a href="#" class="action"><img src="images/folder.gif" alt="" height="100"/></a>
</td>

<td>
<a href="#" class="action"><img src="images/folder.gif" alt="" height="100"/></a>
</td>
<td>
<a href="#" class="action"><img src="images/folder.gif" alt="" height="100"/></a>
</td>

----- The Hidden div -----

<div id="dashboard_box">
<div class="dashboard_inside">
    <form action="#" method="post">
        <p style="font-size:20px; font-weight: bold;">Change color</p>
        </br>
        <fieldset>

            <? load_content1();?>

        </fieldset>
    </form>
</div>
</div>

---- jquery code (working)----

var mouse_is_inside = false;

$(document).ready(function() {
$(".action").click(function() {
    var loginBox = $("#dashboard_box");
    if (loginBox.is(":visible"))
        loginBox.fadeOut("fast");
    else
        loginBox.fadeIn("fast");
    return false;
});

$("#dashboard_box").hover(function(){ 
    mouse_is_inside=true; 
}, function(){ 
    mouse_is_inside=false; 
});

$("body").click(function(){
    if(! mouse_is_inside) $("#dashboard_box").fadeOut("fast");
});


});

You probably want to use ajax to load the content from the server. Take a look at jquery's .load() method: http://api.jquery.com/load/

You could include a data attribute per link:

<a class="action" data-content="content.php?method=load_content1"></a>
<a class="action" data-content="content.php?method=load_content2"></a>

js would look something like this:

$(".action").on('click', function() {    
    $("#dashboard_box fieldset").load($(this).data('content'), function() {
      var loginBox = $("#dashboard_box");
      if (loginBox.is(":visible"))
        loginBox.fadeOut("fast");
      else
        loginBox.fadeIn("fast");
    }
    return false;

});

Then, in your content.php file you could check the method parameter in the url to determine what content to return.

My php is a little rusty, but something like this:

<?
  call_user_func($_GET['method']); // i'm not sure how safe this is. you may want to be more explicit
?>

You can just add data attribute in each of your link's

<a href="#" data-url="content1.php" ..

Then on click of any of the a you can get the php to be called.

$('a').on('click',function(){
    var phpFunctionToCall = $(this).data('url');
});

You probably need to make ajax call to load content into your fieldset As this <? load_content1();?> run's on server and javascript have no control over it.

Thanks for all the help. this is what I ended up doing.

--- HTML ---

<a class="action" data-content="content.php?method=load_content2"></a>

---Jquery---

var mouse_is_inside = false;

$(document).ready(function() {

$(".action").click(function() {

    var phpFunctionToCall = $(this).data('content');
    $('#indhold').load(phpFunctionToCall);

    var loginBox = $("#dashboard_box");
    if (loginBox.is(":visible"))
        loginBox.fadeOut("fast");
    else
        loginBox.fadeIn("fast");
    return false;



});

$("#dashboard_box").hover(function(){ 
    mouse_is_inside=true; 
}, function(){ 
    mouse_is_inside=false; 
});

$("body").click(function(){
    if(! mouse_is_inside) $("#dashboard_box").fadeOut("fast");
});

});

--- PHP (shortened)---

function load_settings_panel($settingOnRequest) {

return $settingOnRequest;

}

$result = call_user_func('load_settings_panel', $_GET['method']);
echo($result);