简体   繁体   中英

Basic Registration Form with PHP

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>

and the PHP script is something like:

<?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: 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]";
 }

Should be

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

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

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. Eg

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

I tink its going to be a good idea to trim your variables after receiving them trom the global variable before checking if they arer empty

        $uname = trim($_POST['username']);
        $email = trim($_POST['email']);
      if(!empty($uname)  && !empty($email)) {
    //if(!empty($_POST['username']) && !empty($_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 />";
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM