-1

Hello everyone! can someone guide me for the following problem? I was trying to make a php form, but it shows the error ( Undefined index: firstname in C:\xampp\htdocs\form_require1.php on line 53/55) when I run it. please help me

enter code here    <?php // define valiables and set to empty values $firstnameErr = $lastnameErr = ""; $firstname = $lastname = ""; if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (empty($_POST["firstname"])) {
    $firstnameErr = "Name is required";
  } else {
    $firstname = test_input($_POST["firstname"]); }
 if (empty($_POST["lastname"])){
$lastnameErr = "Name is require"; } else { $lastname = test_input($_POST["lastname"]);  } } function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
return $data; } ?>    `enter code here`    <form action="<?php echo htmlentities($_SERVER["PHP_SELF"]) ?>" method="post"> Your firstname <input type="text" name="firstname" /> <span class="error">* <?php echo $firstnameErr;?></span> <br><br> Your Lastname <input type="text" name="lastname" /> <span class="error">* <?php echo $lastnameErr;?></span> <br><br> <input type="submit" value="Submit" /> </form> <?php  // } else {
echo 'Your Details'; 
echo "<br>";
echo  'Fistname: ' . $_POST["firstname"];
echo "<br>";
echo  'Lastname: ' . $_POST["lastname"];
Marfy
  • 1
  • 1

1 Answers1

1

Basically the $_POST variables you want to use are not set. To be sure the form passes the right variables use var_dump($_POST); to see what variables it passes on.

For more information about your error see: php notice undefined variable and notice undefined index

Community
  • 1
  • 1
Fin
  • 386
  • 1
  • 15