I am having troubles getting the value from my variables submitted via a web form using a PHP class file.
Here is my structure of the web page:
- Order Form Page
- Process.php Page
- Book.php Page
I can easily get the user data entered (on Order Form Page), process, and display it on the Process.php page. The issue is that I must create a Book class and print the details of the data using the Book class.
I have an empty constructor printing out “created” so I know my constructor is being called. I also am able to print the word “title” so I know I can print to the screen by using the Book class.
My issue is that I can’t get values in my variables in the Book class.
Here is my variable declaration:
private $title;
Here is my printDetails function:
public function printDetails () {
echo "Title: " . $this->title . "<br />";
}
Here is my new instance of the book class:
$bookNow = new book;
Here are my get and set functions:
function __getTitle($title)
{
return $this->$title;
}
function __setTitle($title,$value)
{
$this->$title = $value;
}
I do have four other variables that I’m looking to display as well. Each of those have their own variable declaration, a line in printDetails, and their own setter and getter.
Lastly, I also have a call to the Book class in my process PHP. It looks like this:
function __autoload($book) {
include $book . '.php';
}
$bookNow = new book();
Any help, much appreciated. It must be something so very small (I’m hoping).
_setTitle()should have only one argument, and remove the second$character in the assignment statement (also forgetTitle()), like so:Now call it like this:
And don’t use double underscore unless you are dealing with magic functions. I think that is bad practice. Also check the case of your class – even though php is very relaxed about case. If your class file is called “Book.php” then your constructor should be called with uppercase just to make sure (and parentheses too probably).
EDIT: OK this is the class I made and it works just fine, I tested it:
Make sure you get a value in the POST, like this:
NB: DO NOT put the above statements inside the ‘book.php’ class file.
If your form submission went wrong, you will see ‘No Title Found!’, otherwise you will see the correct title.