如何将注册表格数据输出到当前页面和另一页面? [英] How to output registration form data to the current page and another page?

查看:92
本文介绍了如何将注册表格数据输出到当前页面和另一页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,如果提交的表单数据已成功且经过验证,如何将注册表单数据输出到当前页面(form.php)和另一个页面(profile.php)?我一直在尝试搜索操作方法,但找不到任何有用的方法.我尝试将操作更改为profile.php,希望在其中显示用户数据,但是当我这样做时,我无法在表单页面中显示错误.

As the tittle says, how do I output a registration form data to the current page(form.php) and another page(profile.php) if the submitted form data has been successful and validated? I've been trying to search how to do this but couldn't find anything useful. I tried changing the action to profile.php where I want the user's data to be displayed, but when I did that, I couldn't display the errors in my form page.

基本上,如果用户的输入不正确,我希望在表单中显示错误,并且一旦他正确填写了所有内容并提交了表单,我希望将数据显示在他的个人资料(profile.php)中.就像我在profile.php中执行<?php echo $_POST['username']?>一样,我希望他的用户名显示在profile.php中.有没有办法做到这一点?我对此还是有点陌生​​...

Basically, I want the errors to be displayed in the form if the user's input is incorrect and once he filled out everything correctly and submitted the form, I want the data to be displayed to his profile (profile.php). Like if I do <?php echo $_POST['username']?> in profile.php, I want his username to be displayed in profile.php. Is there a way to do this? I'm still kind of new to this...

我应该添加或更改任何内容吗?我不确定如何将数据输出到profile.php

Is there anything I should add or change? I'm not sure how I can output the data to profile.php

这是我的代码:

<?php
    error_reporting(E_ERROR | E_WARNING | E_PARSE);
    require 'core.inc.php';
    require 'connect.inc.php';

    if(count($_POST) > 0){
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $password_hash = md5($password);
        $age = $_POST['age'];
        $gender = $_POST['gender'];

        $validated = true;

        if(!preg_match("/^[a-zA-Z ]*$/",$username)){
            $usernameErr = " Only letters and white space allowed";
            $validated = false;
        } else if(empty($username)){
            $usernameErr = ' Enter your username';
            $validated = false;
        }

        if(strlen($username)>30){
            $error = 'Please ahear to maxlength of fields.';
        }

        if(empty($password)){
            $passErr = ' Enter your password';
            $validated = false;
        }

        if (!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = " Invalid email";
            $validated = false;
        } else if(empty($email)){
            $emailErr = ' Enter your email';
            $validated = false;
        }

        if(empty($gender)){
            $genderErr = ' Select your gender';
            $validated = false;
        }

        if(empty($age)){
            $ageErr = ' Select your age';
            $validated = false;
        }

        if($validated === true){
            $query = "SELECT `username` FROM `users` WHERE `username`='$username'";
                $query_run = mysql_query($query);

                if(mysql_num_rows($query_run)==1){
                    $error = 'The username already exists.';
                } else {
                    $query = "INSERT INTO `users` VALUES ('','".mysql_real_escape_string($username)."','".mysql_real_escape_string($password_hash)."','".mysql_real_escape_string($email)."')";
                    if ($query_run = mysql_query($query)){
                        header('Location: register_success.php');
                    } else {
                        $error = 'Sorry, we couldn\'t register you at this time. Try again later.';
                    }
                }   
        }
    }
?>
<!doctype html>
<html>
<head>
<style>
form{
    margin: 0px auto;
    width: 470px;
    margin-top:150px;
}
label{
     width: 75px;
    display: inline-block;
}
.error{
    color: red;
}
</style>
</head>
<body>
    <span class="error"><?php echo $error?></span>
    <br><br>
    <form action="register.php" method="POST" autocomplete="off">
        <h2>Register</h2>
        <label for="username">Username</label>
            <input type="text" id="username" name="username" maxlength="50" value="<?php if(isset($username)){echo $username;}?>">
            <span class="error"><?php echo $usernameErr?></span>
            <br><br>
        <label for="password">Password</label>
            <input type="password" id="password" name="password">
            <span class="error"><?php echo $passErr?></span>
            <br><br>
        <label for="email">Email</label>
            <input type="email" id="email" name="email" maxlength="30" value="<?php if(isset($email)){echo $email;}?>">
            <span class="error"><?php echo $emailErr?></span>
            <br><br>
        <label>Gender</label>
            <input type="radio" name="gender" id="male" value="male">Male
            <input type="radio" name="gender" id="female" value="female">Female
            <span class="error"><?php echo $genderErr?></span>
            <br><br>
        <label>Age</label>
            <select name="age" class="age">
                <option value> </option>
                <option value="18">18</option>
                <option value="19">19</option>
                <option value="20">20</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
                <option value="24">24</option>
                <option value="25">25</option>
                <option value="26">26</option>
                <option value="27">27</option>
                <option value="28">28</option>
                <option value="29">29</option>
                <option value="30">30</option>
            </select>
            <span class="error"><?php echo $ageErr?></span>
            <br><br>
        <input type="submit" value="Register">
    </form>
</body>
</html>

推荐答案

数据持久性

HTTP是无状态协议,因此它将不会记住不同请求之间的信息. 为了显示登录信息,您将需要保留此信息. 您已经拥有的INSERT INTO语句已经执行了此操作,因此在配置文件页面上需要做的就是从数据库中检索信息并使用SELECT SQL语句显示该信息.

HTTP is a stateless protocol so it will not remember information across different requests. In order to display the login information, you will need to persist this information. The INSERT INTO statement that you have does that already so all you need to do on your profile page is to retrieve the information from your database and display it using a SELECT SQL statement.

身份验证

您还需要知道要从数据库中检索哪个用户记录. 这通常是通过询问用户的凭据(例如,他的电子邮件地址和密码)来完成的. 基于此,您可以验证是否有一个具有这些凭据的用户,然后检索适当的数据. 您可能需要尝试这样的PHP登录系统教程: http://www.phpeasystep. com/phptu/6.html .

You will also need to know which user record to retrive from your database. This is usually done by asking your user for credentials (e.g. his email address and his password). Based on this, you can verify that there is a user with these credentials and then retrieve the appropriate data. You may want to try a PHP login system tutorial such as this one: http://www.phpeasystep.com/phptu/6.html.

编辑: 您还应该确保您的表单与SQL INSERT语句保持同步. 您的表单中有用户名,电子邮件,密码,性别和年龄,但是INSERT语句中只有用户名,电子邮件和密码,因此不会将性别和年龄添加到数据库中,并且数据也不会被记住.

Edit: You should also make sure you keep your form in sync with your SQL INSERT statement. You have username, email, password, gender and age in your form but only username, email and password in your INSERT statement so gender and age will not be added to your database and the data will not be remembered.

这篇关于如何将注册表格数据输出到当前页面和另一页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆