使用 PHP 的基本注册表单 [英] Basic Registration Form with PHP

查看:78
本文介绍了使用 PHP 的基本注册表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮帮我吗?我是 PHP 的初学者,仍在学习该语言的基础知识.我目前正在学习如何创建会话.显然,当我尝试书中的一个示例时,存在一个错误.HTML 表单如下所示:

Can you please help me. I'm a beginner in PHP and still learning the basics of the language. I'm currently learning how to create sessions. Apparently there's a bug when I try out one of the examples in the book. The HTML form looks like this:

<form action="session_registration_form_script.php" method="post">
    <label for="name">Name:</label>
    <input type="text" name="name" />
    <label for="email">Email:</label>
    <input type="text" name="email" />
    <input type="submit" value="Register" />
</form>

PHP 脚本类似于:

<?php

    // Initialize session data
    session_start();

    /*
     * If the user is already registered, display a 
     * message letting them know.
     */

     if(isset($_SERVER['username'])) {
         echo "You're already registered as $_SESSION[username]";
     }

     // Checks if the form was submitted
     else if($_SERVER['REQUEST_METHOD'] == 'POST') {

         /*
          * If both the username and email fields were filled
          * out, save the username in a session variable and 
          * output a thank you message to the browser. To 
          * eliminate leading and trailing whitespace, we use the 
          * trim() function
          */
          if(!empty(trim($_POST['username']))
          && !empty(trim($_POST['email']))) {

              // Store escaped $_POST values in variables 
              $uname = htmlentities($_POST['username']);
              $email = htmlentities($_POST['email']);

              $_SESSION['username'] = $uname;
              echo "Thanks for registering! <br />",
              "Username: $uname <br />",
              "Email: $email <br />";
          }

          /*
           * If the user did not fill out both fields, display 
           * a message letting them know that both fields are 
           * required for registration
           */

           else {
               echo "Please fill out both fields! <br />";
           }
     }

     // If the form was not submitted, dispalys the HTML form

     else {

?>

<?php } ?>

当我在浏览器上输出这两个文件时,浏览器报告:致命错误:无法在文件路径"和代码行的写入上下文中使用函数值.

When I output both of the files on the browser, the browser reports that: Fatal Error: Can't use function value in write context in 'path of the file' and line of the code.

请帮忙解决这个问题,并随意添加任何你认为对我有用的东西.提前致谢...

Please help out by fixing this and feel free to add on anything that you might find useful for me. Thanks in Advance...

推荐答案

我可以从您的代码中看到许多问题.

There are a number of issues I can see with your code.

 if(isset($_SERVER['username'])) {
     echo "You're already registered as $_SESSION[username]";
 }

应该

 if(isset($_SESSION['username'])) {
     echo "You're already registered as ".$_SESSION['username'];
 }

此外,当您的表单字段名为 name 时,您正在寻找名为 username$_POST 变量.

Also, you're looking for a $_POST variable called username when your form field is called name.

至于您看到的实际错误,那是因为您同时使用 emptytrim 的方式 - 您应该分别检查 2.例如

As for the actual error you're seeing, that's because of the way you're using empty and trim together - you should check the 2 separately. E.g.

$uname = isset($_POST['username']) ? trim($_POST['username']) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
if(!empty($uname)  && !empty($email)) {

这篇关于使用 PHP 的基本注册表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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