php - 函数没有接收到正确的输入
问题描述
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Register</title>
<style>
#reg_form {
display: flex;
flex-direction: column;
/*max-width: 50vw;*/
min-height: 100vh;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<form action="register.php" method='post' id="reg_form">
<input type="text" name="reg_fname" required placeholder="John">
<input type="text" name="reg_lname" required placeholder="Doe">
<input type="email" name="reg_email" required placeholder="example@gmail.com">
<input type="email" name="reg_email2" required placeholder="example@gmail.com">
<input type="password" name="reg_password" required>
<input type="password" name="reg_password2" required>
<input type="submit" value="Submit" name="register_button">
</form>
<?php
$con = mysqli_connect('localhost', 'root', '', 'social');
if (mysqli_connect_errno()) {
echo 'Connection error: '.mysqli_connect_error();
}
$fname = '';
$lname = '';
$email = '';
$email2 = '';
$password = '';
$password2 = '';
$date = '';
$error_array = '';
if (isset($_POST['register_button'])) {
beautify($fname, 'reg_fname'); // fname
beautify($lname, 'reg_lname'); // lname
beautify($email, 'reg_email'); //email
beautify($email2, 'reg_email2'); //email2
$password = strip_tags($_POST['reg_password']); // password
$password2 = strip_tags($_POST['reg_password2']); // password2
$date = date('Y-m-d');
if ($email == $email2) {
} else {
echo "Emails don't match";
}
}
function beautify($var_name, $input_name) {
$var_name = strip_tags($_POST[$input_name]);
$var_name = str_replace(' ', '', $var_name);
$var_name = ucfirst(strtolower($input_name));
}
?>
</body>
</html>
我怀疑该beautify
功能没有按预期工作。因为如果电子邮件不匹配,我不会收到回显声明。如果电子邮件不匹配,我希望得到回显声明。我的代码有什么问题?
解决方案
将函数更改为只需要表单输入字段的名称,并将“美化”值返回给调用者。
function beautify($input_value) {
return ucfirst(strtolower(str_replace(' ', '',strip_tags($input_value))));
}
$fname = beautify($_POST['reg_fname']);
对每个表单输入字段使用 etc 调用它。
推荐阅读
- android - WorkManager 数据的非原始类型
- python - 在 Windows 上安装 GDCM -- ImportError
- python - Python pandas groupby 类别和整数变量导致 pandas last 和 tail 差异
- r - r - 使用 paste0 定义 read_excel 的路径
- sql - SQL Server:PIVOT 数据存储在多行到列中
- php - 每 5 分钟运行一次函数
- google-apps-script - 其他谷歌驱动器中的PDF
- vb.net - Kendo UI - 图表 ValueAxis 数字格式
- python - 访问实例变量,AttributeError
- r - R 为每行运行 T 检验/方差分析,每行有 2 组,有 3 个样本