[英]Taking input from an echoed text field in PHP and 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";
}
?>
這給了我用戶名是一個未定義的索引。 我需要在回顯的文本字段中獲取輸入的數據
只需刪除第一個$Iusername = $_POST['username'];
它會正常工作。
解釋:-
每次加載頁面時,上面的行都會執行。 由於$_POST['username']
當時沒有價值(因為當且僅當表單提交時它才會有價值),您的上述語句失敗並給您Undefined Index
的警告。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.