Last active
November 30, 2016 09:59
-
-
Save brendonexus/17f508e0b8258bfabc6dc4cbe5eba831 to your computer and use it in GitHub Desktop.
Boostrap 4 Form Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$field_name = 'field'; | |
$field_label = 'Field'; | |
?> | |
//INPUT FIELD | |
<div class="form-group <?php echo $_ERROR[$field_name] ? ' has-danger' : ''; ?>"> | |
<label for="<?php echo $field_name; ?>" | |
class="col-sm-0 control-label"><?php echo $field_label; ?></label> | |
<div class="col-sm-0"> | |
<input name="<?php echo $field_name; ?>" type="text" class="form-control" | |
id="<?php echo $field_name; ?>" | |
value="" placeholder=""> | |
<?php echo $_ERROR[$field_name] ? '<span class="help-block">' . $_ERROR[$field_name] . '</span>' : ''; ?> | |
</div> | |
</div> | |
//TEXTAREA FIELD | |
<div class="form-group <?php echo $_ERROR[$field_name] ? ' has-danger' : ''; ?>"> | |
<label for="<?php echo $field_name; ?>"><?php echo $field_label; ?></label> | |
<div class="col-sm-0"> | |
<textarea name="<?php echo $field_name; ?>" class="form-control" id="<?php echo $field_name; ?>" cols="100" rows="3"></textarea> | |
<?php echo $_ERROR[$field_name] ? '<span class="help-block">' . $_ERROR[$field_name] . '</span>' : ''; ?> | |
</div> | |
</div> | |
//SELECT FIELD | |
<div | |
class="form-group <?php echo $_ERROR[$field_name] ? ' has-danger' : ''; ?>"> | |
<label class="control-label" | |
for="method"><?php echo $field_label; ?></label> | |
<select class="form-control" name="<?php echo $field_name; ?>" id="<?php echo $field_name; ?>"> | |
<option value="stripe">Credit / Debit Card</option> | |
</select> | |
</div> | |
<?php | |
if ($_SERVER['REQUEST_METHOD'] == 'POST') { | |
$_ERROR = array(); | |
if (strlen($_POST['field']) == 0){ | |
$_ERROR['field'] = 'Message'; | |
} | |
if (!count($_ERROR)) { | |
$field = $_POST['field']; | |
} else { | |
$_SESSION['message']['error'] = 'Please correct the highlighted fields and submit again'; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment