What happens:
When I write two values in both text boxes, the page doesn't show the Congratulations message as it should. When I write only 1 value, the correct thing happens, which is not show the congratulations message.
What should happen:
If a user writes only 1 value, the form should still appear with any previously filled out fields still there. If a user writes values in all of the fields, the Congratulations should appear.
Edit - Finally got it working, in case any other newbies want to check it out:
<html>
<head>
<?php
$validForm = false;
function getValue($field){
if(isset($_GET[$field])){
return htmlspecialchars(trim($_GET[$field]));
}
else{
return "";
}
}
function validateForm($value,$type){
$field = $_GET[$value];
//magic goes here.
switch ($type){
case 'required':
if (!isset($field) || ($field=="")){
global $validForm;
$validForm = false;
}
else{
global $validForm;
$validForm = true;
}
break;
case 'email':
$regexp = "/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z-][0-9a-zA-Z-]+\.)+[a-zA-Z](2,6)$/";
if(isset($field) && preg_match($regexp,$field)){
global $validForm;
$validForm = true;
}
else {
global $validForm;
$validForm = false;
}
break;
case 'number':
if(!isset($field) || ($field=="") || (!is_numeric($field))){
global $validForm;
$validForm = false;
}
else{
global $validForm;
$validForm = true;
}
break;
default:
die('Validacion desconocida.');
}
}
?>
</head>
<body>
<?php validateForm('name','required'); ?>
<?php validateForm('lastname','required'); ?>
<?php if($validForm == false){ ?>
<form action="class2.php" method="get">
<dl>
<dt>First Name:</dt>
<dd><input type="text" value="<?php echo htmlspecialchars(getValue('name')) ?>" name="name" />
</dd>
<dt>Last Name:</dt>
<dd><input type="text" value="<?php echo htmlspecialchars(getValue('lastname')) ?>" name="lastname" />
</dd>
<br />
<dt>
<input type="submit" value="enviar" name="validate"/>
</dt>
</dl>
</form>
<?php
} else {
?>
<h1>Congratulations, you succesfully filled out the form!</h1>
<?php }
?>
</body>