您提交表單通過將您的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);
}
來源
2013-04-22 14:33:52
ejx