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.
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.
- Form tag: it looks like this <form></form>. Everything else gets placed in between these tags.
- Input types: there are several different types the one we will will use today is <input type=”text”>
- 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.
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.
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.
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 echo $_GET['foo'] ?? 'Not Set'; ?><br>
If you refresh the page you should see this.
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.
However, we did not change the the $_GET variable names. So the $_GET variable is reading “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.
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
<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>
Please like, share and subscribe.
I hope you enjoyed the article. If I was able to help you please consider a tip for the content.
One Time Tip