GETting data

GET it

Rosie

The simplest way to send data from a browser to the server is through a URL.

Here's a URL to a page on OU's Moodle instance:

  • https://moodle.oakland.edu/course/view.php?id=242965

The URL has:

  • The protocol, https
  • The server, moodle.oakland.edu
  • The page path, /course/view.php
  • And some strange stuff on the end, ?id=242965

The stuff on the end is a query string. The ? signals the start of the string. There are parameters and value. So the parameter id has the value 242965.

You can pass more than one parameter, like this:

  • something?name=Rosie&cuteness=12

An ampersand (&) separates the arguments. You can pass lots of them.

Who's the cutest?

Here's the code from last time:

  • <?php
  •     $cutestDog = 'Rosie';
  •     $rating = 12;
  • ?><!DOCTYPE html>
  • <html lang="en">
  •     <head>
  •         <meta charset="UTF-8">
  •         <title>Cutest Dog</title>
  •         <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  •     </head>
  •     <body>
  •         <h1>Cutest dog</h1>
  •         <p>The results are in!</p>
  •         <p>Cutest dog: <?php print $cutestDog; ?></p>
  •         <p>Rating: <?php print $rating; ?> out of 10.</p>
  •     </body>
  • </html>

Let's change lines 2 and 3, to get data from the URL, like this:

  • $cutestDog = $_GET['name'];
  • $rating = $_GET['cuteness'];

So the program will be expecting two parameters in the URL: name, and cuteness.

Try this URL:

https://webappexamples.skilling.us/html/enter-php/cutest.php?name=Rosie&cuteness=12

In your browser, change the name and rating in the URL, and see what happens.

Try leaving out the data, that is, the ? and everything after it.

Calculations

You can do calculation, just as in VBA. Here's another example:

  1. <?php
  2.     $a = $_GET['a'];
  3.     $b = $_GET['b'];
  4. ?><!DOCTYPE html>
  5. <html lang="en">
  6.     <head>
  7.         <meta charset="UTF-8">
  8.         <title>Calculations</title>
  9.         <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> </head>
  10.     <body>
  11.         <h1>Calculations</h1>
  12.         <?php
  13.         $aTimesB = $a * $b;
  14.         print "<p>$a times $b is $aTimesB.</p>";
  15.         $aDividedByB = $a / $b;
  16.         print "<p>$a divided $b is $aDividedByB</p>";
  17.         ?>
  18.     </body>
  19. </html>

This one is expecting two GET parameters. It does some calculations, and shows the results.

Check out line 14:

  • print "<p>$a times $b is $aTimesB.</p>";

If you use double quotes (") around a string, you can put variable names in the string, and PHP will substitute their values. This doesn't work with single quotes. So...

  • print '<p>$a times $b is $aTimesB.</p>';

... would output exactly <p>$a times $b is $aTimesB.</p>, with the dollar signs and variable names.

Sometimes you need to add braces, if PHP can't work out what you mean. For example, you could use...

  • print '<p>{$a} times {$b} is {$aTimesB}.</p>';

The braces tell PHP that the thing between them should be replaced with a value. The braces themselves aren't output.

The code we've seen so far doesn't need braces, but you'll come across them later in the course.

OK, let's try it. Here's a URL:

https://webappexamples.skilling.us/html/enter-php/calculation.php?a=4&b=8

Try it.

Yay!

Here's one with the data missing.

https://webappexamples.skilling.us/html/enter-php/calculation.php

Try it.

Adela
Adela

What's NAN mean?

Not a number.

Calculations can be as long as you want

PHP can do complex calculations, just like VBA. For example:

  • $result = ($x + $y) * 4 - 27 / (90 + $z / 2.33);

() works the same as in VBA. Order of operations is as you expect.

Reflect

What would this code output?

  • $result = 2 + 3 * 4;
  • print $result;
If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.
Adela
Adela

14. It would do the * first, then the +.

Right!

Another example

This one computes a triangle's hypotenuse from its sides.

  1. <?php
  2. // Compute the hypotenuse from the two sides.
  3. $side1 = $_GET['side'];
  4. $side2 = $_GET['other_side'];
  5. $hypotenuse = sqrt($side1 * $side1 + $side2 * $side2);
  6. ?><!DOCTYPE html>
  7. <html lang="en">
  8.     <head>
  9.         <meta charset="UTF-8">
  10.         <title>Hypotenuse</title>
  11.     </head>
  12.     <body>
  13.         <h1>Hypotenuse</h1>
  14.         <p>Side 1: <?php print $side1; ?></p>
  15.         <p>Side 2: <?php print $side2; ?></p>
  16.         <p>Hypotenuse: <?php print $hypotenuse; ?></p>
  17.     </body>
  18. </html>

It grabs two values from the end of the URL. They must called side, and other_side. Then it computes the hypotenuse, and outputs the results.

Video walkthrough

Here's a walkthrough, showing how to do an exercise. First, a sample exercise. It's fake. Don't try to submit it.

- - CUT SCREEN HERE - -

Work out how many grams of dilithium crystal it'll take to ship some quonks from Blutol to Skintal. You can try my solution.

  • Each quonk takes 0.1 grams to ship (they're heavy).
  • Add 0.3 grams of overhead for a shipment (launching to the Blutol orbital platform, loading onto a freighter, unloading at the Skintal orbital, and dropping to the planet).
  • Add 0.4 grams for pirate insurance. It's optional.

Add input data to the end of the URL, like:

  • .../shipping.php?quonks=311&insured=n
  •  
  • .../shipping.php?quonks=9311&insured=Y
  •  
  • .../shipping.php?quonks=666&insured=y

Notice that insured can be upper or lowercase. Assume both params are present, and have no errors.

- - CUT SCREEN HERE - -

Here's how I would do it.

Exercises

Exercise

Circles

Write a PHP program to compute the circumference and area of a circle, from a radius in the URL. Make your output look like this program. Use 3.14159 as pi.

Submit your PHP file, and its URL on your server. To submit the file, attach it to your submission. Don't copy-and-paste its contents into the submission text area.

Exercise

Tips

Write a program that takes meal_amount from GET, and computes tips at different rates.

Here's some output:

Output

Try it:

https://webappexamples.skilling.us/html/exercises/tips/tips.php?meal_amount=60

Use the rates shown in the screenshot. Submit the URL of your solution, and a zip of your PHP file.

Exercise

Party planner

You run a dog party company. Write a program to estimate expenses, based on the number of dogs and pups who'll be invited.

dogs and pups are GET input variables. You can try the program:

https://webappexamples.skilling.us/html/exercises/party-planner/party-planner.php?dogs=52&pups=38

Use 15% for the gratuity. Here are the costs per invitee:

Expense type Per dog Per pup
Food 28 14
Drinks 19 32
Games 18 9
Gifts 12 29
Clean up 17 58

Submit the URL of your solution, and a zip of your PHP file.