php
  • html
  • forms
  • field
  • echo
  • 2016-03-12 144 views 2 likes 
    2

    如何從下面的代碼中的文本字段獲取輸入?從PHP和HTML中的回顯文本字段獲取輸入?

    <?php 
    
    echo "<form action='signup.php' method='POST'>"; 
    echo "<input type='text' placeholder='Username' name='username'/>"; 
    echo "<button type='submit' name = 'submit' value='Register' >Sign up</button>"; 
    echo "</form>"; 
    
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    
    // Create connection 
    $conn = new mysqli($servername, $username, $password,"vaccinations"); 
    $Iusername = $_POST['username']; 
    
    if(isset($_POST['submit'])){ 
    $Iusername = $_POST['username']; 
    echo $Iusername; 
    echo "whadddup"; 
    } 
    
    ?> 
    

    這是給我的用戶名是一個未定義的索引。我要搶這是呼應

    +1

    右後'$ conn',你有'$ Iusername = $ _ POST [ '用戶名'];'。而且每次你​​訪問這個代碼並且不提交表單(當你第一次訪問它時),它會產生這個警告。簡單的解決辦法:刪除這一行,因爲你把它定義中的'如果(isset($ _ POST [ '提交'])){'(無需定義兩次) – Qirel

    +1

    '$ Iusername = $ _ POST [ '用戶名']; '刪除它,因爲你'如果把它(isset($ _ POST [「提交」])){'條件,並且是足夠 –

    +1

    [PHP的可能重複:「通知:未定義變量」和「通知:未定義指數」 (http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Qirel

    回答

    2

    只需卸下第一$Iusername = $_POST['username'];,它做工精細的文本字段中輸入的數據。

    說明: -

    當你的頁面加載上面的線是要執行的每一次。而且,由於$_POST['username']沒有價值在那個時候(因爲當且僅當表單提交它具有價值),你上面的語句將失敗,並給你的Undefined Index警告。

    +0

    工作過,謝謝:D – hyhae

    相關問題