Michael Crawley Michael Crawley - 4 years ago 134
PHP Question

PHP - Class object error

I have created a php person class and a very basic form. The form is to output the data to another page. This is a scaled down version it does not have all of the coding included but I think I have the jist of it. I cannot figure out why it is not working. It cant be anything big nor do I think I am missing any steps. Note my code below:

I'm not getting any errors at all... That data being keyed into the form is not rendering to my proceslogin.php page.

This is my form.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>
<div>
<form id="login" method="post" action="proceslogin.php">
<label for="firstname"> First Name: </label>
<input type="text" id="firstname" name="firstname" maxlength="100" tabindex="1" />

<label for="lastname"> Last Name: </label>
<input type="text" id="lastname" name="lastname" maxlength="100" tabindex="2" />

<input type="submit" id="submit" name="submit" value="Retrieve Full Name"/>

</form>

</div>

<body>
</body>
</html>


This is my class

<?php
//Person Class
class Person{
//attributes for the first and last name of the class
private $firstname;
private $lastname;

/*Constructs the function*/
public function __construct(){
}

/*Destroys the function*/
public function __destruct(){
}

/*Get function*/
public function __get($name) {
return $this->$name;
}

/*Use the set function*/
public function __set($name, $value) {
$this->$name=$value;
}

/*Whis is what retrieves the values from memmory*/
public function retrieve_full_name() {
$fullname = $this->firstname . ' ' . $this->lastname;
return $fullname;
}
} //End of class
?>


This is the page the class object is to render in.

<?php

require_once('websiteconfig.inc.php');

?>





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Process Login</title>
</head>
<div class="container" id="shadow">
<div>
<?php
//include for the header
require_once(ABSOLUTE_PATH . 'header.inc.php');

//This is the inlcude for the class files
require_once('class/person.class.php');

//Instantiate person class
$person = new Person();

//sets atributes for both firs and last names
$person->firstname = $_POST['$firstname'];
$person->firstname = $_POST['$lastname'];
?>
</div>

<p>

<?
echo 'Your full name is ' . $person->retrieve_full_name() . ' .' ;
?>


</p>
<div>

</hr>

</div><!--End of Body-->
<?php
//include for the footer
require_once(ABSOLUTE_PATH . 'footer.inc.php');
?>
</div><!--end of header-->

<body>
</body>
</html>

Answer Source

You are using $lastname and $firstname when pulling from the _POST variable, which it should just be the name. You also had firstname set where lastname should be.

So this is how your php code should look.

PS: look into Smarty, it will help clean this all up.

<?php
        require_once('websiteconfig.inc.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Process Login</title>
</head>
<div class="container" id="shadow">
  <div>
    <?php 
        //include for the header
            require_once(ABSOLUTE_PATH . 'header.inc.php');

            //This is the inlcude for the class files
            require_once('class/person.class.php');

            //Instantiate person class
            $person = new Person();

            //sets atributes for both firs and last names
            $person->firstname = $_POST['firstname'];
            $person->lastname = $_POST['lastname'];
        ?>
  </div>
  <p>
    <?
echo 'Your full name is ' . $person->retrieve_full_name() . ' .' ;
?>
  </p>
  <div>
    </hr>
  </div>
  <!--End of Body-->
  <?php 
            //include for the footer
                require_once(ABSOLUTE_PATH . 'footer.inc.php');
            ?>
</div>
<!--end of header-->

<body>
</body>
</html>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download