Hi folks,
I'm kind of stressed out right now about this class I'm taking, PHP and MySQL...for beginners :)

Anyways, he has us read the Murach PHP and MySQL, and then afterwords we are to write code for an assignment.

This week I had to create two files: book.php and processBookType.php.

In the book.php I have to:

Create a "Book" class
Name the file “book.php”.
Add a public method “displayBookTitle()”.
Accept $_POST array as single argument.
Make use of an “if / else” statement along with isset() and empty() to check if the book title is available.
Echo an appropriate error message when desired condition is not met.
Concatenate the book title retrieved from the $_POST array to this string: “The following book was submitted: “
Echo the concatenated string.

In the processBookTitle.php I need to:

Make use of “include()” function to reference Book class.
Create a Book object.
Call the Book Object’s “displayBookTitle()” method.
Pass in the $_POST array as single argument.

This is my code for book.php:

class Book {
    public function getdisplayTitle('title') {
        $displayTitle = $_POST['title'];
        if(isset($_POST['title'])) {
            $title=$_POST['title']; }
        if(empty($title)) {
            echo 'Need Book Title'; }
        else {echo '<p> "The following book was submitted: "' . $title . '</p>'; }
        return $displayTitle;   



And this is my code for processBookTitle.php:


$publisher = new Book ('name', 'location');
$publisher->name = '';
echo $publisher->name;
$publisher->location = '';
echo $publisher->location;

$publisher->displayTitle ($_POST); 


The error I get:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting '&' or T_VARIABLE in C:\xampp\htdocs\assignment\book.php on line 3

So, does anyone know what I'm doing wrong? The instructor (at a University) says I should be able to figure it out myself. This is all new to me so I don't know. Have worked on it a few days.

Thanks for any input.

Apparently the string is suppose to output to book.php file? That is unclear to me also.

The "1." under the book.php code should not be in the code. I don't know why it shows up there.

else {echo '<p> "The following book was submitted: "' . $title . '</p>';

Although I am not sure this is the cause of your error, you don't actually need the double quotes inside this string unless you want them to display.

echo '<p>The following book was submitted: ' . $title . '</p>';

just noticed this.

public function getdisplayTitle('title') {

you have a string constant in your function definition where you should have a variable.

public function getdisplayTitle($title) {

you may also want to change the order of checking and getting the value of your POST variable here...

$displayTitle = $_POST['title'];
if(isset($_POST['title'])) {


First, don't get the class occupy your head too much. you can add them later. The most important steps in writing program is to established the barebone of your project (logic wise). Without laying out the proper logical flows of your project, it will confused you even more..

Relax, and think about your homework without panicking. Your instructor instructed you to do the following..

  1. Create a "Book" class
  2. Name the file “book.php”.
  3. Add a public method “displayBookTitle()”.
  4. Accept $_POST array as single argument.
  5. Make use of an “if / else” statement along with isset() and empty() to check if the book title is available.
  6. Echo an appropriate error message when desired condition is not met.
  7. Concatenate the book title retrieved from the $_POST array to this string: “The following book was submitted: “
  8. Echo the concatenated string.

Looking at the items above, we can easily use the process of eliminating the less important for us to build the barebone of our program.. Creating the book class is not the top notch priority in writing the barebone script. The public method is part of the class. The most important item of all the items above is the keyword POST. Whenever these terms "POST, GET, REQUEST" comes up, it is 99.9999% sure that a form is involved..

Now lets write this form.. we can call this book.php

 <!-- filename book.php ..yet but later on. -->
<form method = "post" action = "processBookType.php">
<input type = "text" name = "title"/>
<input type = "submit" name = "submit" value = "submit" />

Notice the form html codes above? This is the very first thing we need to write. Without this, book class can do nothing. Especially, if we are going to instantiate the class using the POST value of the form. From this form, we can write every possible php codes to handle the data posted. The form above is set up with very important triggering mechanisms for the processor to work. First is the submit button. Did you notice the name "submit"? We will using this for the "if/else, isset()" on the item number 5 above. While the othe input where the name is "title", we will use it to echo the "The following book was submitted:", but before doing that, we need to check if it is not empty.

The php codes below demonstrate number 5 to 8 .. save as processBookType.php

if((isset($_POST['submit'])) && (!empty($_POST['title']))) {

echo "The following book was submitted: ".$title;


 echo "You must type title";    


The if statement on the php codes above uses a boolean isset() to check if the form has been posted or submitted by clicking the submit button.. and then it is followed by php operator && which is also known as AND, and then we use the NOT or ! operator to check if the title is not empty.

save those document in your xampp server, and then let me know if it is working. Up next, we will write the book class and finished this project. I just want you to have a clear thoughts of what is going on script wise.

The "1." under the book.php code should not be in the code. I don't know why it shows up there.

I fixed the formatting. Please be sure to properly format your code.

cscgal: Thanks. I realize now you have a certain format to follow. Sorry I didn't notice before.

Hearth and Veedeoo,

Thanks so much. I do have it working now. But I had to put the statements (isset, etc) in both the book.php and the processBookTitle.php (is that right?).

This has been really helpful. It is late here, and I'll continue looking at it tomorrow. I'll put up the code tomorrow with the corrections. Thanks.

ok, here are the full codes, I hope this will help you. Please do not hesitate to ask more questions. I should be able post response tomorrow. My wireless keyboard is running out of battery. So, I really need to post my answer as quickly as possible.

Here we go, first updated files

save this as book.php

<!-- filename book.php ..this is updated version. -->
<form method = "post" action = "processBookType.php">
<label>Please type book title</label>
<input type = "text" name = "title"/>

<input type = "submit" name = "submit" value = "submit" />

save this as processBookType.php this file meets the following requirements.. include -> book class.

 include 'book.class.php';
 if((isset($_POST['submit'])) && (!empty($_POST['title']))) {
 $status = true;
 $title = $_POST['title']; 


 $status = false;
 ## you can  uncomment the code below if you want to use the variable equal to null
 //$title = NULL;
 ## this justify our empty argument , but I prefered the NULL above.
  $title = "";

 $publisher = new Book ($status, $title);
 echo $publisher->getdisplayTitle();


Brief info. about the processBookType.php above.. I gave two options for the title when it is submitted empty. One is $title = NULL; and equal to "".. both will do fine. However, it a much bigger application and if it going to be on a production server it should be set to NULL.

Now, the book.class.php.. As you notice in the include function, I have it as book.class.php.

save this file as book.class.php This class is simple, but this will allow you to extends at a later date..

      class Book {

        public function __construct($form_response , $title ){

             $this->formResponse = $form_response;
             $this->title = $title;

        function getdisplayTitle() {

             $displayTitle = "";

             ## form response is not false
             if(($this->formResponse == true) && (!empty($this->title))){
             $displayTitle .= '<p> The following book was submitted: "'.$this->title.'"</p>';     


       else {

         $displayTitle .='<p> "Need Book Title"</p>'; 


      return $displayTitle;

Save all files to your xampp server, and then using your favorit browser, go to the book.php. Fill up the form and then hit submit. If the class is working properly you should read "The following book was sub....". Once it is confrimed to be working, go back to the form again and try to submit an empty form. Script should respond "Need Book Title"..

How do you expand the class?

To expand the class, you can add a new function after the curly bracket below the return $displayTitle. A new function can be as simple as

 function displayAuthor(){

 echo $this->author;


If a new function is added in the class, any variables used by this function that hasn't been defined yet by any function below the constructor, should be defined and added to the constructor. So, in my example above.. my class constructor should be something like this.

  public function __construct($form_response , $title, $author ){

             $this->formResponse = $form_response;
             $this->title = $title;
             $this->author = $author;

You can add more as your program needed extra functions. However, if you think that you will be adding a lot of them, then VAR needs to be declare just below the class declaration. A little too many of them in the constructor will make it look a java..

This is really great and I have printed this out. I'm actually understanding this so much better. Thankyou. We did already have a form under a file named "assignment3.html" which we worked on last week. I asked the instructor why we were using an html file and not a PHP but he just said not to worry about it. :)

So it is good to see how this is done using straight php files. So, I went over my files, but the assignment3.html has the form action with the "processBookTitle.php".

I will probably be back later in the quarter with another question. Next week we are creating the database using phpMyAdmin, etc. I think I will be ok with that.

I would like to donate **to daniweb but I can't see where you have that? **Let me know. I feel it is a great resource. I'll check back to find where I can do this later today. Thank you again.

cscgal should be able to direct you to the right direction about donating to daniweb. Good luck on your school project.

Member Avatar for diafol

I raised the matter with Dani. There is no sponsor/donate function at present, but this may change sometime in the future. This is what Dani had to say:

If you want to donate, you can send a PayPal donation to cscgal@daniweb.com

With a big thank you, obviously.

Thanks! I have to go get a green dot card to replenish my paypal and then I'll send a donation. Thanks everyone

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.