!doctype HTML Head /head Body: // Define Variables and Set To Empty Values
!doctype HTML Head /head Body: // Define Variables and Set To Empty Values
DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
Form required :
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
<label for='formCountries[]'>Select the countries that you have
visited:</label><br>
<select multiple="multiple" name="formCountries[]">
<option value="US">United States</option>
<option value="UK">United Kingdom</option>
<option value="France">France</option>
<option value="Mexico">Mexico</option>
<option value="Russia">Russia</option>
<option value="Japan">Japan</option>
</select>
<?php
if(isset($_POST['formSubmit']))
{
$aCountries = $_POST['formCountries'];
if(!isset($aCountries))
{
echo("<p>You didn't select any countries!</p>\n");
}
else
{
$nCountries = count($aCountries);
?>
<?php
if(isset($_POST['formSubmit']))
{
$varCountry = $_POST['formCountry'];
$errorMessage = "";
if(empty($varCountry))
{
$errorMessage = "<li>You forgot to select a country!</li>";
}
if($errorMessage != "")
{
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
else
{
// note that both methods can't be demonstrated at the same time
// comment out the method you don't want to demonstrate
// method 1: switch
$redir = "US.html";
switch($varCountry)
{
case "US": $redir = "US.html"; break;
case "UK": $redir = "UK.html"; break;
case "France": $redir = "France.html"; break;
case "Mexico": $redir = "Mexico.html"; break;
case "Russia": $redir = "Russia.html"; break;
case "Japan": $redir = "Japan.html"; break;
default: echo("Error!"); exit(); break;
}
echo " redirecting to: $redir ";
// header("Location: $redir");
// end method 1
exit();
}
}
?>