Javascript 搜索结果上的动态链接
我想用id=id、姓名、电子邮件、公司自动填写div。。点击任何搜索结果后。搜索结果中的id用作过滤器,从Mysql中获取适当的行 数据来自search.php中使用的同一个表 这是我的表格Javascript 搜索结果上的动态链接,javascript,php,mysql,Javascript,Php,Mysql,我想用id=id、姓名、电子邮件、公司自动填写div。。点击任何搜索结果后。搜索结果中的id用作过滤器,从Mysql中获取适当的行 数据来自search.php中使用的同一个表 这是我的表格 <link href="../action/css/onlinecustom.css" rel="stylesheet" type="text/css"> <script src="http://code.jquery.com/jquery-1.10.2.js" type="te
<link href="../action/css/onlinecustom.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
<script src="./action/scripts/global2.js" type="text/javascript"></script>
<script>
function searchq() {
var searchTxt = $("input[name='search']").val();
$.post("../action/subs/search.php/", {searchVal: searchTxt}, function(output) {
$("#output").html(output);
});
}
</script>
<title>Search</title>
<body>
<form action="http://comiut.com/index.php/user-records" method="post">
<input type="text" name="search" Placeholder="enter the search criteria..." onkeydown="searchq();"/>
<input type="submit" value ="serach"/>
</form>
//Serach result//
<div id="output"> </div>
//Data to populate upon click on any search result//
<div id="id"></div>
<div id="name"></div>
<div id="email"></div>
<div id="company_name"></div>
</body>
我还创建了search.php
<?php
include '../db/connect6.php';
if(isset($_POST['searchVal'])) {
$searchq = $_POST['searchVal'];
$searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM oz2ts_users WHERE oz2ts_users.id LIKE '%$searchq%' OR oz2ts_users.name LIKE '%$searchq%'") or die("Could not search");
$count = mysql_num_rows($query);
if($count == 0){
$output = 'There is no result to show!';
} else{
while($row = mysql_fetch_array ($query)) {
$id = $row['id'];
$name = $row['name'];
$username = $row['username'];
$output .= '<div><a class="resultItem" data-id="' . $id . '">'
. $name . ' '.$username.'</a></div>';
}
}
}
echo($output);
?>
您走的是正确的道路,尽管您自己没有尝试过任何东西,但这里有一些东西可以让您继续前进:
您应该返回类似的内容并填充搜索结果列表
$output .= '<div><a class="resultItem" data-id="' . $id . '">'
. $name . ' '.$username.'</a></div>';
现在您只需要getItem.php
,它可以是这样的:
<?php
include '../db/connect6.php';
if(isset($_POST['id'])) {
$id = intval($_POST['id']);
$result = mysqli_query("SELECT id,name,email,company FROM yourtable WHERE yourtable.id = $id") or die("Could not search");
// since we expect only one result we don't need a loop
$row = mysqli_fetch_assoc($result);
// let's return the $row in json format
// first let's prepare the http header
header('Content-Type: application/json');
// and now we return the json payload
echo json_encode($row);
}
如果可以,您应该这样做。已在PHP7中删除。了解使用PDO的语句,并考虑使用PDO,谢谢。将更改为MySQL。谢谢亚历克斯。我照你的建议做了。当我将鼠标悬停在项目上时,我会得到搜索结果,但没有链接。我遗漏了什么?当我检查一行结果时,我得到了这个。choou Choudja好了,你有了每个项目的链接,现在你可以点击我了你有什么改变吗?还是不行。没有链接,当我点击任何项目时没有发生任何事情。我将在下面发布我所做的一切。我编辑了我最初的帖子,以展示我所做的。它仍然不起作用。你有1分钟的时间看看我是否做错了什么,好吗?
$output .= '<div><a class="resultItem" data-id="' . $id . '">'
. $name . ' '.$username.'</a></div>';
$('body').on('click', 'a.resultItem', function(e) {
e.preventDefault();
$.ajax({
url: "getItem.php",
method: 'post',
data : $(this).data('id') // see the data attribute we used above in the a tag we constructed
}).done(function(data) {
$("#id").html(data.id);
$("#name").html(data.name);
$("#email").html(data.email);
$("#company_name").html(data.company);
});
});
<?php
include '../db/connect6.php';
if(isset($_POST['id'])) {
$id = intval($_POST['id']);
$result = mysqli_query("SELECT id,name,email,company FROM yourtable WHERE yourtable.id = $id") or die("Could not search");
// since we expect only one result we don't need a loop
$row = mysqli_fetch_assoc($result);
// let's return the $row in json format
// first let's prepare the http header
header('Content-Type: application/json');
// and now we return the json payload
echo json_encode($row);
}