所以我已经在这里问了一些问题并解决了我的问题,但我认为它们正在增长而不是下降,哈哈。我现在的问题是我得到了" Undefined index
" error.I会留下错误here的图片。我试图添加变量来定义我的fsearch.Tried之类的事情
$fsearch = mysqli_escape_string($connect, $_POST['fsearch']);
更简单就像。
$fsearch = $_POST['fsearch'];
但它不起作用。这是代码。
jQuery(document).ready(function ($) {
$("#food_search").keyup(function(event){
var search_term =$(this).val();
$.ajax({
type:"POST",
url:"/Food-Search",
data:{fsearch:search_term},
success:function(res){
$("#food_search_result").html(res);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
}
});
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!----------------------------------------------------------------
HTML
----------------------------------------------------------------->
<form method="POST">
<p>Търсене на храни: <input type="text" name="fsearch" id="food_search"></p>
</form>
<!----------------------------------------------------------------
PHP
-----------------------------------------------------------------><?php
$hostname = "localhost";
$username = "name";
$password = "password";
$databaseName = "dbName";
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
if (!empty($_POST['fsearch'])) {
$fsearch = mysqli_escape_string($connect, $_POST['fsearch']);
$req = $connect->prepare('SELECT * FROM food_data_bg WHERE title LIKE "%".$fsearch."%"');
$req->execute(array(
'fsearch'=>'%'.$_POST['fsearch'].'%'
));
if($req->rowCount()==0){
echo 'Не бяха намерени резултати!';
}
else{
while($foodsearch=$req->fetch()){
?>
<div class="search-result">
<img src="<?php echo $data['fimage']; ?>" class="fimage"/>
<span class="result-title"><?php echo $data['title'];?></span><br>
<span class="calories-total"><?php echo $data['calories total'];?></span><br>
</div>
<?php
}
}
}
?>
&#13;
代码用于搜索,它必须在每个keyup上获取#food-search
(输入字段)的值并将其发送给PHP,PHP必须检查数据库中的匹配并打印它。(我想我得到了通过在PHP页面中打印结果并将其返回到java以在#food_search_result
div)
答案 0 :(得分:0)
希望这会有所帮助。
PHP代码
$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "stackoverflow";
if (!empty($_POST['fsearch'])) {
$fsearch = $_POST['fsearch'];
$mysqli = new mysqli($hostname , $username, $password, $databaseName);
if($mysqli->connect_error)
{
die("$mysqli->connect_errno: $mysqli->connect_error");
}
$query = "SELECT * FROM food_data_bg where title LIKE ?";
$stmt = $mysqli->stmt_init();
if(!$stmt->prepare($query))
{
print "Failed to prepare statement\n";
}
else
{
$searchVar = "%" . $fsearch . "%";
$stmt->bind_param("s", $searchVar);
$stmt->execute();
$result = $stmt->get_result();
if(!$result->num_rows){
echo 'Не бяха намерени резултати!';
}
while ($row = $result->fetch_array())
{
?>
<div class="search-result" style='clear:both; border:1px solid red;'>
<img src="<?php //echo $row['fimage']; ?>" class="fimage"/>
<span class="result-title"><?php echo $row['title'];?></span><br>
<span class="calories-total"><?php echo $row['calories total'];?></span><br>
</div>
<?php
}
}
$stmt->close();
$mysqli->close();
}
HTML代码
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
jQuery(document).ready(function ($) {
$("#food_search").keyup(function(event){
var search_term =$(this).val();
console.log(search_term);
$.ajax({
type:"POST",
url:"Food-Search.php",
data:{fsearch:search_term},
success:function(res){
$("#food_search_result").html(res);
console.log(res);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
}
});
});
});
</script>
<!----------------------------------------------------------------
HTML
----------------------------------------------------------------->
<form method="POST">
<p>Търсене на храни: <input type="text" name="fsearch" id="food_search"></p>
</form>
<!----------------------------------------------------------------
PHP
----------------------------------------------------------------->
<div style="border:#FF0000;clear:both; padding:10px;min-height:300px;" id="food_search_result"></div>