首页 > 解决方案 > 函数没有接收到正确的输入

问题描述

<!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功能没有按预期工作。因为如果电子邮件不匹配,我不会收到回显声明。如果电子邮件不匹配,我希望得到回显声明。我的代码有什么问题?

标签: php

解决方案


将函数更改为只需要表单输入字段的名称,并将“美化”值返回给调用者。

function beautify($input_value) {
    return ucfirst(strtolower(str_replace(' ', '',strip_tags($input_value))));
}

$fname = beautify($_POST['reg_fname']);对每个表单输入字段使用 etc 调用它。


推荐阅读