Skip to content

Instantly share code, notes, and snippets.

@itskingori
Last active December 31, 2015 22:09
Show Gist options
  • Save itskingori/8051342 to your computer and use it in GitHub Desktop.
Save itskingori/8051342 to your computer and use it in GitHub Desktop.
User Input Through Forms
<!DOCTYPE html>
<html>
<head>
<title>Exercise 11: User Input</title>
</head>
<body>
<?php
// Create a page that accepts form the user ...
// a) First name
// b) Second name
// c) Last name
// d) Age
// Passes the data to PHP and outputs:
// "You are $first_name $second_name $last_name and you are $age years old."
?>
<!-- Create form that will reciece input from the user and pass it back to the page -->
<form action="" method="GET">
<!-- Form fields -->
First name: <input type="text" name="firstname"><br/>
Second name: <input type="text" name="secondname"><br/>
Last name: <input type="text" name="lastname"><br/>
Age: <input type="text" name="age"><br/>
<!-- Submit button -->
<input type="submit" value="Submit Button">
</form>
<?php
if (!empty($_GET['firstname']) && !empty($_GET['secondname']) && !empty($_GET['lastname']) && !empty($_GET['age'])) {
// Get the data from the $GET global variable
$first_name = $_GET['firstname'];
$second_name = $_GET['secondname'];
$last_name = $_GET['lastname'];
$age = $_GET['age'];
// Output a constructed sentence
echo "You are ".$first_name." ".$second_name." ".$last_name." and you are ".$age." years old.";
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment