Skip to content
Advertisement

Function not receiving correct inputs [closed]

<!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>

I’m suspecting the beautify function is not working as expected. Because I’m not getting the echo statement if the emails don’t match. I’m expecting to get the echo statement if the emails don’t match. What is wrong with my code?

Advertisement

Answer

Change the function to only require the name of the form input field and have it return the “beautified” value to the caller.

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

Call it using $fname = beautify($_POST['reg_fname']); etc for each of your form input fields.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement