I'm quite new to php (learned and only used C# for web pages so far) and wanted to know if this is the correct approach for validating a form. This is my userform.php code:
<!--Includes HTML, Head, Scripts, CSS, Body and begin of Content div-->
<?php include realpath(__DIR__)."/layout/header.php"; ?>
<?php
function Validate() {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
return IsValid("username") && IsValid("password") && IsValid("usermail");
} else {
return true;
}
}
function IsValid($name) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
switch($name){
case 'username':
return !isset($_POST["username"]) || strlen($_POST["username"]) > 0;
break;
case 'password':
return !isset($_POST["password"]) || strlen($_POST["password"]) > 0;
break;
case 'usermail':
return strlen($_POST["usermail"]) > 0 && filter_var($_POST["usermail"], FILTER_VALIDATE_EMAIL);
break;
}
} else {
return true;
}
}
function GetErrorMessage($name) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
switch($name){
case 'username':
return !isset($_POST["username"]) || strlen($_POST["username"]) == 0 ? 'Username is required' : '';
break;
case 'password':
return !isset($_POST["username"]) || strlen($_POST["password"]) == 0 ? 'Password is required' : '';
break;
case 'usermail':
$message = '';
if (strlen($_POST["usermail"]) == 0) {
$message = $message.'E-mail is required';
}
if(strlen($_POST["usermail"]) != 0 && !filter_var($_POST["usermail"], FILTER_VALIDATE_EMAIL)) {
if(strlen($message) > 0) {
$message = $message.'<br />';
}
$message = $message.'"'.$_POST["usermail"].'" is not a valid e-mail address';
}
return $message;
break;
}
} else {
return '';
}
}
?>
<div class="row">
<div class="col-md-4">
<form action="./userform.php" method="POST" role="form">
<div class="form-group <?php echo IsValid('username') ? '' : 'has-error' ?>">
<label for="username">Username</label>
<input type="text" id="username" name="username" class="form-control" value="<?php echo Validate() ? '' : $_POST["username"] ?>" />
<span class="help-block"><?php echo GetErrorMessage('username') ?></span>
</div>
<div class="form-group <?php echo IsValid('password') ? '' : 'has-error' ?>">
<label for="password">Password</label>
<input type="password" id="password" name="password" class="form-control" value="<?php echo Validate() ? '' : $_POST["password"] ?>" />
<span class="help-block"><?php echo GetErrorMessage('password') ?></span>
</div>
<div class="form-group <?php echo IsValid('usermail') ? '' : 'has-error' ?>">
<label for="usermail">E-Mail</label>
<input type="text" id="usermail" name="usermail" class="form-control" value="<?php echo Validate() ? '' : $_POST["usermail"] ?>" />
<span class="help-block"><?php echo GetErrorMessage('usermail') ?></span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && Validate()) {
echo '<div class="col-md-4"><h4>Data from POST</h4><p>Username: '.$_POST["username"].'</p><p>Password: '.$_POST["password"].'</p><p>E-Mail: '.$_POST["usermail"].'</p></div>';
} else if($_SERVER['REQUEST_METHOD'] === 'POST' && !Validate()) {
echo '<div class="col-md-4"><h4>Data from POST is not valid</h4></div>';
}
?>
</div>
<!--Includes End of Content div, Body and HTML-->
<?php include realpath(__DIR__)."/layout/footer.php"; ?>
The userform.php should work for GET
and POST
requests, thus the $_SERVER["REQUEST_METHOD"]
checks everywhere.
Edit: The GET
requests is just for the empty form. Posting the form should be handled in the same file.
Question 1: Is there a better way to get the validation states and the error messages for my form fields? Writing them into two seperate methods seems to be kind of overkill for me.
Usually I would encapsulate the code for validation in a single method that returns some kind of dictionary which allows me to check the count and if a form field is included (Preferably with LINQ
).
Question 2: Is there a better way to echo
html back to the user? Writing all the html code into a single echo seems to be not very clean. Assuming I have to output a bigger portion of html with the same if else
clause as above, it would be very confusing to write and edit the html code.
Question 3: Although it works is realpath(__DIR__)
the correct usage to include files from a different folder?