0

When I compile my code inside of my browser I get the error, "
Notice: Undefined index: firstname in C:\xampp\htdocs\index.php on line 76
"

Here is the code:

<form method="post">
<div class="form-group">
<label for="name">First Name:</label>
<input type="text" name="firstname" class="form-control" placeholder="First Name"
value="<?php echo $_POST['firstname']; ?>" />
</div>
</form>

My goal with this is to save the user's data if an error occurs after they click the submit button. Any help would be appreciated. Thank you.

Julian
  • 11
  • 2

1 Answers1

0

You only "want" to output the variable/element's value if such variable/element exists. Test it via isset() first.

<form method="post">
    <div class="form-group">
        <label for="name">First Name:</label>
        <input type="text" name="firstname" class="form-control"
            placeholder="First Name"
            value="<?php echo isset($_POST['firstname']) ? htmlspecialchars($_POST['firstname']) : ''; ?>" 
        />
    </div>
</form>

see
http://docs.php.net/isset
http://php.net/language.operators.comparison#language.operators.comparison.ternary
http://docs.php.net/htmlspecialchars

VolkerK
  • 95,432
  • 20
  • 163
  • 226