New on DHTMLGoodies.com:
DHTML Chess is now available for WordPress at WordPressChess.com.
Download AJAX client lookup
Licensing
This script is distributed under the LGPL open source license.
Commercial licenses are also available. Some of these licenses also includes personal e-mail support for up to 1 year.
PHP
This script requires PHP. You can also other server side languages if you rewrite the code for getClient.php(See below)
Put this into your <HEAD> section
Put this into your <BODY> section
Download Javascript file
I have used the Simple Ajax Code Kit library(SACK) for this script. I have made a copy available for download here.
Put ajax.js in a subfolder called "js" or change the path to the file in the code above(i.e. <script type="text/javascript" src="js/ajax.js"></script>)
SQL
I have created a simple mySql table for this example. You can copy the queries from here
File for client lookup
A php file getClient.php is used in this script. This PHP file is called by Ajax when a new client ID is entered into the first text field. It will connect to the database and try to find a client with the entered client ID. If one is found, it will send data back to AJAX. These data will evalulated by our main script.
The getClient.php file looks like this:
<?php
/* Replace the data in these two lines with data for your db connection */
$connection = mysql_connect("host","username","password");
mysql_select_db("databaseName",$connection);
if(isset($_GET['getClientId'])){
$res = mysql_query("select * from ajax_client where clientID='".$_GET['getClientId']."'") or die(mysql_error());
if($inf = mysql_fetch_array($res)){
echo "formObj.firstname.value = '".$inf["firstname"]."';\n";
echo "formObj.lastname.value = '".$inf["lastname"]."';\n";
echo "formObj.address.value = '".$inf["address"]."';\n";
echo "formObj.zipCode.value = '".$inf["zipCode"]."';\n";
echo "formObj.city.value = '".$inf["city"]."';\n";
echo "formObj.country.value = '".$inf["country"]."';\n";
}else{
echo "formObj.firstname.value = '';\n";
echo "formObj.lastname.value = '';\n";
echo "formObj.address.value = '';\n";
echo "formObj.zipCode.value = '';\n";
echo "formObj.city.value = '';\n";
echo "formObj.country.value = '';\n";
}
}
?>
var ajaxObjects = new Array();var currentClientID=false;function getClientData() { var indexThis = ajaxObjects.length; ajaxObjects[indexThis] = new sack(); var clientID = document.getElementById("clientID").value.replace(/[^0-9a-z_]/g,""); if(clientID==currentClientID){ return; } currentClientID = clientID;// I implemented a sort of url's strip and set the method to GET, and told it to ajax.js. var url = "getClient.php?getClientId="+clientID; // full url to get if(url.indexOf('?')>=0) { ajaxObjects[indexThis].method="GET"; // defined method to GET var string = url.substring(url.indexOf("?")); url = url.replace(string,""); string = string.replace("?",""); var items = string.split(/&/g); for(var no=0;no<items.length;no++) { var tokens = items[no].split("="); if(tokens.length==2) { ajaxObjects[indexThis].setVar(tokens[0],tokens[1]); } } url = url.replace(string,""); }// end of implementations ajaxObjects[indexThis].requestFile = url; // Specifying which file to get ajaxObjects[indexThis].onCompletion = function(){ showClientData(indexThis); }; // Specify function that will be executed after file has been found ajaxObjects[indexThis].runAJAX(); // Execute AJAX function}function showClientData(indexThis) { var formObj = document.forms["clientForm"]; eval(ajaxObjects[indexThis].response);}function initFormEvents() { document.getElementById("clientID").onblur = getClientData; document.getElementById("clientID").focus();}window.onload = initFormEvents;
After changes in the JS script and without changing the ajax.js,results in: GET /myScript.cgi?id=1001&rndval=1303660978053This error indicates that there's a problem with the data received from the server.
If you have firefox, please install the Firebug add on. When installed, activate it.
Open your ajax client lookup page and type something in the "id" field, i.e. the field triggering the lookup event.
Now, pay attention to the console page in Firebug. When you typed in the id, you should see a new line appearing in the console, something like :
[+] http://www.yourdomain.com/getClient.php?getClientId=1001
Click on the [+] button in front of the line and then on the "Response" tab. What do you see there ?
If it's working correctly, you should see something like :
formObj.firstname.value = 'John';
formObj.lastname.value = 'Doe';
formObj.address.value = 'Kings square 10';
formObj.zipCode.value = '4070';
formObj.city.value = 'RANDABERG';
formObj.country.value = 'NORWAY';
");out.print("formObj.L_Name.value = '"+nam+"';"+"
");Firebug 1.7.1(On Screen)formObj.Old_ICNo.value = 'A10101010';formObj.L_Name.value = 'THIS IS TEST DATA 1';Firebug Consolehtml head /head body formObj.Old_ICNo.value = 'A10101010'; br formObj.L_Name.value = 'THIS IS TEST DATA 1'; br /body/html
How to use your code to look up values ​​in two tables from different database while not the same form. I tried to use two codes at the same time on the same page and only one works.
If you have an example that would be published.
thank you
Post your comment
Comment preview: