javascript - PHP注册脚本中的“需要电子邮件”错误
问题描述
我目前正在编写一个执行基本用户注册的脚本。
当用户到达登录页面时,我有一个 JS 脚本,它从 URL 中识别他们的电子邮件并将其填充到电子邮件输入框中(已禁用)。之后,用户只需输入密码即可创建帐户。然而,即使它已被 JS 脚本填充,PHP 也会抛出错误“需要电子邮件”。
我试图将输入框的状态更改为禁用到启用,这并没有多大帮助。我已附上以下过程中涉及的文件。任何帮助将不胜感激。
填写电子邮件.js
$(document).ready(function() {
var link = window.location.href;
var emailIndex = link.indexOf("email");
if(emailIndex != -1) {
link = link.substring(emailIndex + 6);
} else {
link = "";
}
document.getElementById("email").value = link;
//$('#email').attr('disabled', 'disabled');});
注册.php
<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Register</h2>
</div>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div hidden class="input-group">
<label>Username</label>
<input type="text" name="username" value="user">
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" id="email" value="<?php echo $email; ?>" disabled>
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm password</label>
<input type="password" name="password_2">
</div>
<div class="input-group">
<button type="submit" class="btn" name="reg_user">Register</button>
</div>
</form>
</body>
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous">
</script>
<script type='text/javascript' src="fillEmail.js"></script>
</html>
服务器.php
<?php
session_start();
// initializing variables
$username = "";
$email = "";
$errors = array();
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'registration');
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($user['email'] === $email) {
array_push($errors, "email already exists");
}
}
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
}
?>
我对 PHP 相当陌生,所以任何帮助将不胜感激。提前非常感谢!
解决方案
根据我的评论,您可以简单地使用readonly
而不是disabled
达到类似的效果。还请使用准备好的语句来防止 SQL 注入。mysqli_real_escape_string
是不足够的
推荐阅读
- python - Python:如何使用 super() 初始化 2 个超类?
- blockchain - 仅将唯一键推送到 Solidity 中的数组
- sql - 使用子查询中的 LIMIT 和子查询和外部查询中重复的 WHERE 子句来简化查询
- python-3.x - 如何在标题的字符串中调用函数
- vba - Excel 2016 VBA - 比较 2 个数据透视表字段的匹配值
- php - 教义:从原始 SQL 中水合模型
- go - HTTP 请求在异步执行时失败
- dialogflow-es - Dialogflow 控制台不显示用于响应的 Facebook Messenger 选项
- jquery - 更改 img 源绑定后的 JQuery 丢失
- javascript - Rx Js 中不存在 Angular 6 throttleTime