I have this code working for submitting a form using PHP and HTML (Bootstrap). I did not post the CSS, so I hope this is not an issue.
I am interested if this code is clean, following good practices, secure etc.
- Are the headers needed?
- Do I need to declare and initialise all those variables?
- Do I need
<?php if (!empty ($_POST['lastname'])) echo $_POST['lastname']; ?>
or is<?php echo $_POST['lastname']; ?>
enough? - Is it properly indented ?
- Could I separate PHP and HTML even more?
- Would you put the PHP part below the HTML? etc etc
<?php
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() - 8400) . ' GMT');
error_reporting(0);
// declare variables and initialise
$error = "";
$form_errors_array = array(); // array to hold errors to be displayed to the user
$account = "";
$error_css_class = array(); // array to hold which fields will have an "error" class added in the html
$error_css_class_arraykeys = array('firstname', 'lastname', 'email', 'ordervalue', 'account', 'deposit', 'term');
$_POST = array_map('trim', $_POST);
// test if the form was submitted and perform basic validation on all fields
if ( !empty($_POST['postform']) ) {
$error_css_class = array_fill_keys($error_css_class_arraykeys, '');
if ( empty($_POST['firstname']) ) {
$form_errors_array[] = "Please enter first name.";
$error_css_class['firstname'] = "error"; // firstname input field will have a red border because of the "error" css class
}
if ( empty($_POST['lastname']) ) {
$form_errors_array[] = "Please enter last name.";
$error_css_class['lastname'] = "error";
}
if ( !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
$form_errors_array[] = "Invalid email";
$error_css_class['email'] = "error";
}
if ( empty($_POST['ordervalue'] ) || !is_numeric($_POST['ordervalue']) ) {
$form_errors_array[] = "Order value is not valid";
$error_css_class['ordervalue'] = "error";
}
if ( empty($_POST['deposit'] ) || !is_numeric($_POST['deposit']) ) {
$form_errors_array[] = "Deposit value is not valid";
$error_css_class['deposit'] = "error";
}
if ( empty($_POST['term'] ) || ($_POST['term']) == 0 ) {
$form_errors_array[] = "Term value is not valid";
$error_css_class['term'] = "error";
}
if ( empty($_POST['account']) ) {
$form_errors_array[] = "Account value (Account1 or Account2) not selected";
$error_css_class['account'] = "error";
}
//rest of the code only happens if there are no errors in the form
if ( count($form_errors_array) == 0 ) {
// do some useful stuff
;
}
}
function trim_value($value)
{
$value = trim($value);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>title</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="common/js/jquery-1.11.1.min.js"><\/script>')</script>
<link href="common/bootstrap/bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="common/bootstrap/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet">
<link href="common/css/default.css" rel="stylesheet">
<script src="common/bootstrap/bootstrap/js/bootstrap.js"></script>
<!--[if lt IE 9]>
<link href="common/css/ie8.css" rel="stylesheet">
<![endif]-->
</head>
<body>
<div id="wrapper" class="container-narrow">
<div class="header clearfix">
</div> <!-- /header -->
<div id="content">
<div class="formcontainer noborder">
<form action="" method="post" >
<fieldset>
<legend>Customer details (don't use initials - cannot be amended!)</legend>
<input type='hidden' name='postform' value='1'>
<div class="input-group">
<span class="input-group-addon input-small">First name: </span>
<input type="text" name="firstname"
class="form-control input-small <?php echo $error_css_class['firstname']; ?>" value="<?php if (!empty ($_POST['firstname'])) echo $_POST['firstname']; ?>" placeholder="First name" />
</div>
<div class="input-group">
<span class="input-group-addon input-small">Last name: </span>
<input type="text" name="lastname"
class="form-control input-small <?php echo $error_css_class['lastname']; ?>" value="<?php if (!empty ($_POST['lastname'])) echo $_POST['lastname']; ?>" placeholder="Last name" />
</div>
</fieldset>
<fieldset>
<legend>Customer email</legend>
<div class="input-group">
<span class="input-group-addon input-small">Email: </span>
<input type="text" name="email" class="form-control input-small <?php echo $error_css_class['email']; ?>" value="<?php if (!empty ($_POST['email'])) echo $_POST['email']; ?>" placeholder="Email" />
</div>
</fieldset>
<fieldset>
<legend>Order details (cannot be amended!)</legend>
<div class="input-group">
<span class="input-group-addon input-small">Order value: </span>
<input type="text" name="ordervalue" class="form-control input-small <?php echo $error_css_class['ordervalue']; ?>" value="<?php if (!empty ($_POST['ordervalue'])) echo $_POST['ordervalue']; ?>" placeholder="Order value"/>
</div>
<div class="input-group">
<span class="input-group-addon input-small">Deposit: </span>
<input type="text" name="deposit" class="form-control input-small <?php echo $error_css_class['deposit']; ?>" value="<?php if (!empty ($_POST['deposit'])) echo $_POST['deposit']; ?>" placeholder="Deposit"/>
</div>
</fieldset>
<fieldset>
<legend class="radio-group-legend">Select account</legend>
<div class="radio marginbottom">
<label for="account1"><input class="radio-button-selector" type="radio" name="account" id="account1" value="account1">Account1</label>
</div>
<div class="radio">
<label for="account2"><input class="radio-button-selector" type="radio" name="account" id="account2" value="account2">Account2</label>
</div>
</fieldset>
<fieldset>
<legend>Select term (cannot be amended!)</legend>
<!-- Select Basic -->
<div id="scs_select" class="form-group">
<div class="col-md-4 no-left-padding">
<select id="term" name="term" class="form-control <?php echo $error_css_class['term']; ?>">
<option value="0">Please select</option>
<option value="6">6 months</option>
<option value="9">9 months</option>
<option value="10">10 months</option>
<option value="12">12 months</option>
<option value="18">18 months</option>
<option value="24">24 months</option>
<option value="30">30 months</option>
<option value="36">36 months</option>
<option value="42">42 months</option>
<option value="48">48 months</option>
</select>
</div>
</div>
</fieldset>
<div id="submiter">
<button type="submit" class="btn btn-primary" >Submit</button>
</div>
</form>
</div>
<div class="formcontainer">
<?php
if ( count($form_errors_array) > 0 ) {
echo '<div class="alert alert-danger">';
foreach ($form_errors_array as $formerror) {
echo '<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>', " ", "$formerror<br>";
}
echo '</div>';
}
?>
</div>
</div> <!-- /content -->
</div> <!-- /wrapper -->
</body>
</html>