i have a table with the following structure
CREATE TABLE IF NOT EXISTS `test_check` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` varchar(65) NOT NULL DEFAULT '',
`lastname` varchar(65) NOT NULL DEFAULT '',
`email` varchar(65) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
i have a html page that has three text boxes, one for entering a search string and the other one for displaying the first name and surname of the returned row
the html page is as follows
<html>
<body>
<style>
#displayDiv{
background-color: #ffeeaa;
width: 200;
}
</style>
<script type="text/javascript">
function ajaxFunction(str)
{
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateChanged()
{
if(httpxml.readyState==4)
{
document.getElementById("displayDiv1").value=httpxml.responseText;
document.getElementById("displayDiv2").value=httpxml.responseText;
}
}
var url="ajax-search-demock.php";
url=url+"?txt="+str;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateChanged;
httpxml.open("GET",url,true);
httpxml.send(null);
}
</script>
</head>
<body>
<form name="my">
Search: <input type="text"
onkeyup="ajaxFunction(this.value);" name="" />
<p>
Name: <input type="text" name="displayDiv" id="displayDiv1"><br>
Surname: <input type="text" name="displayDiv" id="displayDiv2">
<p>
<input name="" type="reset" value="Reset">
</form>
</body>
</html>
the php code for searching the table is as follows
<?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?