php - 当我单击提交时,没有任何回声。为什么?
问题描述
我有一些代码。
这是我的 config.php
<?php
$db_user = "root";
$db_pass = "";
$db_name = "hello_world_accounts";
$db = new PDO('mysql:host=localhost;dbname='. $db_name . ';charset=utf8', $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
这是我的 index.php
<?php
session_start();
if(!isset($_SESSION['hello_world_accounts'])){
header("Location: login.php");
}
if(isset($_GET['logout'])){
session_destroy();
unset($_SESSION);
header("Location: login.php");
}
?>
<!DOCTYPE>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<p>Welcome to index</p>
<a href="index.php?logout=true">Logout</a>
</body>
</html>
这是我的 jslogin.php
<?php
error_reporting(-1);
session_start();
require_once('config.php');
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$emailval = ['email'];
$check = (strpos($emailval,'@'));
if ($check === false){
echo 'We require valid email';
}else{
$sql = "SELECT * FROM accounts WHERE username=? and password=? and email=? LIMIT 1";
$stmtselect = $db->prepare($sql);
$result = $stmtselect->execute([$username, $password, $email]);
$user = $stmtselect->fetch(PDO::FETCH_ASSOC);
if ($stmtselect->rowCount() > 0) {
$_SESSION['accounts'] = $user;
echo 'You have signed in successfully!';
}else {
echo 'Incorrect Username or Password or Email';
}
}
这是我的 login.php
<?php
error_reporting(-1);
session_start();
if(isset($_SESSION['hello_world_accounts'])){
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Programming Knowledge Login</title>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="container h-100">
<div class="d-flex justify-content-center h-100">
<div class="user_card">
<div class="d-flex justify-content-center">
<div class="brand_logo_container">
<img src="img/logo.png" class="brand_logo" alt="Programming Knowledge logo">
</div>
</div>
<div class="d-flex justify-content-center form_container">
<form>
<div class="input-group mb-2">
<div class="input-group-append">
<span class="input-group-text"><em class="fas fa-user"></em></span>
</div>
<input type="text" name="username" id="username" class="form-control input_user" placeholder="Username" required>
</div>
<div class="input-group mb-2">
<div class="input-group-append">
<span class="input-group-text"><em class="fas fa-key"></em></span>
</div>
<input type="password" name="password" id="password" class="form-control input_pass" placeholder="Password" required>
</div>
<div class="input-group mb-1">
<div class="input-group-append">
<span class="input-group-text"><em class="fas fa-inbox"></em></span>
</div>
<input type="email" name="email" id="email" class="form-control input_pass" placeholder="Email" required>
</div>
<div class="form-group">
<div class="custom-control custom-checkbox">
<input type="checkbox" name="rememberme" class="custom-control-input" id="customControlInline">
<label class="custom-control-label" for="customControlInline">Remember me</label>
</div>
</div>
</div>
<div class="d-flex justify-content-center mt-1 login_container">
<button type="button" name="button" id="login" class="btn login_btn">Login</button>
</div>
</form>
<div class="mt-3 mb-1">
<div class="d-flex justify-content-center links">
Don't have an account? <a href="registration.php" class="ml-2">Sign Up</a>
</div>
<div class="d-flex justify-content-center">
<a href="#">Forgot your password?</a>
</div>
</div>
</div>
</div>
</div>
<script src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script>
$(function(){
$('#login').click(function(e){
var valid = this.form.checkValidity();
if(valid){
var username = $('#username').val();
var password = $('#password').val();
var email = $('#email').val();
}
e.preventDefault();
$.ajax({
type: 'POST',
url: 'jslogin.php',
data: {username: username, password: password, email: email},
success: function(data){
alert(data);
if($.trim(data) === "1"){
setTimeout(' window.location.href = "index.php"', 1000);
}
},
error: function(data){
alert('There were errors while doing the operation.');
}
});
});
});
</script>
</body>
</html>
无论我输入什么,它都不会说什么。
如果您查看图像,当我单击提交时,它什么也没说。在解决此问题之前,我将对此进行评论。
我做错什么了吗?
我查看了关于 SOF 的教程和问题,甚至查看了手册。
解决方案
您在 if 块中声明变量。所以发生的事情是,当代码运行时,它会将 DOM 引用发送到服务器,而不是您引用的元素的值。
当表单无效时,您正在提交表单。你需要退出。
所以添加检查无效,然后退出,从 if 中删除变量。
$('#login').click(function(e){
e.preventDefault();
var valid = this.form.checkValidity();
if(!valid){
alert('Error');
return;
}
var username = $('#username').val();
var password = $('#password').val();
var email = $('#email').val();
$.ajax({...});
}
推荐阅读
- pentaho - 自定义 pentaho 用户控制台
- php - php echo html - 左边距不起作用
- javascript - 无法在 IE 中显示错误消息,但可以在 Chrome 中显示
- java - 当我向arraylist添加值以将内容添加到listview时,android ram不断上升
- airflow - Apache Airflow——无论前一个进程是否成功,都需要始终在 dag 中运行一个进程
- .net-core - 命令 dotnet publish/test no-build。由于 x64 构建无法找到文件
- php - 您可以使某些变量值在表单的 2 个实例中保持不变吗?
- iis - IIS WebActivator 不在 IIS 10 上运行
- arrays - 如何在反应中的对象数组中设置对象数组的状态
- java - 读取 pom.xml 中的属性文件时出错