dongmie3526 2014-02-23 15:59
浏览 22
已采纳

如何在我的注册表格中添加新字段[关闭]

I want to add extra fields in my registration form like date of birth, city, country, and marital status. I am new to PHP and PDO.

Here is my code:

PHP:

<?php
require 'core/init.php';
$general->logged_in_protect();

if (isset($_POST['submit'])) {
    if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) {
        $errors[] = 'All fields are required.';
    } else {
        if ($users->user_exists($_POST['username']) === true) {
            $errors[] = 'That username already exists';
        }
        if (!ctype_alnum($_POST['username'])) {
            $errors[] = 'Please enter a username with only alphabets and numbers';
        }

        if (strlen($_POST['password']) <6) {
            $errors[] = 'Your password must be atleast 6 characters';
        } else if (strlen($_POST['password']) >18) {
            $errors[] = 'Your password cannot be more than 18 characters long';
        }

        if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
            $errors[] = 'Please enter a valid email address';
        } else if ($users->email_exists($_POST['email']) === true) {
            $errors[] = 'That email already exists.';
        }
    }

    if (empty($errors) === true) {
        $username = htmlentities($_POST['username']);
        $password = $_POST['password'];
        $email = htmlentities($_POST['email']);
        $users->register($username, $password, $email);
        header('Location: register.php?success');
        exit();
    }
}

HTML:

<!doctype html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <title>Register</title>
    </head>

    <body>
        <div class="nav-bar" style="box-shadow:0 0 5px 0 rgba(0, 0, 0, 0.4);">
            <?php include 'includes/menu.php'; ?>
        </div>
        <!-- NAV BAR DIV closes here -->
        <div id="main-wrap" style="box-shadow:0 0 5px 0 rgba(0, 0, 0, 0.4);">
            <div id="container">
                <h1>Register</h1>

                <?php if (isset($_GET['success']) && empty($_GET['success'])) { echo 'Thank you for registering. Please check your email.'; } ?>
                <form method="post" action="">
                    <h4>Username:</h4>
                    <input type="text" name="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>">

                    <h4>Password:</h4>
                    <input type="password" name="password" />
                    <h4>Email:</h4>

                    <input type="text" name="email" value="<?php if (isset($_POST['email'])) echo htmlentities($_POST['email']); ?>" />

                    <br>
                    <input type="submit" name="submit" value="Register" />
                </form>
                <?php if (empty($errors) === false){ echo '<p>' . implode('</p><p>', $errors) . '</p>'; } ?>
            </div>
            <!-- Container DIV closes here -->
        </div>
        <!-- Main Wrap DIV closes here -->
    </body>

</html>

How do I add the extra fields?

  • 写回答

1条回答 默认 最新

  • dongxing2710 2014-02-23 21:14
    关注

    This is a very open-ended question. You'll need code to handle adding the input to the database, but that warrants a separate question.

    For the PHP, you'll need to decide how to validate each field, and then add another if block for each question. For the HTML, you'll need to add another input for each field.

    Example Code

    For date of birth, you should probably ensure that it's a valid date. And then you may want to validate age as well.

    You might use PHP code like this for birth date:

    //test date
    try {
        $max_date = new DateTime('13 years ago');
        $birth_date = new DateTime($_POST['birth_date']);
        if ($birth_date > $max_date) { {
            $errors[] = 'You must be at least 13 years old.';
        }
    } catch (Exception $e) {
        $errors[] = 'Birth date is not a valid date.';
    }
    

    You could then use the following to format the date for MySQL:

    $birth_date->format('Y-m-d');
    

    Relevant PHP manual pages:

    And you might use HTML code like this for birth date:

    <h4>Birth Date:</h4>
    <input type="text" name="birth_date" value="<?php if (isset($_POST['birth_date'])) echo htmlentities($_POST['birth_date']); ?>" />
    

    You could also format the input into separate fields for each part of the date and use drop down menus. I don't find this is necessary since the DateTime class can handle a wide variety of formats. However, I usually add a jQuery Datepicker to make it more user friendly.


    Other Fields

    The above should give you a good starting point. You can do something similar for each field. You need to show more personal effort if you expect people here to help.

    If you have specific questions about how to validate the other inputs, you should post a separate question (after first searching for the answer). Show what you've tried. Describe the result and explain how it's different from the expected result.


    Suggestions for Code Improvement

    1. I'd combine the two if blocks for username. If the username already exists, you don't need to validate the format.

      if ($users->user_exists($_POST['username']) === true) {
          $errors[] = 'That username already exists';
      } else if (!ctype_alnum($_POST['username'])) {
          $errors[] = 'Please enter a username with only alphabets and numbers';
      }
      
    2. Comparing the return value of empty($errors) to true or false is redundant. The return value is always a boolean. You only need if (empty($errors)) or if (!empty($errors)).

    3. You should use label elements in your HTML instead of h4 elements to make your HTML more semantic. You can then use CSS to style them however you want.

      <label for="fld-password">Password:</label>
      <input type="password" id="fld-password" name="password" />
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大