Skip to content

Instantly share code, notes, and snippets.

@xDaevax
Last active September 20, 2015 03:09
Show Gist options
  • Save xDaevax/d818bc2d3958ee1a229d to your computer and use it in GitHub Desktop.
Save xDaevax/d818bc2d3958ee1a229d to your computer and use it in GitHub Desktop.
PHP Form Submission Example for Steve
<?php
class FormProcessor {
private $_pagePosted; // Boolean: stores a value that indicates whether or not the user submitted the form.
private $_hasError; // Boolean: If the page has been posted back, whether or not the form contained errors.
private $_messages; // Strings: Contains validation error messages (if any)
private $_debug; // Boolean: controls debug output to aid in development.
function __construct() { // Constructor that is called when: "$x = new FormProcessor" is called.
$this->_debug = true; // set this to false to get rid of unwanted output
$this->_pagePosted = isset($_POST["submit"]);
$this->_hasError = false;
$this->_messages = "";
if($this->_pagePosted) { // If this is the form being posted, perform the necessary actions
$this->ProcessData();
}
}
// This controls whether or not the validation messages are shown.
public function ShowMessages() { // returns a css- class name for which class to put on the output messages.
if($this->_hasError) {
return "messages";
} else {
return "no-messages";
}
}
private function ProcessData() { // Performs business logic and saves the data
if($this->IsValidForm()) {
// Only save the data if the form is valid
// TODO: Load the values from the form using the GetPostValue method, then write them to a database of some kind.
$proNumber = $this->GetPostValue("ProNumber");
if($this->_debug) {
echo "Pro Number Received: " . $proNumber . "<br />";
}
} else {
$this->_hasError = true;
}
}
private function IsValidForm() { // Validates the form and adds validation error messages.
$returnValue = true;
// TODO: Add any form field validation here
// Example to require the Pro Number field.
if($this->GetPostValue("ProNumber") == "") {
$this->_messages .= "Pro Number Required.<br />";
$returnValue = false;
}
return $returnValue;
}
// Convenience method to check the posted values in the form.
public function GetPostValue($key) {
if($this->_pagePosted) {
return $_POST[$key]; // return the posted value (if found).
} else {
return ""; // don't bother checking the POST since it hasn't happened yet.
}
}
// Returns the validation messages (if any)
public function GetMessages() {
return $this->_messages;
}
// Public function for exposing the value that indicates whether or not the form has been submitted
public function IsPostBack() {
return $this->_pagePosted;
}
}
$processor = new FormProcessor; // Instantiate the FormProcessor class so we can use it.
?>
<html>
<head>
<title>Sample Form</title>
<style type="text/css">
body {
font-size: small;
font-family: Arial, Verdana, Sans-Serif;
color: #000000;
margin: 0px;
padding: 0px;
}
.body-liner {
margin: 0px auto;
width: 90%;
text-align: center;
padding: 2px;
}
H1 {
display: block;
}
.form-wrapper {
margin: 4px auto;
display: block;
width: 300px;
clear: right;
height: 22px;
}
.no-messages {
display: none;
}
.form-wrapper LABEL {
float: left;
height: 20px;
display: block;
width: 130px;
margin-right: 20px;
text-align: right;
}
.form-wrapper INPUT[type='text'] {
float: left;
height: 20px;
display: block;
width: 150px;
text-align: left;
}
.messages {
display: block;
background-color: yellow;
color: #880000;
font-weight: bold;
}
</style>
</head>
<body>
<div class="body-liner">
<h1 class="page-header">
Load Form
</h1>
<div class="<?php echo $processor->ShowMessages()?>">
<?php
echo $processor->GetMessages();
?>
</div>
<div class="form-wrapper">
<form action="" method="post" name="load-form">
<span class="form-wrapper">
<label for="ProNumber">Pro #</label>
<input type="text" name="ProNumber" id="ProNumber" value="<?php echo $processor->GetPostValue("ProNumber")?>" /> </span>
<span class="form-wrapper">
<label for="OriginLocation">Origin City &amp; ZipCode</label>
<input type="text" name="OriginLocation" id="OriginLocation" vlaue="<?php echo $processor->GetPostValue("OriginLocation");?>" />
</span>
<input type="submit" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment