Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
375 views

!doctype HTML Head /head Body: // Define Variables and Set To Empty Values

The document contains code for a PHP form with validation. It defines variables to store form input and a function to sanitize input. The form collects name, email, website, comment, and gender. It is then validated on submission to check for empty required fields before displaying the input. A second version adds error messages for required fields.

Uploaded by

kuttydon
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
375 views

!doctype HTML Head /head Body: // Define Variables and Set To Empty Values

The document contains code for a PHP form with validation. It defines variables to store form input and a function to sanitize input. The form collects name, email, website, comment, and gender. It is then validated on submission to check for empty required fields before displaying the input. A second version adds error messages for required fields.

Uploaded by

kuttydon
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

<!

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;
}
?>

<h2>PHP Form Validation Example</h2>


<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Name: <input type="text" name="name">
  <br><br>
  E-mail: <input type="text" name="email">
  <br><br>
  Website: <input type="text" name="website">
  <br><br>
  Comment: <textarea name="comment" rows="5" cols="40"></textarea>
  <br><br>
  Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
  <input type="radio" name="gender" value="other">Other
  <br><br>
  <input type="submit" name="submit" value="Submit">  
</form>

<?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;
}
?>

<h2>PHP Form Validation Example</h2>


<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Name: <input type="text" name="name">
  <span class="error">* <?php echo $nameErr;?></span>
  <br><br>
  E-mail: <input type="text" name="email">
  <span class="error">* <?php echo $emailErr;?></span>
  <br><br>
  Website: <input type="text" name="website">
  <span class="error"><?php echo $websiteErr;?></span>
  <br><br>
  Comment: <textarea name="comment" rows="5" cols="40"></textarea>
  <br><br>
  Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
  <input type="radio" name="gender" value="other">Other
  <span class="error">* <?php echo $genderErr;?></span>
  <br><br>
  <input type="submit" name="submit" value="Submit">  
</form>

<?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);

echo("<p>You selected $nCountries countries: ");


for($i=0; $i < $nCountries; $i++)
{
echo($aCountries[$i] . " ");
}
echo("</p>");
}
}

?>

<?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

// method 2: dynamic redirect


//header("Location: " . $varCountry . ".html");
// end method 2

exit();
}
}
?>

You might also like