A Super-Simple PHP Tutorial for Beginning to Code — Part 2

John Coonrod
May 22 · 3 min read
Photo by Austin Distel on Unsplash

Purpose of this part:

  • To be able to communicate between pages. For example, on one page you enter data, submit it and then display it on another page.
  • Click here to get back to Part 1


  • Basic understanding of HTML5 forms and inputs, Note — in the old days, in order to validate inputs or offer pop-up calendars, you needed to use javascript. Now HTML5 has that built in.
  • Basic understanding of HTML Tables.

Basic Concepts

A browser has 3 ways to communicate to the server:

  • GET (when you submit a form, placing the key/value pairs in the URL,
  • POST (when you submit a form in POST mode, transmitted to the server without being in the URL)
  • COOKIE (Stored in your browser and transmitted to the server with every call)
  • These are read on the server in pre-defined associative arrays $_COOKIE, $_GET and $_POST. (All of these are actually transmitted together in a larger array $_REQUEST but you rarely need to know that).

Example of $_GET

  • In your directory, create index.php as a entry page that says you want to see a table with a number of rows and columns. Note, there is actually no hp in this page. That’s not unusual in a home page!
  • Create a second page results.php that is jumped to from the form. You’ll see it receives the parameters from index.php in as key/value pairs in the $_GET array

Example: $_COOKIE

  • Often, you will want to have your app remember what you told it before. You do this by setting cookies. This is a bit tricky — cookies are stored on in your browser but are set by a message from the server before it writes anything to your screen, so you have to run the setcookie($key,$value,$expires) function first.
  • By default, cookies expire when you close the browser, but you might want them to last for, say, a million seconds, which is about 2 weeks.
  • Let’s modify our index.php function to require you to login with your name first. This is a trivial type of authentication. A really secure authentication will likely depend on the type of database you use in Part 3.

So, what’s going on here? In a PHP logical expression ! means “not.” So if you’ve passed in a name from a form, it is stored in a cookie. Otherwise, read it in from a cookie. If name is something other than a null string, say hello and prompt for rows and columns. Otherwise, ask as the name to be entered in a form. The “logout” option passes a null string.

Problem Set

  • Modify the app to store the settings for rows and columns in cookies and display them as default values when prompting for them in the form. In other words, make these values “sticky.”
  • Add a third page which creates a table like results.php but with the number displayed running in reverse order.

What you might want to explore before going to Part 3 on databases:

  • From the results page, how to export and download the table as a csv or json file.
  • From a new page, how to import a table from a csv or json file.

On to Part 3 —Databases!

Everything connected with Tech & Code