我是PHP的新手,正在创建一个登录系统,ajax请求似乎没有通过调用php文件中的正确函数。
JavaScript函数:
function register () {
var form = $(".form");
$.ajax({
type: "POST",
url: "../app/actions.php",
data: form.serialize(),
success: function(response) {
alert("request sucess");
},
fail: function(xhr, textStatus, errorThrown){
alert("request failed");
}
});
}
PHP文件actions.php:
<?php
require_once "config.php";
require "login.php";
require "logout.php";
require "register.php";
require "resetpassword.php";
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'register' : register();break;
case 'login' : login();break;
case 'resetpassword' : resetpassword();break;
}
}
?>
PHP文件app / register.php:
<?php
function register() {
$name = $username = $password = "";
if(empty(trim($_POST["name"]))){
$name_err = "Please enter a name.";
} else {
$param_name = trim($_POST["name"]);
$name = trim($_POST["name"]);
}
if(empty(trim($_POST["username"]))){
$username_err = "Please enter a username.";
} else{
$sql = "SELECT id FROM users WHERE username = ?";
$stmt = mysqli_prepare($link, $sql);
if($stmt){
mysqli_stmt_bind_param($stmt, "s", $param_username);
$param_username = trim($_POST["username"]);
if(mysqli_stmt_execute($stmt)){
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1){
$username_err = "This username is already taken.";
} else{
$username = trim($_POST["username"]);
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
mysqli_stmt_close($stmt);
}
if(empty(trim($_POST["password"]))){
$password_err = "Please enter a password.";
} elseif(strlen(trim($_POST["password"])) < 8){
$password_err = "Password must have at least 8 characters.";
} else{
$password = trim($_POST["password"]);
}
if(empty($name_err) && empty($username_err) && empty($password_err)){
$sql = "INSERT INTO users (name, username, password) VALUES (?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "sss", $param_name, $param_username, $param_password);
$param_name = $name;
$param_username = $username;
$param_password = password_hash($password, PASSWORD_DEFAULT);
if(mysqli_stmt_execute($stmt)){
header("location: login.php");
} else{
echo "Something went wrong. Please try again later.";
}
}
mysqli_stmt_close($stmt);
}
mysqli_close($link);
}
?>
PHP文件register.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="wrapper">
<h2>Register</h2>
<p>Please fill this form to create an account.</p>
<form class="form" onsubmit="register()">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control">
<span class="help-block">Please enter a name.</span>
</div>
<div class="form-group">
<label>Username</label>
<input type="text" name="username" class="form-control">
<span class="help-block">Please enter a username.</span>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control">
<span class="help-block">Please enter a password which must have 8 characters or more, including at least one number and one letter.</span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit">
<input type="reset" class="btn btn-default" value="Reset">
</div>
<p>Already have an account? <a href="login.php">Login here</a>.</p>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script type="text/javascript" src="scripts/app.js"></script>
</body>
</html>
我试图调试它,但似乎无法到达位于actions.php文件中的if语句。
任何想法这可能有什么问题吗?
答案 0 :(得分:0)
事实证明我在html中使用Jquery Slim构建。需要使用普通的Jquery。固定。