2013-05-17 148 views
-2

請用這個註冊表單幫我解決問題。很明顯,我的PHP腳本中有幾個錯誤。我的HTML形式如下:用PHP註冊表格

<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 } ?> 

我需要它來檢查,如果輸入字段包含的東西,然後執行操作。如果在這些字段中沒有插入任何內容,我還需要它來執行不同的功能。

+0

你的表單字段'name'不符合您的PHP這是尋找'username'。 –

+0

哦,我的話......對不起。我花了8小時學習PHP,現在我已經死了,但主要是,謝謝你的一切。你救了我... – Mosire

回答

0

在表單的腳本,你必須輸入名稱作爲名稱

要麼將​​其更改爲

或更改script_1.php改變所有

$ _ POST [ '用戶名']

$ _ POST [ '名']

0

在表單的腳本,你必須輸入名稱作爲名稱

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

要麼將​​其更改爲

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

或更改script_1.php全部變更

$_POST['username'] 

$_POST['name'] 
0

在您使用FORM<input type="text" name="name" />所以你shoulduse的$_POST['username']因爲$_POST['username']完全不存在 $_POST['name'] instead。您沒有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 />";