This week was all about connecting the front end to the backend through forms. The form can be made using HTML and CSS, and you can write JavaScript to validate the data. However, to use the data you have to collect it, which can be done with PHP.
$_GET
vs $_POST
The difference between $_GET
and $_POST
deals with how the data is shared with the server. In simple terms, a GET request has user data being shared through a URL string. This is good for sending a small amount of data that is okay to be visible to the public. Generally $_GET
is used for search query strings.
On the other hand you have $_POST
which sends a PHP file. You can send a lot more data this way and it is possible to have it encrypted so that you can maintain some amount of privacy. This is the preferred method for sending most data to the server because of those attributes. For example, user login data should always be passed with $_POST
and never with $_GET
.
Connecting HTML Forms to PHP
On my Sandbox Website I have built a simple form that takes a first and last name and then greets the user when they press submit. This form has no JavaScript to validate it, however I did write a little bit of PHP on the destination page to gather and echo that data. You can find the form here or follow the link at the bottom of this post.
When the HTML form posts the data to the server it is stored in a global variable. In the PHP script the data can be extracted, processed, and then printed or saved on the database.
Right now the PHP script does not do much more than echoing data. I am looking forward to next week when I start learning SQL. With SQL I will be able to collect and save data to the server and I can start making more interesting web apps.
Links
Sandbox Module 8 Form: http://simon-liles.epizy.com/Module8.html