Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Php and Ajax autocomplete populate two fields

Status
Not open for further replies.

dkemas

Programmer
Mar 22, 2012
70
0
0
GB
I am making a fullname text field and want it so that when people start to type, it accesses the users table in the database and suggests names, the user clicks one to autocomplete the text field. This will hopefully prevent fullname spelling errors. I got this working no problem, but, is it possible to populate another text field automatically.

Here is the code I have to make it populate the fullname text field, but I need the employee number info to go into the employee number text field and not into the fullname space.

Code:
//form page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html>
<head>

<title>Autocomplete</title>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type='text/javascript' src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />

<script type="text/javascript">
$().ready(function() {
	$("#fullname").autocomplete("script.php", {
		width: 500,
		matchContains: true,
		selectFirst: false
	});
});
</script>
</head>
<body>

<div id="content">
	<form autocomplete="off" method='get'>
		<p>
			Enter Name <label>:</label>
			<input type="text" name="fullname" id="fullname" />
			<input type="text" name="employeenumber" id="employeenumber" />
		</p>
		<input type="submit" value="Submit" />
	</form>
</div>
</body>
</html>

Code:
//script page
<?php
$host="###"; 
$username="###"; 
$password="###"; 
$db_name="###"; 


$con = mysql_connect($host,$username,$password)   or die(mysql_error());
mysql_select_db($db_name, $con)  or die(mysql_error());

$q = strtolower($_GET["q"]);
if (!$q) return;

$sql = "select fullname, employeenumber from users where fullname LIKE '%$q%'";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
	$fullname = $rs['fullname'];
	$employeenumber = $rs['employeenumber'];
	echo "$fullname, $employeenumber\n";
}
?>


Can anyone help?

Thanks
 
Yrs, it is possible.

You'll need to go into the autocomplete() function inside the autocomplete.js file and modify it so it fills in the employee number when the name is clicked.


Without seeing what jquery.autcomplete.js does its hard to be any more specific.







----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top