-2

请帮我填写此注册表单。对我来说很明显我的 PHP 脚本中有几个错误。我的 HTML 表单如下所示:

<form action="script_1.php" method="post">
    <label for="name">Username:</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($_SESSION['username'])) {
         echo "You're already registered as" .$_SESSION[username];  // Corrected on Stack Overflow
     }

     // 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
          * trim() function.
          */

          $uname = isset($_POST['username']) ? trim($_POST['username']) : '';   // Corrected on Stack Overflow
          $email = isset($_POST['email']) ? trim($_POST['email']) : '';         // Corrected on Stack Overflow

          if(!empty($uname) && !empty($email)) {        // Corrected on Stack Overflow

              $_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, displays the HTML form
     else {

?>

<?php } ?>

我需要它来检查输入字段是否包含某些内容,然后执行操作。如果这些字段中没有插入任何内容,我还需要它来执行不同的功能。

4

3 回答 3

0

在您的表单脚本中,您将输入名称作为名称

要么将其更改为

或更改您的 script_1.php 全部更改

$_POST['用户名']

$_POST['名字']

于 2013-05-17T01:15:05.533 回答
0

在你FORM使用<input type="text" name="name" />所以你应该 使用$_POST['name'] instead$_POST['username']因为$_POST['username']根本不存在。你没有textbox命名username

 echo "Thanks for registering! <br />",
              "Username: $uname <br />",
              "Email: $email <br />";

如果你在 php 中回显变量,你应该这样做:

echo "Thanks for registering! <br />",
echo "Username: ". $uname."<br />";
echo "Email: ". $email."<br />";
于 2013-05-17T02:00:38.480 回答
0

在您的表单脚本中,您将输入名称作为名称

<input type="text" name="name" />

要么将其更改为

<input type="text" name="username" />

或更改您的 script_1.php 全部更改

$_POST['username']

$_POST['name']
于 2013-05-17T01:18:07.353 回答