i want to fetch dynamic select option into SECOND text box after selecting autocomplete suggestion value into FIRST text box, For example, if i click on one of the autocomplete suggestions like "10 - computer" in the first text box (see my sample data below), I want to fetch the corresponding names (sahil, sumit, anil, shweta), and add them to the select in second text box.
If instead I click "12-History" I want to fetch those corresponding names (sanchita, riya, neha, pratik).
How to achieve this? Any help would be appreciated.
table
--
-- Table structure for table `stndb`
--
DROP TABLE IF EXISTS `stndb`;
CREATE TABLE IF NOT EXISTS `stndb` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Div_No` int(2) DEFAULT NULL,
`Div_Name` varchar(30) DEFAULT NULL,
`Student_Name` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `stndb`
--
INSERT INTO `stndb` (`id`, `Div_No`, `Div_Name`, `Student_Name`) VALUES
(1, 10, 'computer', 'sahil'),
(2, 10, 'computer', 'sumit'),
(3, 10, 'computer', 'anil'),
(4, 10, 'computer', 'shweta'),
(5, 11, 'IT', 'apoorva'),
(6, 11, 'IT', 'neeta'),
(7, 11, 'IT', 'sachin'),
(8, 11, 'IT', 'priya'),
(9, 12, 'History', 'sanchita'),
(10, 12, 'History', 'riya'),
(11, 12, 'History', 'neha'),
(12, 12, 'History', 'pratik');
SearchForm.php
<html>
<head>
<title> Search Record </title>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css"/>
</head>
<body>
<form action="" method ="POST" accept-charset="UTF-8" role="form" >
<fieldset>
<div class="form-group">
<input class="form-control" id="search" name="DivNumber" type="text" placeholder="Enter Division Number" type="text" required>
</div>
<div class="form-group">
<select input class="form-control" id="nameList" name="StName" type="text" placeholder="Select Student" type="text" readonly="readonly">
</div>
<input class="btn btn-lg btn-info btn-block" type="submit" value="Search ">
</fieldset>
</form>
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#search").autocomplete({
source: 'search.php',
minLength: 1,
});
});
</script>
search.php
<?php
require_once 'conn.php';
$search = $_GET['term'];
$list = array();
$search = $conn->real_escape_string($search);
$query = $conn->query("SELECT DISTINCT Div_No, Div_Name FROM `stndb` WHERE
Div_No LIKE '$search%' OR Div_Name LIKE '$search%' LIMIT 10") or
die(mysqli_connect_errno());
$rows = $query->num_rows;
if($rows > 0){
while($fetch = $query->fetch_assoc()){
$data['value'] = $fetch['Div_No'];
$data['label'] = $fetch['Div_No']. "-". $fetch['Div_Name']."";
array_push($list, $data);
}
}
echo json_encode($list);
?>