Gyroscope / Record Detail View
In the previous chapter we have created place holders for displaying landlord
details.
In
landlords.js (Client Handler):
showlandlord=function(llid,llname){
addtab('landlord_'+llid, llname,
'showlandlord&llid='+llid);
}
In
myservices.php (Server Handler):
case 'showlandlord':
include 'icl/showlandlord.inc.php';
showlandlord();
break;
Now we focus on
showlandlord.inc.php.
<?php
function showlandlord($llid=null){
if (!isset($llid)) $llid=GETVAL('llid');
global $db;
?>
<div class="section">
<?
$query="select * from landlords, persons
where landlords.personid=persons.personid
and landlords.llid=$llid ";
$rs=sql_query($query,$db);
$myrow=sql_fetch_array($rs);
$fname=$myrow['fname'];
$lname=$myrow['lname'];
?>
<table>
<tr><td>Firstname:</td>
<td>
<input id="llfname_<?echo $llid;?>" value="<?echo $fname;?>">
</td></tr>
<tr><td>Lastname:</td>
<td>
<input id="lllname_<?echo $llid;?>" value="<?echo $lname;?>">
</td></tr>
<tr><td></td>
<td>
<button onclick="updatelandlord('<?echo $llid;?>');">
Update
</button>
</td></tr>
</table>
</div>
<?
}//func
The
updatelandlord handler is first implemented in JavaScript and then
processed on the server side.
updatelandlord=function(llid){
var ofname=gid('llfname_'+llid).value;
var olname=gid('lllname_'+llid).value;
fname=encodeHTML(ofname);
lname=encodeHTML(olname);
reloadtab('landlord_'+llid, ofname+' '+olname,
'updatelandlord&llid='+llid+
'&fname='+fname+'&lname='+lname,
function(){
if (document.viewindex==0)
showview(0);
}
);
}
The above function performs a few tasks. It gathers the values from the
landlord form. Note how each field ID is made unique by appending the
unique landlord ID. The form values are then concatenated to a parameter
string, together with the
updatelandlord command. By invoking the reloadlab
function, the tab title is instantly replaced by the new, specified values. The
content of the tab will be replaced by the server side response. Upon server
call back, the landlord list on the left of the screen is also updated if the list is
currently visible.
Next we need to implement the server-side handler.
Add a clause in the message switch in
myservices.php:
case 'updatelandlord':
include 'icl/updatelandlord.inc.php';
updatelandlord();
break;
Then create
updatelandlord.inc.php in the
icl folder:
<?php
include 'icl/showlandlord.inc.php';
function updatelandlord(){
$llid=GETVAL('llid');
$fname=GETSTR('fname');
$lname=GETSTR('lname');
global $db;
$query="update landlords set
fname='$fname', lname='$lname'
where llid=$llid";
sql_query($query,$db);
showlandlord($llid);
}
The above function parses the values from the parameter string, updates the
database with new values and displays the updated content.
The
GETVAL function ensures that the input value is numeric.
GETSTR is a
wrapper function for $_GET; it detects Unicode strings and encodes the
value correctly.
The
showlandlord function is defined in a separate file so it's important to
include the file at the beginning of the script.
Now review the files we've just created or modified and pay attention to the
piggy-backing mechanism. In the updatelandlord JavaScript function, we
could have simply sent a request for updating the record; and upon
completion, send another request for displaying the record. Instead we send a
single updatelandlord event, and it's up to the server-side handler to both
update the data and return the new result. The file
showlandlord.inc.php is
structured to be called by both
myservices.php or inside another function.
Also note how file dependencies and parameter parsing are all done in the
implementation of server handler. The switch clause in
myservices.php
remains simple and agnostic of the underlying structure.