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.
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") {
} }
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") {
} }
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") {
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.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps