我在PHP和AJAX方面表现不佳我需要你的帮助。在我的代码人员中,使用PHP和AJAX无法进行简单的搜索。 AJAX中的load_data函数正在运行,但我不确定它是否在我的文件中调用.php函数,但文件没有分开。
这是我的HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TESTING</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<br />
<h2 align="center">TESTING</h2>
<br />
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Search</span>
<input type="text" name="search_text" id="search_text" placeholder="Search" class="form-control" />
</div>
</div>
<br />
<div id="result"></div>
</div>
</body>
</html>
这是我的AJAX脚本
<script>
$(document).ready(function() {
load_data();
function load_data(query) {
//alert('testing');
$.ajax({
url: "functions.php",
method: "POST",
data: {
query: query
},
success: function(data) {
$('#result').html(data);
}
});
// alert(query);
}
$('#search_text').keyup(function() {
var search = $(this).val();
//alert(search);
if (search != '') {
load_data(search);
} else {
load_data();
}
});
});
</script>
这是我的PHP
<?php
//fetch.php
$connect = mysqli_connect("localhost", "root", "", "erpts_system");
$output = '';
if (isset($_POST["query"])) {
$search = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "
SELECT * FROM users
WHERE firstname LIKE '%".$search."%'
OR middlename LIKE '%".$search."%'
OR lastname LIKE '%".$search."%'
OR email LIKE '%".$search."%'
OR username LIKE '%".$search."%'
";
} else {
$query = "
SELECT * FROM users ";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>Customer Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["firstname"].'</td>
<td>'.$row["middlename"].'</td>
<td>'.$row["lastname"].'</td>
<td>'.$row["email"].'</td>
<td>'.$row["username"].'</td>
</tr>
';
}
echo $output;
} else {
echo 'Data Not Found';
}
?>
答案 0 :(得分:1)
看看我的设置......与你的设置类似......
$.ajax({
url:'checkin_ajax.php',
type:'POST',
data:'id=' + id + '&address=' + address2 + '&latitude=' + latitude2 + '&longitude=' + longitude2 + '&business=' + businessname2,
success: function(msg){
if($.trim(msg) == "failed"){
alert('You must be logged in');
}
else {
alert('Check in successful');
/*
var datad = $(msg).text();
console.log(datad);
var resultstring = datad.replace(',]',']');
var JsonParseData = JSON.parse(resultstring);
*/
}
}//ends success code
});//ends ajax
答案 1 :(得分:1)
我复制了代码并在我的本地机器上启动了它,它的工作非常完美。我认为您的问题来自链接您的js文件。我还没有看到链接你的js文件的脚本标签。如果您不打算使用外部脚本,我建议您在文档末尾插入javascript代码,如下所示
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TESTING</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<br />
<h2 align="center">TESTING</h2>
<br />
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Search</span>
<input type="text" name="search_text" id="search_text" placeholder="Search" class="form-control" />
</div>
</div>
<br />
<div id="result"></div>
</div>
<script>
$(document).ready(function() {
//load_data();
function load_data(query) {
//alert('testing');
$.ajax({
url: "functions.php",
method: "POST",
data: {
query: query
},
success: function(data) {
$('#result').html(data);
}
});
// alert(query);
}
$('#search_text').keyup(function() {
var search = $(this).val();
//alert(search);
if (search != '') {
load_data(search);
} else {
load_data();
}
});
});
</script>
</body>
请注意我如何评论您在函数声明之前调用的加载函数。确保php文件与包含此代码的php文件位于同一目录中。一切顺利