Welcome back.
I'm having a lot of fun.
So what is it that I want to do in the PHP file?
Well, when we submit this data, I want to create two types of arrays.
I want to create an errors array and I want to create a data array.
What is an array?
Don't worry about exactly what it is, just think about it as a very big container that allows us to
store things inside of it.
It's almost like a container.
I think that's the best way I can describe it.
The errors array is only going to be filled when there are errors. And the data array is going to contain our
success status, whether we are successful or whether we have failed in our form submission.
And it's also going to contain a message or it's going to contain that errors array if there are any.
So it's going to be quite interesting.
Let's code up the PHP together.
It'll be very quick and then we'll finish off with JavaScript.
Let's get cracking.
So all we need to do is we need to go to our file here.
You can see our directory is empty by the way, we've only got an index.html file.
And remember we've got very, very simple HTML here.
We've got two form controls and a button.
Now, you guessed it,
let's create our PHP file. And let's just call it php-server-file, and remember the extension .php.
So there we go.
We've got our PHP file.
Remember syntax with php
is this.
There is our PHP.
Okay, so let's get started here.
I want to set up two arrays
as I mentioned.
One for errors,
and one for the data.
How do we set up an array?
Well, you know, JavaScript,
we use a variable keyword like let or const.
In PHP, we don't.
In PHP, we use a dollar sign ( $ ) and here we want to call our array
"errors".
And we use the square bracket notation just like JavaScript.
Then I want to set up another array called data.
Right.
Right
now they are empty.
You can see we've just set up two empty arrays.
The next thing I want to do is I want to create our error messages,
right.
How do we do that?
Well, it's very, very simple.
We can access the IF statement. Again, its very similar to JavaScript. And this is what's cool about server
side code, my dear students, is that once you learn one server side language, it's a lot quicker
for you to learn others, because the syntax is largely the same.
Anyway, we've got an IF statement and we want to say right, IF ... let's just go to our browser here ...
we've got this form,
IF the user doesn't fill anything in the username, we want an error.
And you'll notice on the client side, in our index file, we don't have the required attribute.
In other words, we're not performing client side validation.
We're going to do it on the server side.
Right now the user can submit this, but we don't want that to happen.
So we want to check right,
IF it's empty ... "if what is empty,
Clyde?" Well, remember we've got this PHP global variable POST.
And we know we've called our variable username - if that is empty,
then we want to execute something, right?
We want to execute something between these curly braces.
Do you understand what it is we've done?
We've used the "empty" word in PHP.
We're saying if that POST variable is empty, then what do we want to happen?
Well, we want to populate our errors array.
We want to create a new error array where we push in a name of username and we assign it the value of
our message, which is "Your funky username is required".
Does it make sense?
I hope so.
Just think about what it is we've done.
We're checking to see whether that POST variable contains a key:value pair,
where it's key is username, and if it's empty, we want to then populate our errors array with a key
of username and assign it a message of "Your funky username is required".
That's all we've done. And now we can do exactly the same to create our next error message.
Can you give it a go?
Give it a go with the favFood.
If we got our index.html file,
we have given the food variable a name of favFood.
So just pause the video here quickly, and try and create your own custom error message, but this time
targeting the key of favFood.
All right, all right, all right.
Hope you gave it a go.
If not, don't worry.
We literally just do the same as we did above.
If it's empty ...
if what is empty?
Well, if the POST variable does not contain a key of
that's right,
favFood,
then we want to access our errors array
we want to now push in a key called favFood and we want to give that, or assign that, the value of our
message, which is "Choose a food, man".
Very, very simple.
And there we go.
That is literally all our error messages.
The next thing I want to do, right, is to check whether we have any errors
and create our data variable
sorry,
our data array, accordingly.
What do I want to do first?
Well, let's deal with the case where we do have errors, right.
And if we do have errors, we know that our errors array is not going to be empty.
Right.
We know that our errors array is not going to be empty.
That's all that, that syntax means.
And in that case, I want to access our data array that we've defined above.
Let me just scroll up here.
Remember, we defined two arrays, the data array and the errors array.
I'm now accessing the data array. And I want to push in here two keys.
The one is going to be success, and that is going to be false because we know we're not in a success
state.
The other thing I'm going to do is we're going to access our data array,
push in another key, this time called errors.
And we are going to assign that the value of our entire errors array.
It really is this simple.
And just like JavaScript, we can use an ELSE statement.
Now we know we're dealing in the success case, because now we know that our errors array is empty.
Meaning if we scroll up that both username and favFood have been received by the server.
In that case, it's very simple.
All we want to do is grab our data array, define its key of success to the value of not false,
true, because now we are in a success state.
And then the final thing I want to do only in the success state is I want to give this data array a
key of message.
And this is going to be our success message.
We can say "Success.
The data has been submitted to the server."
There we go.
And finally, let's echo back our results to the client, aka the browser
brow ... browser
sorry, I'm not thinking straight today.
And I want ... and I want the format to be in JSON.
So all we need to do is access PHP's echo function, and it's json_encode function and you can see my
IDE is already telling us what it returns.
It returns the JSON representation of a value and I want the data array returned to us.
Oh, my dear, dear students, this is it.
This is all our PHP.
This is all our server side code.
Very simple, right?
We're not dealing with anything complex.
I don't want to be complex because server side code, as I keep mentioning, can get very complex and
very long and beyond the scope of this course.
So just remember what it is we did.
We set up two empty arrays, one for errors, one for our data.
We create our error messages by saying if either of those values are not present when the server receives
the data, then create an error message.
Put it in our errors array.
Then we define our data array depending on whether we are in an error state or success state.
And then finally we send it back to the browser.
And by sending "it" back, I'm just saying we're sending our "data array" back to the browser.
And remember, this data array is either going to contain errors, in which case the success value will
be false, or the success value will be true, and it's going to contain a success message.
Why don't we stop here. In the next lecture,
we're going to code up AJAX.
It's going to be super, super exciting.
I can't wait. See you now.