Autocomplete second textbox with first textbox input

autocomplete the second text box by typing the first text box. For example, if I select Jane as the first name, I want the second textbox to retrieve the last name from the database and automatically populate it with Doe. I am not getting automatic input in the second textbox, not sure what I am doing wrong.

Here is the code for test.php

<?php   

?>
<script src="jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
	  $("#firstName").change(function(){
		  var firstName=$(this).val();
		   if(firstName != ''){
		 	      $.ajax({
                              type:"post",
                              url:"insert_process.php",
                              data:"firstName="+firstName,
							  datatype:"json",
				              success:function(data){ $("#lastname").val(data);
							    $('#ename').css( "background-color","#B3CBD6"  ) 
							 $('#ename').animate({backgroundColor: "#fff",});
							   }
                                
	                                                       });
		   }
		   else{
			   $("#lastname").val("");
			   }
		  });
	 });
</script>
<!DOCTYPE html>
<html>
<head>
<title>Systems Request </title>

</head>
<body>

<div align="center">

<form action = "insert.php" method ="post" class="form" style="width: 285px; height: 192px">

<br><br>First Name<br>
<input type="text" id="firstName" name="firstName">

<br>&nbsp;Last Name<br>
<input type="text" id="lastname" name="lastname" ><br><br>

<input type="submit" value= "Submit Name "><br>

</form>
</div>
</body>
</html>
      

Run codeHide result


and then here is the insert_proccess.php code

<?php
header('Content-Type: application/json');
$host = "localhost";
$user = "root";
$db_name= "people";
$pass= "systems399";
$con = mysql_connect($host, $user, $pass);
$db_conx=mysql_select_db("people", $con);
$fname=  $_POST["firstName"];
	   $sql="SELECT * FROM  names WHERE firstName='$fname'   ";	
		$query= mysqli_query($db_conx, $sql);
	$row = mysqli_fetch_array($query2 MYSQLI_ASSOC);
	$rc	= $row["lastname"];

echo json_encode ($rc);
 
?>
      

Run codeHide result


+3


source to share


1 answer


Is the request actually being sent (you can check the tab on the net)? Can you alert the returned data? Basically, to be more helpful, I'll need more information on exactly where the process is failing, with a quick glance try to set up your data structure like this:



data: { "firstName": firstName }

      

+1


source







All Articles