PHP - Ошибка объекта класса - PullRequest
0 голосов
/ 04 марта 2012

Я создал класс php person и очень простую форму. Форма предназначена для вывода данных на другую страницу. Это уменьшенная версия, она не включает в себя всю кодировку, но я думаю, что у меня есть суть. Я не могу понять, почему это не работает. Это не может быть что-то большое, и я не думаю, что я пропускаю какие-либо шаги. Обратите внимание на мой код ниже:

Я вообще не получаю никаких ошибок ... Эти данные, введенные в форму, не отображаются на моей странице processlogin.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>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>

Это мой класс

<?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
?>

Это страница, на которой должен отображаться объект класса.

    <?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>

Ответы [ 2 ]

1 голос
/ 04 марта 2012

Вы используете $lastname и $firstname при извлечении из переменной _POST, которая должна быть просто именем. Вы также установили имя, где должна быть фамилия.

Так вот как должен выглядеть ваш php-код.

PS: посмотрите на Smarty, это поможет очистить все это.

<?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>
0 голосов
/ 04 марта 2012

Не совсем уверен, что вы имеете в виду, но вы устанавливаете имя дважды:

$person->firstname = $_POST['$firstname'];
$person->firstname = $_POST['$lastname'];

Еще одна вещь, чтобы проверить, имеет ли значение $ _POST ['$ firstname'] значение, попробуйте повторить его

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...