This is purely for example and educational purposes.
The resulting contact form you will create with this example is extremely insecure. Do not use it on a live website.
- Create
website
directory on Desktop - Open terminal and test PHP
php --version
- Browse to the new
website
foldercd ~/Desktop/website
- Start the php server
php -S localhost:9999
- Create the first php file
echo "Hello" >> index.php
- Open your browser to http://localhost:9999
- Get the bootstrap Basic Template and paste it into
index.php
- Delete the local references to css and js, and replace with Bootstrap served from CDN. Make sure jQuery comes before Bootstrap's js file.
- Get the Example Navbar and paste it above the
<h1>
tag inindex.php
- Delete a significant chunk of the
.navbar
. Keep a few links and the search box. - Create a
.container>.row>.col-sm-8+.col-sm-4
- Move our
<h1>
tag to the.col-sm-8
div - Add
<h3>My Sidebar</h3>
to the.col-sm-4
div - Create a new
.row
- Place
<h3>My Footer</h3>
into the new.row
- Copy and page our
index.php
to a new file namedabout.php
, and a new file namedcontact.php
- Change the
<h1>
for each page to reflect its purpose (ie About Us, Contact Me. - Identify the Repeated HTML Problem. To fix the menu across all of our pages, we would have to edit each one individually.
- Identify areas of our page that are repeatable. These are areas we can abstract.
- Abstract the header, sidebar, and footer into template files.
- Include the new files in each content page.
include( 'header.php' );
etc - Fix the Repeated HTML Problem
We've significantly simplified the creation and maintenance of a website as opposed to flat HTML.
- Identify the Metadata Problem - Current page has no context (active menu, page title).
- Identify the File Organization Problem - Our file paths are hard-coded into every page.
WARNING - Do not use this code on a live site. This is only for example purposes and is very insecure.*
GET requests are a means of accepting user input that is repeatable, thus sharable, and ultimately visible to the browser's history.
- Ony dynamic portion of URLs is the Query. Proceeded by a question mark
?
, the query can contain many key-value pairs, separated by ampersands&
. - Open
contact.php
and add<pre><?php print_r( $_GET ); ?></pre>
to the.col-sm-8
column. - Visit these example URLs
- Example: http://localhost:9999/contact.php?some_key=some_value&another_key=another_value Represented in PHP:
Array( [some_key] => some_value [another_key] => another_value )
- Example: http://localhost:9999/contact.php?my_name=jonathan&my_search=red+birds Represented in PHP:
Array( [my_name] => jonathan [my_search] => red birds )
- Visit your terminal and notice how the the URL shows the values of GET requests.
- Add the following HTML to the
contact.php
file within the main column.
<form action="" method="">
</form>
action
Attribute tells the form what file it should submit to. By default (if no value is given), it submits to the current page.method
Attribute tells the from what type of http request the form will use to submit the data to the server. By default (if no value is given), the method ismethod="get"
.- Knowing about the defaults, we can tell that this form will submit its values to the
contact.php
page, using theget
request method. - There for, the above html is equivalent to the following html.
<form action="contact.php" method="get">
</form>
- Add a button and input field to the form so that we can start collecting user submitted data.
<form action="contact.php" method="get">
Name: <input name="my_name" type="text">
<button type="submit">Submit</button>
</form>
- Now when a user visits the
contact.php
page, they can submit a form that sends the data back to the PHP server. - Add another field where the user can type a bigger message.
<form action="contact.php" method="get">
Name: <input name="my_name" type="text">
Message: <textarea name="my_message"></textarea>
<button type="submit">Submit</button>
</form>
- Add the following code above the
header
incontact.php
if ( !empty( $_GET ) ) {
mail( 'your-real-email-address@example.com', 'Contact form submission from ' . $_GET['my_name'], $_GET['my_message'] );
}
- It is not a coincidence that the form method is "get", and the values are stored within the $_GET PHP system variable
- Identify the problem with using a GET request with this type of user data. Repeatable, shareable, and browser histories all become liabilities in this case.
POST requests are another way to accept user submitted data, without passing the values within the URL. They are not repeatable, shareable, nor can their values be tracked by the browser history.
- Edit the form's
method
attribute to the value ofpost
:<form action="contact.php" method="post">
- Change our debugging PHP to show the values of the
$_POST
system variable.<?php print_r( $_POST ); ?>
- Revisit the page, and submit the form with new data. Notice that the data is available to PHP with the
$_POST
variable, and no longer in the URL. - Adjust our code that sends the email to use the
$_POST
variable instead of$_GET
if ( !empty( $_POST ) ) {
mail( 'your-real-email-address@example.com', 'Contact form submission from ' . $_POST['my_name'], $_POST['my_message'] );
}