0

I'm just relearning PHP, except it's OOP this time.

So I'm doing my classes/functions lesson and I decided to try my hand at a user-inputted function and echo at that information.

My code:

<?php

  $f_name = $_POST['firstname'];
  $l_name = $_POST['lastname'];
  $agee = $_POST['age'];
  $name = new Person($f_name, $l_name, $agee);

  class Person {

    var $first_name;
    var $last_name;
    var $age;

    function __construct($firstName, $lastName, $myAge) {

      $this->first_name = $firstName;
      $this->last_name = $lastName;
      $this->age = $myAge;

      echo "My name is $firstName $lastName, and my I'm $myAge years old.";

    }

  }

?>

<!doctype html>
<html lang="en">
  <head>
      <meta charset="utf-8">
      <title>Inserting</title>
  </head>
  <body>
    <form action="enteredfunction.php" method="post" autocomplete="off">
      <input type="text" name="firstname" placeholder="First name">
      <input type="text" name="lastname" placeholder="Last name">
      <input type="text" name="age" placeholder="Age">
      <input type="submit" value="Insert">
    </form>
  </body>
 </html>

And I get the following errors:

Notice: Undefined index: firstname in C:\xampp\htdocs\ooptutorial\classes\enteredfunction.php on line 3

Notice: Undefined index: lastname in C:\xampp\htdocs\ooptutorial\classes\enteredfunction.php on line 4

Notice: Undefined index: age in C:\xampp\htdocs\ooptutorial\classes\enteredfunction.php on line 5
My name is , and my I'm years old. 

I know what my error is, but I'm not sure what to do for it.

Any help would be appreciated.

Thomas Hutton
  • 793
  • 5
  • 15
  • 34

2 Answers2

2

Check if it's empty first

if (
  !empty($_POST['firstname']) &&
  !empty($_POST['lastname']) &&
  !empty($_POST['agee'])
){
$f_name = $_POST['firstname'];
$l_name = $_POST['lastname'];
$agee = $_POST['age'];
$name = new Person($f_name, $l_name, $agee);
}
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • I'm sorry, I completely to put in the original post. I did try that, however, nothing echoes after I hit submit. – Thomas Hutton May 28 '16 at 23:24
  • Right. Does it even make sense to instantiate the class without user input? If so, then you should define some default values. The notices are because you're attempting to use undefined variables. `empty` allows you to verify that first. – Jeff Puckett May 28 '16 at 23:27
  • Yes, I see now. I'm an idiot. Another question, can I check if it's emtpy without doing 3 different checks? – Thomas Hutton May 28 '16 at 23:31
  • 1
    Never mind if (!empty($_POST)) will do a check for the entire form. – Thomas Hutton May 28 '16 at 23:31
  • That will probably work most of the time. But it's not as precise, and will throw the same notices if anyone posts any form to a page with this class. So while this works fine in this example page, you want to think about the OO pattern decoupling your classes from the forms so that they can be reused elsewhere, such as in a standalone API wherein you would want to validate others' code. It's just more portable, doesn't leave loose ends, and I thought I should mention it since you mentioned you're trying to learn OOP. – Jeff Puckett May 29 '16 at 02:18
0

Your problem is the $_POST values you are looking for aren't there. Try doing var_dump($_POST) and seeing what is there.

Also, try making sure the action attribute on the form contains the name of the file its in or try leaving it empty.

blazerunner44
  • 657
  • 8
  • 19