您提交启动 ajax 脚本的表单,该脚本将数据发送到处理输入并为您提供答案的 PHP 文件。
使用 PDO 或 MySqLi。Mysql 已弃用,不再受支持。我下面的示例使用 PDO 方法。
你的 PHP 应该看起来像这样(这是未经测试的代码,所以可能有错别字):
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if (!empty($username) && !empty($password)) {
// We create a PDO connection to our database
$con = new PDO("mysql:host=yourhost;dbname=yourdatabase", "username", "password");
// We prepare our query, this effectively prevents sql injection
$query = $con->prepare("SELECT * FROM table WHERE username=:username AND password=:password LIMIT 1");
// We bind our $_POST values to the placeholders in our query
$query->bindValue(":username", $username, PDO::PARAM_STR);
$query->bindValue(":password", $password, PDO::PARAM_STR);
// We execute our query
$query->execute();
$result = $query->fetch(); // Grab the matches our query produced
// Here we check if we found a match in our DB
if (!empty($result)) {
echo "Matches were found";
} else {
echo "No matches found";
}
} else {
echo "Please fill out all fields";
}
?>
至于从您的 AJAX 脚本中获得回复,您可以简单地提醒回复或随意显示。
success: function(data) {
alert(data);
}