Hands-On Exercises #7 Superglobals $_GET and $_POST Source: Source: Connolly, R. and Hoar, R., (2018), Fundamentals of Web Development Before you do the exercises If you haven’t already done so, you should create a folder in your personal drive for all the exercises. This set of exercises requires a functioning webserver to interpret the PHP code. Exercise 7-1 Checking for POST 1. 2. Test lab7‐exercise7-1.php in a browser and notice that it contains a simple one field form that posts the results back to itself. The first thing to do is detect whether the page was reached through a get (a link followed) or whether the page was arrived by the form being posted. Edit the displayPostStatus() function as follows and test. function displayPostStatus() { if ($_SERVER["REQUEST_METHOD"] == "POST") { echo "Post detected"; } else { echo "No Post Detected"; } } Now when you post the form you see the post detected, and when you enter the URL and hit enter (GET) it does not see the POST. Next, let us use the values being transmitted to the server. Inside the code for when a POST was detected, we can print out the full contents of the $_POST array to see what variables we have using print_r. function displayPostStatus() { if ($_SERVER["REQUEST_METHOD"] == "POST") { echo ""; print_r($_POST); echo ""; } else { echo “No Post Detected”; } } You will see that the only field being posted (and can be seen in the HTML form as well) is alias. Comment these lines out for now and bring them back to help with debugging if needed. 3. Note: The tag defines preformatted text. Text in a element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code. We will now say "Hello user", where user is the value submitted through the input field alias. Modify our earlier if statement with the following and test. function displayPostStatus() { if ($_SERVER["REQUEST_METHOD"] == "POST") { echo "Hello ".$_POST['alias']; } } 4. This works, and will say hello to whatever you type into the box. Now let's do a little checking to ensure it's not blank. Place the single echo name inside and modify our if statement to check for blank. function displayPostStatus() { if ($_SERVER["REQUEST_METHOD"] == "POST") { echo ""; print_r($_POST); echo ""; if (isset($_POST['alias']) && $_POST['alias']!="") { echo "Hello ".$_POST['alias']; } else { echo "Post detected, but no value"; } } else { echo "No Post Detected"; } } 5. Test in browser. Now the page says hello if you type a name and other detects the post but is able to handle the post accordingly if the name is blank.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
 

Hands-On Exercises #7 Superglobals $_GET and $_POST Source: Source: Connolly, R. and Hoar, R., (2018), Fundamentals of Web Development

Before you do the exercises

If you haven’t already done so, you should create a folder in your personal drive for all the exercises. This set of exercises requires a functioning webserver to interpret the PHP code.

Exercise 7-1 Checking for POST

1. 2.

Test lab7‐exercise7-1.php in a browser and notice that it contains a simple one field form that posts the results back to itself.

The first thing to do is detect whether the page was reached through a get (a link followed) or whether the page was arrived by the form being posted.

Edit the displayPostStatus() function as follows and test.

function displayPostStatus() {

if ($_SERVER["REQUEST_METHOD"] == "POST") { echo "Post detected";

} else {

echo "No Post Detected";

}

}

Now when you post the form you see the post detected, and when you enter the URL and hit enter (GET) it does not see the POST.

Next, let us use the values being transmitted to the server.

Inside the code for when a POST was detected, we can print out the full contents of the $_POST array to see what variables we have using print_r.

function displayPostStatus() {
if ($_SERVER["REQUEST_METHOD"] == "POST") {

echo "<pre>"; print_r($_POST); echo "</pre>"; } else { echo “No Post Detected”;

} }

You will see that the only field being posted (and can be seen in the HTML form as well) is alias. Comment these lines out for now and bring them back to help with debugging if needed.

3.

Note: The <pre> tag defines preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code.

We will now say "Hello user", where user is the value submitted through the input field alias. Modify our earlier if statement with the following and test.

function displayPostStatus() {
if ($_SERVER["REQUEST_METHOD"] == "POST") {

echo "Hello ".$_POST['alias'];

} }

4. This works, and will say hello to whatever you type into the box. Now let's do a little checking to ensure it's not blank. Place the single echo name inside and modify our if statement to check for blank.

function displayPostStatus() {
if ($_SERVER["REQUEST_METHOD"] == "POST") {

echo "<pre>"; print_r($_POST); echo "</pre>";

if (isset($_POST['alias']) && $_POST['alias']!="") { echo "Hello ".$_POST['alias'];

} else { echo "Post detected, but no value";

}

} else { echo "No Post Detected";

}

}

5. Test in browser. Now the page says hello if you type a name and other detects the post but is able to handle the post accordingly if the name is blank.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY