PHPBuilder - Remote Scripting with Javascript, IFrames and PHP Page 6



RSS Twitter
Articles Html

Remote Scripting with Javascript, IFrames and PHP - Page 6

by: David Vance
|
May 10, 2004

Here, the search value is extracted from the textbox, and if it's less than 2 characters in length, nothing is done, and we are returned. With 60,000 records I needed to limit the search results and the resulting drop down list, so I implemented this arbitrary length check. Next, I check that the character entered is alphanumeric, a space, dash, or period. Again, an arbitrary limit that is specific to my case. Finally, if the search term is >= 2 characters and conatins valid characters, we dispatch the search term to callToServer().
Basically, the callToServer() function creates a hidden IFrame object and "calls" the server by loading the URL and it's query string with the IFrame's location attribute. There are also various browser compatibility and error checks interspersed.
Now, over on the server side is our PHP page of course, in my case server.php. The basic code looks like this:

<?php
if(!isset($_GET['s']))
{
    
$listAsString 'No results';
}
else 
{
//cleanVar() just does some slash/trim/strip_tags type things
    
$listAsString searchFromJs(cleanVar($_GET['s']));
}

function 
searchFromJs($searchTerm
{
    
    
$query 'select id, first_name, last_name 
        from users where last_name like "'
.$searchTerm.'%" 
    order by last_name, first_name";        
    $result = mysql_query($query);
    if($result === false) return '
Invalid query' . mysql_error();  
    if(mysql_num_rows($result) < 1) return '
No results';
    while($r = mysql_fetch_assoc($result))
    {
        $list .= $r['
id'].'~'.$r['first_name'].'~'.$r['last_name'].'|';
    }
    $list = htmlspecialchars(preg_replace('
/|$/','',$list),ENT_QUOTES);
    return $list;    
}
?>

<html>
<script type="text/javascript">
window.parent.handleResponse(encodeURIComponent('
<?=$listAsString;?>'));
</script>
</html>

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
David Vance

Comment:



Comment:

(Maximum characters: 1200). You have characters left.