<?php
//***************************************
// This is downloaded from www.plus2net.com //
/// You can distribute this code with the link to www.plus2net.com ///
// Please don't remove the link to www.plus2net.com ///
// This is for your learning only not for commercial use. ///////
//The author is not responsible for any type of loss or problem or damage on using this script.//
/// You can use it at your own risk. /////
//*****************************************
//error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
////// Your Database Details here /////////
$dbservertype='mysql';
$servername='127.0.0.1';
$dbusername='root';
$dbpassword='';
$dbname='delete_checkbox';
////////////////////////////////////////
////// DONOT EDIT BELOW /////////
///////////////////////////////////////
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
///////////////////// Main Code sarts ////////
$in=$_GET['txt'];
$msg="";
if(strlen($in)>0 and strlen($in) <20 ){
$t=mysql_query("select * from test_check where name like '$in%' OR lastname like '$in%' LIMIT 1");
while($nt=mysql_fetch_array($t)){ error_reporting(0);
$msg.=$nt[name];
}
}
echo $msg;

?>

The search results should display both the first name and last name but it's only displaying the first name in both textboxes. I need to display name in first textbox and surname in second textbox. Where am i getting it wrong?

Featured Replies in this Discussion

You are returning only the name in a message not alsoo the lastname. Add lastname also and separate the name and lastname so you can distinguish them when returned from ajax cal, maybe with comma: ... On html page insert the name and lastname: ...

// split the returned string into array of name and lastname
nameAndLastnameArray = value=httpxml.responseText.split(',');
// insert name into appropriate box
document.getElementById("displayDiv1").value=nameAndLastnameArray[0];
// insert lastame into appropriate box
document.getElementById("displayDiv2").value=nameAndLastnameArray[1];

Thanks. It's now displaying the correct data in the correct fields. But there's an issue with the surname field displaying 'undefined' when you start removing text from the search box. Only the name field is going blank when you do that, and i want both fields to go blank. Why is that?

Well, i'm developing a web-based client underwriting system which will contain a lot of records. So at the moment i am trying to make it easier for the user to fill in forms so that they don't have to manually type in the fields, hence the reason for the search box which will automatically populate the other textboxes as the user types. So your help would be much appreciated.

So at the moment i am trying to make it easier for the user to fill in forms so that they don't have to manually type in the fields

Now you have an input box where only one name/surname can be displayed. But in cases like yours I think the practice is to display a list of matches which gets shorter the more characters you type in. So the principle is:

on keyup event you shoot the ayax and read the matches for whatever is currently typed in an input box

ajax returns an unordered list (<ul></ul>) of matches into a designated div

you use CSS to position the list directly under the textbox and shape it appropriately

usually the ajax does not get fired on first letter but once you have say three letters or more

I hope I guessed right what is your intention, please correct me if I am wrong.

You will find a concept there. It is a bunch of texts to read but it is good if you get an idea how it goes. Then try to code it and when you have troubles come back here. Maybe it would be a good idea to start a new thread since this one has been marked as solved. If you start a new thread please PM me, otherwise I might not notice it.

Also have a look at these links for jquery plugins that do this functionality: