In this lesson we'll be programming a BHP script that outputs data.
A user has input it into a form on the same page as the form the BHP script will also do some basic
validation that will program into a function we'll do the following to all input a data we'll pass variables
through P piece each TMM special characters function to make sure data is transmitted securely will
trim.
We'll use the trim function to strip any unnecessary characters such as extra space tabs or brakes inserted
into the input fields
we'll use the strip lashes function to remove any backslash is from user input a data.
This helps add an additional layer of security and protects data integrity
before we get started.
I'll show you a demonstration of the end result.
You'll notice right now under the heading user input there's no information next to the field labels.
When I press submit we can see that the data that was input it into the form has now been output on
the same page.
Next to these text labels
since the HP variables are sent to the same page as the form we'll need to use the post method coupled
with our form action set to the super global variable server.
We'll get to that in just a second.
So let's go ahead and get started.
The file used in this lecture is available in the downloads folder the HMO you see here is just a basic
table that contains all our form elements.
So right now there's no p p in the script.
You can take a moment to go through it.
It's just a form and insight inside the form there's a table and it contains all of our form labels.
Our inputs and our buttons we've already named each input so we can see here next to the text label
name.
We have the name input and this input has the name attribute set to name.
Next we have website and website.
This input has its name attribute at the website followed by position experience
now employment status.
We've said it to e that is just to be more efficient
comments.
The name is set to comments
and then we have our two buttons.
The first thing we'll do is create our form action.
So what you're gonna do is you're going to take the following into the action attribute in your form
element so it's a little bit long.
The global variable server will send the submitted data to the same file as the existing file.
We've embedded the global variable server in an HMO special characters function and this provides an
additional layer of security by preventing anyone from injecting malicious javascript code into the
transmitted data.
So it's important to include that and you'll see that the global server variable is active BHP self
so that's this is what allows us to transmit this data to the same page that we're working on and that's
going to output the information on the same page as well
our method is already set to post.
So we don't have to worry about that so let's go ahead and start with our p HP script
so create your
HP text
we'll need to define the variables that we'll use to contain the input form data so enter the following
variables
and we're going to set each one of these variables to an empty value for no
and you'll see that the variable names we've used are the same names as the names that we've given our
input fields.
Now we'll have to create a function to validate the input the input the data and so you're going to
go ahead and create this function.
So we've called this function val and this this function has one argument called data
there's three statements in this function the first statement will trim the data so it'll remove any
unnecessary spaces and then it'll remove any unnecessary backslash is using strip lashes and we're securing
the data by using the HMO special characters function as well.
So these are the three basic ways we'll be validating the data.
So next we'll create an if statement that checks whether the user has submitted the form and we'll do
this using the server request method you'll see in a second
is you're going to go ahead and type in the following block of code
so you can see that we've set the global server variable here to request method which equals post and
what this does is it checks to see if any data has been posted and the only way data would have been
posted is if somebody click the Submit button.
Now if the data has been posted then each we've created a series of variables.
Remember we define these variables up here and we set it to an empty value.
Well if the form has been submitted these variables will now take on new values.
So the named variable will take the value in the name field the website variable will take the information
and put it into the website field and so on and you can see that all this data will be retrieved using
the post super global variable.
Now that information will be stored in our new variable
we've also enclosed our post super global variable in the function that we created the value function
and so each variable is going to be validated using the three validation techniques we've specified
in the function.
The last thing we need to do is just create the output and we'll do that using the P P equals statements.
This was the demo file.
I'm just going to switch now to this new file that we're working with.
So enter this block of code underneath your form your closing form tag and this is fairly straightforward
it's just a series of echo statements that is going to output the information that the user is input.
So once you've entered that block save your file and refresh your page and just test to see if it works
and it looks like it's working fine.
So we can see that all the data that was encoded into the form has now been output it on the same page
in the next lesson we'll be working with additional types of validation techniques.