hyhae
hyhae

Reputation: 43

Taking input from an echoed text field in PHP and HTML?

How do you get input from the text field in the following code?

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

?>

This is giving me username is an undefined index. I need to grab the inputted data in the text field that was echoed

Upvotes: 2

Views: 61

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72289

Just remove the first $Iusername = $_POST['username']; and it will work fine.

Explanation:-

Each time when your page is loaded the above line is going to execute. And since $_POST['username'] have no value at that time (because it will have value if and only if form submits), your above statement fails and give you the warning of Undefined Index.

Upvotes: 2

Related Questions