Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Advanced PHP
  • Discussion

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Web Application Development

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Version 2.0 Changelog 0:09
    • Updating the Site's Current Form to Use the POST Method
    • Creating Admin Website
    • Dynamically Generated Department List

Transcription: Web Application Development

Hello again, and welcome back to Educator.com's Advanced PHP with MySQL course.0000

In today's lesson, we are going to be continuing development of our web application.0005

What we are going to be doing is updating to a new version, version 2.0.0011

What we are going to be doing is incorporating the information that we learned in our last lecture about the HTTP POST method.0015

This slide lists the different changes that we made to the web application.0022

One of the first changes that we made is: we updated all of the site's current forms that submit data 0027

with the intention of doing something-- for example, the Contact Us form and the Checkout form--0037

both of those either email an order's information or email something to the website administrator.0042

Additionally, viewCart.php, because that is something that submits order information--the number of a particular item that you want--0055

causes a total to be calculated; we are going to use the POST method there, too.0066

We are going to update all of those forms to use the POST method.0071

And because we are using the new superglobal we were introduced to, called $_POST, we are going to create a new method,0073

called processPostVar, which works the same way as our processGetVar does.0084

We use it to create short forms of POST variables; it checks to see if the POST variable was provided.0091

If it was, it sets the value of the short variable equal to it; if not, it sets it equal to null.0097

If we go ahead and take a look at one of the scripts (for example, contactUs.php), you can see here:0102

at the beginning, where it processes the form variables, you can see now that it has two variables that it processes:0110

the action POST variable and the contactInfo POST variable, which is an array of contact information.0115

It uses this processPostVar method to process that variable, because these variables are now submitted by a POST,0122

because if we go down and look at the HTML output for this, where we see the form where the user inputs0131

their name, email address, and their comments, you can see that we have updated it to use the POST method.0138

And so, this and checkout.php and viewCart.php have all been updated to use the POST method.0144

We are going to go, really quickly, and look at our documentation, and see that, in our utility file,0150

we have a new function called processPostVar.0157

And like I said, it functions the same as the processGetVar: it tests if a particular is set; it returns the value of it if it was, and returns null otherwise.0163

So, now that we have learned about this POST method, in this lesson, we are going to set up the beginnings of an admin website.0178

And our admin website is going to allow us to add items to our store and add items (for example) to departments.0185

We are not going to implementing the full functionality of that site today, but we are going to be setting up the forms that we are going to use.0194

We are going to be setting up two forms: one that is for adding a new item to the store, and also one that is for adding an item to a department.0199

So, it adds an existing item to a particular department.0207

What we have done is: if you look at the version 2.0 of this web application, you can see that in that directory,0210

there is a new directory called admin that we have created.0220

That is where we are going to be storing all of our admin site files.0222

There are three files that are being created: admin.php, which is the home page, and then addItem.php 0227

(which is going to be our page that we are going to use to add an item to the store) 0233

and addItemToDept.php (which is what we are going to use to add an item to a department).0236

So, when we add a new item to the store...let's say it's a football we want to add to the Sports department--it is going to enable us to do that.0241

One other thing to note is: first of all, let's go ahead and look at what this admin store looks like.0250

So, in the version 2.0 directory, if we click on the admin folder and go to admin.php, we can see the homepage: Welcome to the EducatorStore.admin site.0257

And on the left-hand side, it has links to the different tasks that you can perform.0267

Right now, we have only set up two tasks; and we are going to be adding more as this course progresses.0270

For example, when we click on the addItem task, we can see that a form shows up that is eventually going to allow us to add a new item to our store's database.0275

It allows you to input a name, a price, and a description, and then select from three different image file extensions.0283

It also has the form addItemToDept.php; and when we click on that, what it allows you to do is enter an item's ID and a department's ID.0290

And it creates the association between the two of those.0300

Both of these forms that we have set up, if we look at the source code...the form definition...0303

I don't know why the source code is not showing up right, but let's go look at the actual source behind it.0338

If we look at addItemToDept.php (I don't know why I couldn't find that in there), we can see that0343

it sets up a form that submits the item ID and the department ID to itself, using the POST method.0351

And so, in this first Output HTML section, what it does is: if no action is provided to the form 0361

(an action is a POST variable that we use), it is going to say "output that form."0370

If you go ahead and submit that form, then what it is going to do is have a section that is going to process that.0373

Right now, it is not going to add an item to the department, because we haven't gone over 0379

how to write to files yet, which is how we are going to first enable to do that.0383

So, for this sample application, if we do it, we are just going to output a message that says, 0386

"Item ID would have been added to this department" with department ID.0391

And if we look up at the top here, we can see that we have used the processPostVar method to process the action for the form,0395

and then also to get short variables for the item ID and the department ID.0403

One thing that we do up here, as part of validation for this form, is make use of our itemExists and deptExists functions,0407

which are part of our file I/O library, and it is only going to allow this form to be valid if you submit an item ID and a department ID0413

that are actually items and departments that exist in the store.0425

The thing I wanted to mention was that, if you look at this script, we have an include file.0429

The pages look similar to our regular store, but we have created some new constants called HTML_ADMIN_HEADER and HTML_ADMIN_FOOTER.0438

And those are just slightly-modified versions of our header and footer files that are located...0448

if we go back in the includes directory, we can see that we have two new files here: adminHeader and adminFooter.0454

So, all of these admin pages are going to make use of these new admin header and footer files.0461

That is why this admin site looks the way that it does.0467

For example, to demonstrate this form, if we go ahead and type item 1001 (which we know exists; we know that department1 exists),0471

and we are going to say "Add it to the department," it is going to say, "Item 1001 would have been added to department number 1."0481

And in a future lesson, we are going to go ahead and add the functionality that actually does that.0486

Now, as mentioned, one of the things that it does is: it tests to make sure that both the item exists in the database, and the department exists.0490

Right now, there are only three departments in the database; and we know that they have the department ID's 1, 2, and 3.0498

So, if we try to add in item 1001 to department 4, that test at the beginning is going to test that the item and department already exist in the database.0503

It is going to say, "An item ID or department ID provided was not valid."0511

Now, for addItem, it doesn't have full functionality right now, but one thing that it does do is:0517

when we add a new item to the store, we are going to need to generate an item ID for it.0525

For example, right now, all 6 items in the store have the ID's 1001 through 1006.0530

When we get to the point where we will be adding a new item to the store, we are going to take the ID of the last value,0535

the numerical ID of the last item added, and we are going to increment it by 1.0541

So, the next item we add to the store is going to be item 1007, in this particular case,0545

because, if we look at our items.txt, we can see that we have 1006.0552

So, what we have done is added another data file in the includes directory, called lastAdded.txt.0557

What that does is: that has the ID of the last item that we added.0565

And what that does is: that is going to allow us to easily see, when we start implementing adding a new item to the store--0570

it is going to allow us to easily pull what the last item added was, and then go ahead and increment that.0577

Otherwise, we would have to do more file I/O by searching through the entire items.txt file to figure out what the last item added was.0581

So, it is a way to simplify that; so we now have this lastAdded.txt file that is going to enable us to do that.0589

In addItem.php, when you submit the form (and let's go ahead and try that...baseball...fifteen dollars), and we go to add the item,0597

it is going to say, "The item you added would have been assigned item 1007."0618

And that is because, if we go and look at our lastAdded.txt file, it has item 1006.0621

And then, it says, "These are the values that you submitted to..." it uses the POST superglobal to get this information.0628

And we haven't added this file; this is a mock setup right now, because we haven't learned how to write to files yet.0636

But what it did was: in the script, in the beginning here, what we did was: if the action of the script was to add an item,0642

then we called this getLastItemAdded function, which is a new function that we have added to our file library0653

(which we are going to look at in a second) that goes out and reads that lastAdded file to find out what the ID of the last item added was.0659

And then, in this case, it adds 1 to it; and it says, "This is what the item ID would be if you had added this item to the store,"0667

because it is going to get incremented by 1.0672

If you go ahead and look at the documentation for this version, we can see, in the file library, there is a new function called getLastItemAdded.0676

And what that does is: as it says, it retrieves the ID of the last item added to the store.0693

And because all of our functions that open files...for example, we have functions that are used 0699

to open our items data file and our departments data file...because there is this new data file, we have also created0707

this helper function that is used in this method, called openLastAddedFile, which opens that file lastAdded.txt.0712

And we have created a constant in our config file to set the path to that particular file.0720

This method is just going to open it, and return false if it is not able to successfully open it.0726

And then, all we do is: as we had done in our other file I/O examples, all we are doing is reading the first line from the file, using the fgets method.0732

We say, "This was the last item ID that was created"; we close the file; we test to make sure that the item ID is valid,0743

that it is not empty, that that first line is not empty, and that it was numeric; and then we return the value.0750

And so, that is how that getLastItemAdded function works.0754

The other thing to mention is that, in our store.php page, on our main site in this version, we have updated it,0764

because before, we used to list the departments and then all of the different items in the department.0773

Because now we are getting to the point where we are going to be adding new items to the store,0778

and we don't want to have all the items listed on the one page--let's say we had 100 items--0782

we are just going to list the departments here.0786

So, we have changed this so that it just dynamically generates links to all of the different departments.0787

And if we go and look at that code in store.php, we can see that now, instead of manually outputting links0792

to each department and each item, we just loop over all of the different departments.0805

And here, we are using our getDepartments method that we learned about in the last web application lesson0810

that is going to get an indexed array of all of the departments in our store.0814

And then, we are going to output a link to that department, using that new method that we developed, called outputSimpleDeptLink.0819

And so, now we have set up this...almost you could say...body of our admin application.0829

And in our next web application lesson, after we learn how to write files, we are going to implement that functionality0835

of adding the item to the store and of being able to add an item to a particular department.0841

That ends today's lesson; thank you for watching Educator.com, and I look forward to seeing you next time.0847