-1

Im trying to save form input after submitting the form, I tried this:

public $username;
public function loginForm() {
    $this->login = 
    "<form action='' method='post'>" .
    "<input id=first_name type=text class=validate name=username value=$username>" .

    if (isset($_POST["submit"]) && (empty($_POST["username"]))) {
        $this->username = $_POST["username"];

It still says that variable username is not defined, what am I doing wrong?

yivi
  • 42,438
  • 18
  • 116
  • 138
Jay
  • 1
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Rotimi Mar 21 '18 at 17:37
  • It checks if it isset then checks if its empty? and if it is empty then it stores the empty variable?, and if its not empty then what – Richard Mar 21 '18 at 17:38
  • @Richard I have the same `$this->username = $_POST["username"];` in the else block – Jay Mar 21 '18 at 17:41

2 Answers2

0

Fixed by putting the var inside the function:

public function displayLogin() {
    $username = isset($_POST['username']) ? $_POST['username'] : '';
Jay
  • 1
-1

You are missing some quotes here:

public $username;
public function loginForm() {
  $this->login = 
  '<form action="" method="post">' .
  '<input id="first_name" type="text" class="validate" name="username" value="' . $username . '">' .

if (isset($_POST["submit"]) && (empty($_POST["username"]))) {
    $this->username = $_POST["username"];
jonas3344
  • 201
  • 1
  • 7
  • No, this is when creating a form outside PHP, the form I made is still in the PHP code – Jay Mar 21 '18 at 17:44
  • HTML-attributes always need quotes, no matter where you create them.You should post the whole code by the way, not just partial stuff. – jonas3344 Mar 21 '18 at 17:46
  • This is not true, you would need them in the following example: `"
    "` because doing: `"
    "` would cause the HTML to read it like `action ="method=post"` but you can write classes like `class=classname` when creating the form inside PHP code
    – Jay Mar 21 '18 at 17:54
  • This is not a PHP-issue. I checked it again, it can work without quotes, but it's bad style and I wouldn't guarantee that every browser is able to parse it. – jonas3344 Mar 21 '18 at 20:43