Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

PHP Lessons - Lesson 1 - Working with Forms

16/04/2025, by Ivan

In everyday life, we get information from television, radio, the internet, or through live communication. Websites also receive information from users, but they do so in a special way — through forms. Forms can be compared to paper questionnaires or official application forms. Despite the limited number of form elements, forms allow websites to gather all the necessary user data.

To get the most out of this lesson, you should review basic HTML form creation tutorials.

Before starting this first lesson, let’s create the foundation of our application — a class that will manage our website.

Create a folder called class and place a file simpleCMS.php inside it, which will contain our site management class. Also create index.php, which will launch the site management. Create an empty messages folder and a style.css file. You’ll have the following structure:

class/simpleCMS.php

messages/

index.php

style.css

The content of simpleCMS.php:


We’ll leave the methods empty for now and fill them out later.

The content of style.css:

* {
  margin: 0;
  padding: 0;
}

body {
  font: 12px "Lucida Grande", Sans-Serif;
  background: #ccc;
}

#page-wrap {
  width: 500px;
  margin: 50px auto;
  padding: 20px;
  background: white;
}

h1, h2, h3 {
  font: 28px Georgia, Serif;
  border-bottom: 1px dotted #ccc;
  margin: 0 0 10px 0;
}

.clear {
  clear: both;
}

input[type="text"], textarea {
  padding: 3px;
  border: 1px solid #666;
  width: 350px;
  margin: 0 0 15px 0;
}

input[type="text"] {
  font: 28px Georgia, Serif;
}

textarea {
  height: 100px;
  font: 12px "Lucida Grande", Sans-Serif;
}

label {
  background: #666;
  color: white;
  padding: 2px 6px;
}

.post {
  margin: 0 0 20px 0;
}

The content of index.php:



    
    Learning PHP
    
  
  
    

Let’s understand how this works. When the site is loaded, index.php runs. Right now, it shows nothing. We’ve included the class and created an object. That object has methods we can call at the appropriate time — which we’ll detect using if statements.

Global variable $_GET

HTML forms have the method attribute (get or post). Depending on the method, submitted data is stored in $_GET or $_POST. Here’s an example:

Name: Age:

Clicking Submit takes us to:

http://sitename/welcome.php?fname=Peter&age=37

Now we can access values with:


$_GET is an associative array created from the URL parameters. You can pass data without a form too:

URL: http://sitename/example.php?number=45


Global variable $_POST

$_POST works just like $_GET, but its data is not visible in the URL. Here’s a similar example:

Name: Age:

This leads to: http://sitename/welcome.php


Now let’s build the message submission form using POST, while the site will be controlled using GET:

public function display_admin() {
  $content = '';
  $content .= '
'; $content .= '
'; $content .= ''; $content .= '
'; $content .= '
'; $content .= ''; $content .= '
'; $content .= ''; $content .= '
'; $content .= '

Back to Home

'; return $content; }

This code will be used when we want to add a message. By default, we’ll show the messages. Let’s say we want the form to show when admin=1 is in the URL:

http://test/index.php?admin=1

So modify index.php like this:

display_admin();
} else {
  echo $obj->display_public();
}
?>

Now update display_public() so the homepage has a link to the form:

public function display_public() {
  $content = '';
  $content .= '

Add a message

'; return $content; }

If your page shows garbled characters, create a .htaccess file and add:

AddDefaultCharset utf-8

This forces UTF-8 encoding on your server.

Now we have a working form that sends data to the same page. You can try switching the form’s method to get:

$content .= '
';

This way, form data appears in the URL. We’ll handle that data in the next lesson when we learn to work with files and save the entries.

Прикрепленные файлы