用PHP、Ajax从Mysql中获取数据
我有一个数据库,包含分支机构、名称、代码、成员、状态等字段 我正试图通过分组mysql查询在下拉菜单中获取名称。然后显示下拉列表中所选名称的详细信息。但没有显示结果 选择分支、名称以及在搜索框中自动输入名称都可以正常工作。但在该ID、名称、分支机构、代码号等之后,数据不会显示 使用了三页。index.php、findName.php和searchfinal.php 我试过的代码如下: index.php:用PHP、Ajax从Mysql中获取数据,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我有一个数据库,包含分支机构、名称、代码、成员、状态等字段 我正试图通过分组mysql查询在下拉菜单中获取名称。然后显示下拉列表中所选名称的详细信息。但没有显示结果 选择分支、名称以及在搜索框中自动输入名称都可以正常工作。但在该ID、名称、分支机构、代码号等之后,数据不会显示 使用了三页。index.php、findName.php和searchfinal.php 我试过的代码如下: index.php: <html> <head> <script type=
<html>
<head>
<script type="text/javascript">
$(document).ready(function()
{
$(".branch").change(function()
{
var branch=$(this).val();
var dataString = 'branch='+ branch;
$.ajax
({
type: "POST",
url: "findName.php",
data: dataString,
cache: false,
success: function(html)
{
$(".getname").html(html);
}
});
});
});
</script>
<script>
$(document).ready(function()
{
$('#getname').change(function() {
$('#name').val($(this).val());
});
});
</script>
</head>
<body>
Branch :
<select name="branch" class="branch">
<option selected="selected">--Select Country--</option>
<?php
$sql=mysql_query("select branch from mutualbs group by branch");
while($row=mysql_fetch_array($sql))
{
$branch=$row['branch'];
echo '<option value="'.$branch.'">'.$branch.'</option>';
} ?>
</select> <br/><br/>
Name :
<select name="getname" id="getname" class="getname">
<option selected="selected">--Select Name--</option>
</select>
<br><br>
<hr>
<form method="get">
<label for="name">Name To Search :</label>
<input type="text" id="name" name="name" />
<button class="btnSearch">Search</button>
</form>
<br><br>
<table id="resultTable" style="color:#ff0000;">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>MBS Number</th>
<th>Branch</th>
<th>Status</th>
</tr>
</thead>
<tbody></tbody> //** Results Are Not Getting Shown
</table>
<script src="../js/jquery-1.10.2.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script>
$jQuery(document).ready(function($) {
$('.btnSearch').click(function(){
makeAjaxRequest();
});
$('form').submit(function(e){
e.preventDefault();
makeAjaxRequest();
return false;
});
function makeAjaxRequest() {
$.ajax({
url: 'searchfinal.php',
type: 'get',
data: {name: $('input#name').val()},
success: function(response) {
$('table#resultTable tbody').html(response);
}
});
}
});
</script>
</body>
</html>
$(文档).ready(函数()
{
$(“.branch”).change(函数()
{
var branch=$(this.val();
var dataString='branch='+branch;
$.ajax
({
类型:“POST”,
url:“findName.php”,
数据:dataString,
cache:false,
成功:函数(html)
{
$(“.getname”).html(html);
}
});
});
});
$(文档).ready(函数()
{
$('#getname').change(函数(){
$('#name').val($(this.val());
});
});
分支机构:
--选择国家--
姓名:
--选择名称--
要搜索的名称:
搜寻
身份证件
名称
MBS编号
分支机构
地位
//**结果没有显示出来
$jQuery(document).ready(函数($){
$('.btnSearch')。单击(函数(){
makeAjaxRequest();
});
$('form')。提交(函数(e){
e、 预防默认值();
makeAjaxRequest();
返回false;
});
函数makeAjaxRequest(){
$.ajax({
url:'searchfinal.php',
键入:“get”,
数据:{name:$('input#name').val(),
成功:功能(响应){
$('table#resultable tbody').html(响应);
}
});
}
});
直到搜索按钮一切正常。。。但单击搜索按钮后,不会显示任何结果,即ID、名称、代码、分支机构和状态
其他代码:
searchfinal.php
<?
require_once 'Connection.simple.php';
$conn = dbConnect();
if (isset($_GET['name'])) {
$data = "%".$_GET['name']."%";
$sql = "SELECT * FROM mutualbs WHERE name like ?";
$stmt = $conn->prepare($sql);
$results = $stmt->execute(array($data));
$rows = $stmt->fetchAll();
}
if(empty($rows)) {
echo "<tr>";
echo "<td colspan='4'>There were not records</td>";
echo "</tr>";
}
else {
foreach ($rows as $row) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['code_number']."</td>";
echo "<td>".$row['branch']."</td>";
echo "<td>".$row['status']."</td>";
echo "</tr>";
}
}
?>
需要帮助….请使用适当的缩进设置代码格式,以确保可读性您是否在控制台上看到任何错误!在加载jQuery本身之前,无法在页面中插入jQuery代码。head中的代码将在console中抛出错误
$未定义
。使用浏览器控制台(F12)检查有关此$jQuery(document).ready(函数($)的错误{
在bootstrap.min.js
调用之后。不确定为什么要使用$jQuery
。如果它是由您在jQuery文件中定义的,那也没关系,但通常是使用$
或jQuery
。我猜这也是一个错误