Tip:
Highlight text to annotate it
X
We are looking at our PHP
program that is designed to do an automated inventory
and we are working on the delete button. Here is what is happening:
We come in, this is our first time
visiting the page, we come in here and we select an item.
Let’s say we want to delete the “Dandelion Tree”. When we hit the Delete button
The DEBUG statement shows that we went into our deleteRecord( )
stub. We haven’t written the code yet, but the program has gone into the function,
but, our table doesn’t show up
and it shows that we have an error on line #94 “Invalid argument”
To get back to where I was, I can just
redo the URL and the page starts back as a first-time visitor again.
I just highlighted the URL and hit ENTER. That treated me like
a first-time visitor. If I break the page
and just do a refresh (CTRL R)
you can see that it see me as a returning visitor and it doesn’t change
anything. So, that’s a little trick you can use as you develop your programs. (This is done in FireFox 4.0)
Now, let’s do that code walk-through.
Going into the code
go into our code
The first thing we are going to look at is coming into the page
and PHP is going to grab the current
PHP filename that we are working on. We are going to store that in a variable
called $self. I’m going to show you where we are going to use that in just a minute.
Also, down in the form we have a
hidden submit field (hidSubmitFlag) and we are going to have PHP ask, “Does that
exist in the $_POST[ ] array?”
and we look for the hidSubmitFlag variable. If it does exist,
that means that the user has been to the page before.
But, let’s say it doesn’t exist.
Well, then we come down to the else clause and build a hard-coded array
So, let’s say we are a first-time visitor. We are going to build the array, do the welcome
build the array,
We are going to skip over these functions,
although they get put into memory,
and as we get into the of the page we call the displayInventory( ) function.
So, the programs jumps up
to the displayInventory( ) code. (Notice I have the functions in alphabetical order.)
Here is where PHP builds the table and inserts the data from the array.
It makes $invenArray global and we build the table.
a row at a time. For each row,
we are going to loop through and get the column. So, it is a for each inside of a for each.
So, here are our columns. For each column, for each row.
All right, so now we have the table displaying,
and next we display the form
We populate the form with a for each using the data from the array.
Here is our listbox. And we go through the array records
and for each record, we pull out the ID field $record[0] and the description $record[0]
Here is where the $self variable comes in. When the user
clicks the Submit button on the form, the page is going to call/request itself from the server.
This form action=“ “ is going to call that file
name which is the current filename we are working on.
Down here, in the form, is the hidden submit flag
hidSubmitFlag, right before the submit button, and we give it a value of ’99’
which we just pulled out of our head. But, that’s going to signify “Delete”
All right. So, now we have the selection
from the list box (lstItem), we have the hidden submit flag with the value of “99” (hidSubmitFlag)
and we have the submit button (btnSubmit). When the user
clicks on the submit button the form
is going to take those three items, put them in a POST type ‘suitcase’ and send them off
to request this same file from the web server.
We go out to the web server telling it we want this form
The server is going to come in here and it is going to ask,
“Does the hidSubmitFlag exist?"
We will say, “Yes! It does!!”. We are going to take the value of this flag
put it into our $submitFlag variable
and just so we know what is happening, there is a
debug statement
We are going to have several options in the future. An add and delete
and we can have several different options of what a person does
on this page. Right now we are just going to do a delete.
So, we pulled the value “99” out of our hidSubmitFlag
and we go into the switch, find the match, and runt he
deleteRecord( ) function. PHP goes to that function
and it is going to delete the record from the array. We didn’t write that code yet,
we just have a stub in.
So, PHP does that
and then it is going to come down to the and do the
and display our inventory.
Well, when it displayed out inventory
it can’t find this variable $invenArray
Let’s go back to that error message.
On line #94 PHP can’t find this variable right here. It can’t find our array!
Well, why not?
You notice that when we went back out to the server the second time, as a repeat visitor,
we didn’t create an array.
All we have here is
the switch telling us to delete a record. But, there’s no array. The array is
only built the first time we requested the page and the server does not remember things.
So, what we are going to have to do
is use a session to remember this array
and then, when we come in here, we will recall that array
out on the server. We will tell the server, “Hey, remember that array?”
And we will bring up that session variable. That way we will have the array available
to delete the record and continue on.
So, this is a really good example of
the Web being stateless.
Each time you make a request
with a post going out to the filename listed in the action=“ “ attribute
each time you make a request for a web page, you are starting fresh.
So, as things stand right now this is as far as we can get until we
figure out session variables. And that will be in the next workshop.
The End.