Livesearch php and ajax



我的代码遇到了一些问题,我希望每当我输入某些内容但它不起作用时它都会执行 php 文件

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    function getStates(value) {
        $.post("search.php", {name:value},function(data)
            $("#results").html(data);
        }); 
    }
</script>
</head>
<input type="text" onkeyup="getStates(this.value)"/>
<br>
<div id="results"></div>
<body>
</body>
</html>

.php

<?php
mysqli_connect("localhost", "#", "#") or die(mysqli_connect_errno());
mysql_select_db("#") or die(mysql_error());
$search = $_POST["name"];
$players = mysql_query("SELECT firstname FROM players WHERE firstname LIKE '%search%'");
while($player = mysql_fetch_array($players)) {
    echo "<div>" . $players["firstname"] . "</div>";
}

?>

据我所知,您应该更改此内容

  '%search%' 

  '%{$search}%'

  $players = mysql_query("SELECT firstname FROM players WHERE firstname LIKE '%search%'");

编辑

@user3187651 假设您在服务器端完成了所有正确的工作。将你的 JavaScript 更改为:

function getStates(value) {
    $.post("search.php", {name:value},function(data){
        $("#results").html(data);
    }
    ); 
}

这应该可以消除客户端的错误。

你缺少{ .只需做:

function xyx(name) {
    $.post("search.php", { name: value }, function(data) {
        $("#results").html(data);
    });
}

您的代码中缺少一些内容:

  <!doctype html>
<html>
<head>
 <meta charset="UTF-8">
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> //ur getting the jquery via online
 <script>
 $(document).ready(function(){
    $("#textBoxId").change(function() //triggers when you change the value in your textbox
    {
       var value = $(this).val(); //gets the value of your textbox
       $.post("search.php", {id:value},function(data)
           $("#results").append(data);
        }); 
    }
 });
</script>
</head>
<body>
 <input type="text" id="textBoxId"/>
<br>
<div id="results"></div>
</body>
</html>

在你的 php 中:

<?php
mysqli_connect("localhost", "#", "#") or die(mysqli_connect_errno());
mysql_select_db("#") or die(mysql_error());
$search = $_POST['id'];
$returnData = ""; 
$players = mysql_query("SELECT firstname FROM players WHERE firstname LIKE '%search%'");
while($player = mysql_fetch_array($players)) {
    $returnData .= "<div>" . $players["firstname"] . "</div>";
}
echo $returnData; 

对于更安全和更有创意的后端代码,您可以使用它。

<?php
$host = 'localhost';
$user = 'root';
$password = '';
$database = 'dev_testing';
$mysqli = new mysqli($host, $user, $password, $database);
$username = $_GET['username'];
$username = trim(htmlspecialchars($username));
$like = '%' . strtolower($username) . '%';
$statement = $mysqli -> prepare('
    SELECT name, picture, description 
    FROM users 
    WHERE lower(name) LIKE ?  
    ORDER BY INSTR(title, ?), title
    LIMIT 20'
);
if (
    $statement &&
    $statement -> bind_param('ss', $like, $username) &&
    $statement -> execute() &&
    $statement -> store_result() &&
    $statement -> bind_result($name, $picture, $description)
) {
    $array = [];
    while ($statement -> fetch()) {
        $array[] = [
            'name' => $name,
            'picture' => $picture,
            'description' => $description
        ];
    }
    echo json_encode($array);
    exit();
}

代码的优点

  • 防止 SQL 注入
  • 订单结果来自最佳匹配
  • 发送 JSON 响应(JSON 是轻量级的)

完整教程:使用 AJAX、PHP 和 MYSQL 进行实时搜索

相关内容

  • 没有找到相关文章

最新更新