PROCESSING FORMS PHP7 USING $GET BEGINNERS GUIDE LESSON 8

Processing forms in PHP7 for beginners can be hard to understand. Let’s break it down for you before we actually start coding. In the last lesson we manually created URL query strings.  We then used the $_GET method to get sections of the string and display them on a page. In programming we call this “Parsing A String“.

In this lesson we will be using HTML forms to create a query string in the URL. We will then “Parse The String” with PHP7 using the $_GET variable.

For those of you just joining us here are the files from the last lesson. Download and unzip them into your htdocs. The name of the folders is called mywebsite. Here are files for this lesson.

Only cool people share!

CREATING A FORM FOR PROCESSING IN PHP

To create a form we use the form tag with input fields and a submit button. Remember these are 3 essential ingredients to creating a form. This will help you in the future to never forget that there are 3 things that you need to take care of before your form will work. Let’s go over them again one more time in a numbered list.

  1. Form tag: it looks like this <form></form>. Everything else gets placed in between these tags.
  2. Input types: there are several different types the one we will will use today is <input type=”text”>
  3. Submit button: There are different ways to write a submit button. For simplicity we will use <input type=”submit”>

If you need a refresher on HTML forms please go to our HTML5 Forms For Absolute Beginners lesson.

Start out by opening our doo.php page. Make sure you are outside the PHP tag and insert this form.

<form>

<input type="text" name="foo">

<input type="submit" value="Submit">

</form>

Creating A URL String Query With Form

Make sure that you save the file then open it up in your browser. You should see a little form on that page that looks like this.

PROCESSING FORMS PHP7 USING $GET BEGINNERS GUIDE

Type something into the box then hit the submit button. I will type in foo into the box. After hitting the submit button look at the URL.

results from submitting form

This should look familiar to you from our last lesson. Instead of manually creating a link with a query we are creating a form that creates the query string.

Multiple Terms In Text Box

Now type in 2 different terms into the text box. I will type in foo bar. After I click on the submit button or hit return on my keyboard I see this in the URL string.

Forms with URL String

Parsing The URL With PHP7

In the last lesson we learned how to parse strings coming from a URL by targeting the Parameter“. We did that with the $_GET variable in PHP7. Somewhere on your doo.php page lets create that. We are going to use the “null coalescing operator (??)” a new feature in Php 7.

[php]<?php echo $_GET[‘foo’] ?? ‘Not Set’; ?><br>[/php]

If you refresh the page you should see this.

Results from query string with a form

Adding Another Input Field For Processing

Most of the time we will require more than one input field. Lets see what happens to the URL string when we add another text box. Lets change the name attributes to fname and lname for this process.

<form>

<input type="text" name="fname">

<input type="text" name="lname">

<input type="submit" value="submit">

</form>

 

After entering my name and hitting the submit button I see this in the URL string.

Results From URL String

However, we did not change the the $_GET variable names. So the $_GET variable is reading “Not Set”.

Variable Not Set

In order to get values from 2 different text boxes we will use 2 $_GET variables. Here is the code for fname and lname. We are using the “null coalescing operator (??)” to show the results in Php 7.

<?php

echo $_GET['fname'] ?? 'Not Set<br>';

echo $_GET['lname'] ?? 'Not Set<br>';

?>

When I enter my first name and last name I can see it echo out on the page.

Processing Forms in PHP

More About $_GET in PHP7

$_GET cannot process binary data like images or documents. So you would not use $_GET to upload images or a PDF file. We will cover this in a later lesson.

Adding Bootstrap To The Form And Results

Let’s tidy up our website a little. We are already using the Bootstrap CSS and JavaScript library for formatting. We will use those CSS attributes to format the form and the results. Here is a the code to use.

<div class="row">
<div class="col-sm-6">
<form>
<div class="form-group">
<label for="fname">First Name:</label>
<input type="text"class="form-control" name="fname">
</div>
<div class="form-group">
<label for="lname">Last Name:</label>
<input type="text" class="form-control" name="lname">
</div>
<input type="submit" class="btn btn-success btn-lg" value="submit">
</form>
</div>
<div class="col-sm-6">
<?php
$firstName=$_GET['fname'] ?? 'Not Set';
$lastName=$_GET['lname'] ?? 'Not Set';
echo '<p>'.$firstName.' '.$lastName.'</p>';
?>
</div>
</div>

 

PROCESSING FORMS PHP7 USING $GET BEGINNERS GUIDE LESSON 8 was last modified: October 29th, 2021 by Maximus Mccullough
PROCESSING FORMS PHP7 USING $GET BEGINNERS GUIDE

2 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.