【发布时间】:2021-06-05 17:41:28
【问题描述】:
我正在使用code from another thread,这让我非常接近预期的结果,但我被困在最后一步,请寻求专家的帮助。
我正在构建一个表单(带有 Xxamp 的 php),它由多个字段组成,用于将数据输入到 MySQL 数据库。自动完成输入字段正在检索文本搜索结果,但我还需要获取搜索和选择的记录的“id”值。
提交按钮需要发布所选搜索结果的 id 值 - 而不是显示的文本。
非常感谢您的帮助。
addnewrecord2.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Autocomplete Search Box in PHP MySQL - Php Coding Stuff</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<!-- Bootstrap Css -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="container">
<div class="row">
<h2>Search Here</h2>
<input type="text" name="place" id="search" placeholder="search here...." class="form-control">
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</div>
</form>
<script type="text/javascript">
$(function() {
$( "#search" ).autocomplete({
source: 'inputnewrecord2.php',
minLength: 3,
});
});
</script>
<?php
if(isset($_POST['place'])){
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$id = $_POST['place'];
if (empty($id)) {
echo "Place is empty";
} else {
echo $id;
}
}
}
?>
</body>
</html>
并输入newrecord2.php
<?php
/*
Code on this page and addrecord2.php is from: https://stackoverflow.com/questions/41104478/php-mysql-autocomplete
Same code is also here: https://www.phpcodingstuff.com/blog/autocomplete-search-box-in-php-mysql.html
*/
require_once('include/dbconn.php');
if (isset($_GET['term'])) {
$query = "SELECT TowerID, Place, Place2, Dedicn FROM tbldove WHERE Place LIKE '%{$_GET['term']}%' ORDER BY Place";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($user = mysqli_fetch_array($result)) {
if(empty($user['Place2'])){
$placeDis = $user['Place'].", ".$user['Dedicn'];
} else {
$placeDis = $user['Place'].", ".$user['Place2'].", ".$user['Dedicn'];
}
//$res[] = $user['Place'].", ".$user['Place2'] ;
$res[] = $placeDis;
//$res[] = $user['TowerID'];
//$res = [$placeDis,$user['TowerID']];
//$res = array($placeDis,$user['TowerID']);
}
} else {
$res = array();
}
//return json res
echo json_encode($res);
}
?>
【问题讨论】:
标签: javascript php mysql forms autocomplete