2

I have 2 files, .tpl and .inc.php

In my .inc.php I am using 2 querys to get some data. With the smarty, I am passing the query responses to the .tpl file and showing those data in the table and in a select list.

Now, I would like that my list updates automaticaly when the user selects a new item in the select list.

I am actually able to detect the changes on the selection list.

The problem is, how could I call the function

updateToolPrivilegesTableForSelectedTool($Object)

from the .tpl file. The function is in .inc.php file. This shuld be possible with jquery but I don't know how.

Here is my code:

PHP:

<?php


updateToolPrivilegesTable();

$tool =$_DB->queryRaw("SELECT objects FROM backend_menu WHERE parent != 0");
while ($row_tool = $tool->next_assoc())
{
    $resultstool[] = $row_tool;
}

$smarty->assign("tool_name",$resultstool);
$smarty->assign("privileges",$resultsprivlieges);
$smarty->TDisplay("users/backend_tools_user_privileges.tpl", "Backend Tools Privileges", "general-content.tpl");



function updateToolPrivilegesTable()
{
global $_DB;
global $resultsprivlieges;
$resultsprivlieges = array();
$privlieges = $_DB->queryRaw("SELECT `group_id`, `user_id`, `Object`, `Read`, `Update`, `Insert`, `Delete` FROM `backend_privileges`");

while ($row = $privlieges->next_assoc())
{
    $resultsprivlieges[] = $row;
}
}

function updateToolPrivilegesTableForSelectedTool($Object)
{
global $_DB;
global $resultsprivlieges;
$resultsprivlieges = array();
$privlieges = $_DB->queryRaw("SELECT `group_id`, `user_id`, `Object`, `Read`, `Update`, `Insert`, `Delete` FROM `backend_privileges` WHERE `Object`=$Object");

while ($row = $privlieges->next_assoc())
{
    $resultsprivlieges[] = $row;
}
}

?>

HTML (.tpl)

<h1> Backend Tools Privileges</h1>

<table>
<tr>
<td>Tool</td>
<td>
<SELECT name="object" id="selectTool">
        {foreach from=$tool_name item=toolItem name=foo}    
                <OPTION name="object" VALUE="{$toolItem['objects']}">{$toolItem['objects']}</OPTION>
        {/foreach}
</SELECT>
</td>
</tr>
</table>

<table width='700px' id="employeetable" class="tablesorter" style='table-layout:fixed;'>
<thead>
    <tr>
        <th>Group Id</th>
        <th>User Id</th>
        <th>Object</th>
        <th>Read</th>
        <th>Update</th>
        <th>Insert</th>
        <th>Delete</th>
    </tr>
</thead>
<tbody>
    {foreach from=$privileges item=privilegesItem name=foo}
    <tr>
        <td>{$privilegesItem['group_id']}</td>
        <td>{$privilegesItem['user_id']}</td>
        <td>{$privilegesItem['Object']}</td>
        <td>{$privilegesItem['Read']}</td>
        <td>{$privilegesItem['Update']}</td>
        <td>{$privilegesItem['Insert']}</td>
        <td>{$privilegesItem['Delete']}</td>
    </tr>
    {/foreach}
</tbody>
</table>
</form>


{literal}
<script type="text/javascript">

$(selectTool).change(function() 
{
!!!!!!! Here I shuld call the function updateToolPrivilegesTableForSelectedTool($Object) from .php file.
}
</script>
{/literal}
Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265

1 Answers1

2

You could simply use a form post to reload the page with the selected object in your drop down as a post variable with an onchange form submit in your select box.

So change your drop down to:

<form action="mypage.php" method="post">
   <select name="object" id="selectTool" onChange="form.submit()">
        <option>Please pick an object to filter...</option>
        {foreach from=$tool_name item=toolItem name=foo}    
                <option name="object" VALUE="{$toolItem['objects']}">{$toolItem['objects']}</option>
        {/foreach}
   </select>
</form>

Then in your PHP have:

if(isset($_POST['object']{0})){
    updateToolPrivilegesTableForSelectedTool($_POST['object']);
}else{
    updateToolPrivilegesTable();
}

Be careful! Change the query in updateToolPrivilegesTableForSelectedTool to avoid database injection:

$privlieges = $_DB->queryRaw("SELECT `group_id`, `user_id`, `Object`, `Read`, `Update`, `Insert`, `Delete` FROM `backend_privileges` WHERE `Object`='".mysql_real_escape_string($Object)."'");

An Ajax solution, which is nicer would require some more code as you would need to replace the whole tbody on the fly and move updateToolPrivilegesTableForSelectedTool into a separate file. You will probably have to invoke the tablesorter again too if you are using a jQuery tablesorter plug in of some sort. It's going to be more tricky to get this all working correctly and you should understand the basics of form posting first I think.

  • Thank you Andew but nothing happens when I try to do this... Could you please help me – Milos Cuculovic Jun 12 '12 at 14:45
  • Did you change all three parts? Note that mypage.php needs to be replaced with the same filename as where you are loading from. Double check the $_POST['object'] is populated (maybe echo this with echo $_POST['object']) and also check your sql query is correct. echo "SELECT `group_id`, `user_id`, `Object`, `Read`, `Update`, `Insert`, `Delete` FROM `backend_privileges` WHERE `Object`='".mysql_real_escape_string($Object)."'"; when it gets into that function. – Andrew Nicholson Jun 12 '12 at 14:49
  • Sorry change, the line this.submit() to form.submit(); – Andrew Nicholson Jun 12 '12 at 14:55
  • Thanks again but still the same thing, with those changes, my table is not populated anymore and I am not accessing at all the if(isset($_POST['object']{0})) – Milos Cuculovic Jun 12 '12 at 15:01
  • Remove everything from between literal tags at the bottom too, that will help! – Andrew Nicholson Jun 12 '12 at 15:04
  • Ouh, Finally, almost work. I am accessing the function and get back the right result, but only one thing left, my table is not populated... – Milos Cuculovic Jun 12 '12 at 15:23
  • Check the results are in your PHP first before sending them to the Smarty template. So try something like: print_r($resultsprivlieges); before $smarty->assign("privileges",$resultsprivlieges); – Andrew Nicholson Jun 12 '12 at 15:27
  • Ok, problem resolved. Thank you very much, the problem was my variable refreshing. – Milos Cuculovic Jun 12 '12 at 15:28
  • Ok great! When you understand this solution you can perhaps explore the Ajax way of doing it. It's a bit more tricky like I said but would be nicer for your user as it would do it without reloading the page. :-) – Andrew Nicholson Jun 12 '12 at 15:34
  • Do you maybe have an tutorial or example of how to do it with Ajax? – Milos Cuculovic Jun 13 '12 at 06:44