不久前我用ajax和php做了一个搜索功能。您可以在文本框中填写文本,它会尝试在数据库中存储的所有国家/地区中查找匹配项。 现在我正在完善代码并使其成为 PDO,但我破坏了一些东西,但我无法找出是什么。
这是我的纯 HTML
<head>
<title>Ajax</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="scripts/Javascript.js"></script>
</head>
<body>
<div id="main">
<h1 class="title">Enter your country please</h1>
<input type="text" id="search" autocomplete="off" onchange="">
<h4 id="results-text">Showing results for: <b id="search-string">Array</b></h4>
<ul id="results"></ul>
</div>
</body>
这是我的 Jquery 和 javascript。请注意,我没有对 HTML 或 javascript 进行任何更改,因此不会出现类型错误。
$(document).ready(function() {
alert('asdf');
function search() {
var query_value = $('input#search').val();
$('b#search-string').html(query_value);
if(query_value !== ''){
$.ajax({
type: "POST",
url: "search.php",
data: { query: query_value },
cache: false,
success: function(html){
$("ul#results").html(html);
}
});
}
return false;
}
$("input#search").live("keyup", function(e) {
clearTimeout($.data(this, 'timer'));
var search_string = $(this).val();
if (search_string == '') {
$("ul#results").fadeOut();
$('h4#results-text').fadeOut();
}
else {
$("ul#results").fadeIn();
$('h4#results-text').fadeIn();
$(this).data('timer', setTimeout(search, 100));
};
});
});
这是我的 Search.PHP
<?php
class SearchEngine{
private $html;
public function __construct($conn){
$this->html = '<li class="result">
<h3>NameReplace</h3>
<a target="_blank" href="ULRReplace"></a>
</li>';
if (isset($_POST["query"])) {
$search_string = $_POST['query'];
}
else{
$search_string = '';
echo('Something went wrong, post query not set');
}
//$search_string = mysql_real_escape_string($search_string);
if (strlen($search_string) >= 1 && $search_string !== ' ') {
$query = 'SELECT * FROM country WHERE name LIKE "%' . $search_string . '%"';
$result = $conn->prepare($query);
$result->execute();
$result_array = $result->fetchAll();
foreach ($result_array as $result) {
$display_name = preg_replace("/" . $search_string . "/i", "<b>" . $search_string . "</b>", $result['name']);
$display_url = 'sadf';
$output = str_replace('NameReplace', $display_name, $this->html);
$output = str_replace('ULRReplace', $display_url, $output);
echo($output);
}
}
}
}
?>
问题:
Post 查询永远不会创建,为此我做了一个 isset,所以现在当没有创建 Post 查询时。它将创建一个值为“B”的 Post Query。
任何帮助将不胜感激。请温柔一点,我是 Ajax 新手,我宁愿理解而不是找到解决方案。谢谢
最佳答案
您没有指向正确的网址!看:
您已将 ajax 请求指向 search.php :
$.ajax({
type: "POST",
url: "search.php",
但是你在 search.php 中只有一个类。类本身不做任何事情。您必须实例化并调用其方法/函数。请比较这2段代码:
<?php
//server.php
//Doing nothing
class SearchEngine{
private $html;
public function __construct($conn){
echo "I'm executing";
}
}
?>
假设你在 server.php 中有这个
<?php
//server.php
//It will print "I'm executing" in the screen
class SearchEngine{
private $html;
public function __construct($conn){
echo "I'm executing";
}
}
$search = new SearchEngine($conn);
?>
要解决您原来的问题,您必须将ajax指向具有实例化代码的页面,而不是类,如下所示:
//index.php
//Let's suppose you have this code in your index.php
$SearchEngine = new SearchEngine($conn);
所以你的 JQuery ajax 代码应该如下所示:
$.ajax({
type: "POST",
url: "index.php",
关于javascript - PHP Ajax 不创建 post 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24618099/