我想从数据库中获取数据并在弹出窗口中显示它?

时间:2016-06-21 10:36:24

标签: javascript php jquery json ajax

<html>
<head>
<script type="text/javascript" src="jquery-3.0.0.min.js"></script>
</head>
<body>
<form method="POST">
<input type="text" class="form-control"  placeholder="Search" name ="searchterm">
<a href="#" id="button"><button type="submit" id="submit">click me</button></a>                           
</form>
<script>
$(document).ready(function(){
$("#button").click(function(){
$.get("search.php");
return false;
});
});
</script>
</body>
</html>

上面是我的代码search.html

的html文件
<?php
mysql_connect("localhost","root","");
mysql_select_db("testdb");
$search = mysql_real_escape_string(trim($_POST['searchterm']));
$find_categories = mysql_query("SELECT `person3`,`person4` FROM `dbtable` WHERE `person1` LIKE'%$search%'");
$data = array();
while($row = mysql_fetch_assoc($find_categories))
{
$data[]=$row;
}   
echo json_encode($data);
?>

这是我的php文件search.php

我在从数据库中获取数据并显示数据时遇到问题。我怎么能这样做,有什么方法可以让我在弹出窗口中显示它?

1 个答案:

答案 0 :(得分:0)

this is the output after removing the jquery and writing php in the same file

<!doctype html>
<html>
<head>
<script type="text/javascript" src="jquery-3.0.0.min.js"></script>
</head>
<body>
<form method="POST">
<input type="text" class="form-control"  placeholder="Search"  name="searchterm">
<a href="#" id="popup"><button type="submit" id="submit">click me</button> </a>                           
<?php
mysql_connect("localhost","root" ,"");
mysql_select_db("testdb");
$search = mysql_real_escape_string(trim($_POST['searchterm']));
$find_categories = mysql_query("SELECT `person3`,`person4` FROM `dbtable` WHERE `person1` LIKE'%$search%'");
$data = array();
while($row = mysql_fetch_assoc($find_categories))
{
$data[]=$row;
}   
echo json_encode($data);
?>
</form>
</body>
</html>

的search.php