Skip to content

Instantly share code, notes, and snippets.

@christophengelmayer
Created March 12, 2013 19:55
Show Gist options
  • Save christophengelmayer/5146413 to your computer and use it in GitHub Desktop.
Save christophengelmayer/5146413 to your computer and use it in GitHub Desktop.
Simple PHP Form with validation
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>PHP Normform</title>
</head>
<body>
<?php
//----- field names -----
define("NAME","name");
define("EMAIL","email");
define("CHECK_SUBMIT","checkSubmit");
define("PASSWORD", "passwort");
define("PASSWORDCHECK","passwortcheck");
define("PLZ","plz");
define("INTERESTS","interests");
//----- auxiliary function used by printForm -----
//-----------------------------
function param($name) {
//-----------------------------
// return the content of post-parameter $name if it exists, else ""
return isset($_REQUEST[$name]) ? $_REQUEST[$name]:"";
}
//-------------------------
function printErrMsg() {
//-------------------------
global $errMsg;
if (isset($errMsg)) {
echo "<br />Bitte folgende Fehler korrigieren:\n";
echo "<ul>\n";
foreach ($errMsg as $e) {
echo "<li>$e</li>";
}
echo "</ul><br />\n";
}
}
//------------------------
function printForm() {
//------------------------
// print form, including pre-defined values and error messages
?>
<?php
printErrMsg();
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="get">
<label>Vorname:
<input type="text"
name="<?php echo NAME ?>"
value="<?php echo param(NAME) ?>"/>
</label><br />
<label>E-Mail:
<input type="text"
name="<?php echo EMAIL ?>"
value="<?php echo param(EMAIL) ?>"/>
</label><br />
<label>Passwort:
<input type="password"
name="<?php echo PASSWORD ?>"
value="<?php echo param(PASSWORD) ?>"/>
</label><br />
<label>Passwort-Check:
<input type="password"
name="<?php echo PASSWORDCHECK ?>"
value="<?php echo param(PASSWORDCHECK) ?>"/>
</label><br />
<label>PLZ:
<input type="text" size="4"
name="<?php echo PLZ ?>"
value="<?php echo param(PLZ) ?>"/>
</label><br />
<label>Interessen:<br />
<input type="checkbox"
name="<?php echo INTERESTS . '[]' ?>"
<?php if (in_array("Fernsehen", $_GET[INTERESTS])) echo "checked=\"checked\"" ?>
value="Fernsehen"/>Fernsehen<br />
<input type="checkbox"
name="<?php echo INTERESTS . '[]' ?>"
<?php if (in_array("Sport", $_GET[INTERESTS])) echo "checked=\"checked\"" ?>
value="Sport"/>Sport<br />
<input type="checkbox"
name="<?php echo INTERESTS . '[]' ?>"
<?php if (in_array("Essen", $_GET[INTERESTS])) echo "checked=\"checked\"" ?>
value="Essen"/>Essen<br />
<input type="checkbox"
name="<?php echo INTERESTS . '[]' ?>"
<?php if (in_array("Studieren", $_GET[INTERESTS])) echo "checked=\"checked\"" ?>
value="Studieren"/>Studieren<br />
<input type="checkbox"
name="<?php echo INTERESTS . '[]' ?>"
<?php if (in_array("Fortgehen", $_GET[INTERESTS])) echo "checked=\"checked\"" ?>
value="Fortgehen"/>Fortgehen<br />
</label><br />
<input type="hidden"
name="<?php echo CHECK_SUBMIT ?>"
value="checkSubmit"/>
<input type="submit" name="submit"/>
<input type="reset" name="reset"/>
</form>
<?php
}
//----- auxiliary functions used by validateForm -----
//----------------------------
function isEmpty($name) {
//----------------------------
// returns true, if post-arameter $name consists of nothing
// but maybe whitespaces
return (strlen(trim($_REQUEST[$name])) == 0);
}
//---------------------------
function isValidForm() {
//---------------------------
global $errMsg;
if (isEmpty(NAME)) {
$errMsg[NAME]= "Name fehlt.";
}
if (isEmpty(EMAIL) != true && eregi('@',param(EMAIL)) != true) {
$errMsg[EMAIL]= "Ung&uuml;ltige E-Mail.";
}
if (isEmpty(PASSWORD) || strlen(param(PASSWORD)) < 5) {
$errMsg[PASSWORD]= "Ung&uuml;ltiges Passwort.";
}
else if (strcmp(param(PASSWORD),param(PASSWORDCHECK)) != 0) {
$errMsg[PASSWORD]= "Passw&ouml;rter stimmen nicht &uuml;berein.";
}
if (ereg("[0-9]{4}",param(PLZ)) == 0 || strlen(param(PLZ)) != 4) {
$errMsg[PLZ]= "Ung&uuml;ltige PLZ.";
}
if (count(param(INTERESTS))<2) {
$errMsg[INTERESTS]= "Mindestens Zwei Interessen angeben.";
}
return !isset($errMsg);
}
//--------------------------
function processForm() {
//--------------------------
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr><td>Name</td><td><?php echo $_GET[NAME];?></td></tr>
<?php
if ($_GET[EMAIL] != 0){
echo "<tr><td>E-Mail</td><td>".$_GET[EMAIL]."</td></tr>";
}?>
<tr><td>Passwort</td><td><?php echo $_GET[PASSWORD];?></td></tr>
<tr><td>PLZ</td><td><?php echo $_GET[PLZ];?></td></tr>
<tr><td>Interessen</td><td>
<?php foreach ($_GET[INTERESTS] as $v) {
echo "$v<br />\n";
}?>
</td></tr>
</table>
<?php
}
//-----------------------------
function isFormSubmission() {
//-----------------------------
return isset($_REQUEST[CHECK_SUBMIT]);
}
//-----------------------------
function normForm() {
//-----------------------------
if (isFormSubmission()) {
if (isValidForm()) {
processForm();
} else {
printForm();
}
} else {
printForm();
}
}
normform();
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment