Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple server-side validation routine for most PHP forms
<?php
/*
Handling PHP Form Postbacks: Same-Page Validation
Copyright (C) 2016 Doug Vanderweide
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if(isset($_POST['submit'])) {
//boolean for whether form is valid
//assume valid, set to invalid as bad inputs fail validation
$ok = true;
//prepare error notification message
$msg = "<p><strong>Sorry, there are some problems with this post.</strong> Please correct the following:</p>";
$msg .= "<ul>";
//check for empty text field
if(empty($_POST['firstName'])) {
$ok = false;
$msg .= "<li>Please provide a first name.</li>";
}
//select a radio button
if(empty($_POST['mudkipz']) || !in_array($_POST['mudkipz'], array('yes', 'no'))) {
$ok = false;
$msg .= "<li>Please confirm or deny your love of mudkipz.</li>";
}
if(!is_array($_POST['rock']) || count($_POST['rock']) < 1 ) {
$ok = false;
$msg .= "<li>You must at least want to rock and roll all night, or party every day.</li>";
}
else {
foreach($_POST['rock'] as $kiss) {
if(!in_array($kiss, array('I wanna rock and roll all night', 'and party every day'))) {
$ok = false;
$msg .= "<li>You can either want to rock and roll all night, or party every day, or both, but not anything else.</li>";
}
}
}
$msg .= "</ul>";
if($ok) {
unset($msg);
//code to process form goes here
}
}
?>
<!doctype html>
<html>
<head>
<title>Simple PHP form validation</title>
<link rel="stylesheet" type="text/css" href="../demo.css" />
<style>
label {
display: block;
text-align: left;
font-weight: normal;
}
input[type="submit"] {
padding: 5px;
}
input[type="submit"], label {
margin: 5px 0;
}
button {
margin: 5px;
}
</style>
</head>
<body>
<h1>My form</h1>
<?php echo $msg; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<label for="firstName">First name: <input type="text" name="firstName" value="<?php echo $_POST['firstName']; ?>" /></label>
<label><input type="radio" id="mudkipz1" class="mudkipz" name="mudkipz" value="yes" <?php if (!isset($_POST['mudkipz']) || $_POST['mudkipz'] == "yes") { echo "checked"; } else { echo ""; } ?> /> I liek mudkipz.</label>
<label><input type="radio" class="mudkipz" name="mudkipz" value="no" <?php if(isset($_POST['mudkipz']) && $_POST['mudkipz'] == "no") { echo "checked"; } else { echo ""; } ?> /> I do not liek mudkipz.</label>
<label><input type="checkbox" id="rock1" name="rock[]" value="I wanna rock and roll all night"
<?php
if(isset($_POST['rock'])) {
if(is_array($_POST['rock'])) {
foreach($_POST['rock'] as $choice) {
if($choice == "I wanna rock and roll all night") {
echo "checked";
break;
}
}
}
}
?>
/> I wanna rock and roll all night.</label>
<label><input id="rock2" type="checkbox" name="rock[]" value="and party every day"
<?php
if(isset($_POST['rock'])) {
if(is_array($_POST['rock'])) {
foreach( $_POST['rock'] as $choice ) {
if($choice == "and party every day") {
echo "checked";
break;
}
}
}
}
?>
/> And party every day.</label>
<input type="submit" name="submit" value="Submit" />
</form>
<p><a href="https://www.dougv.com/2016/04/handling-php-form-postbacks-page-validation">Handling PHP Form Postbacks: Same-Page Validation</a>
<div>
<button id="polluteMudkipz">Pollute radio buttons</button> <button id="restoreMudkipz">Restore mudkipz radio buttons</button>
<br />
<button id="polluteRock">Pollute checkboxes</button> <button id="restoreRock">Restore checkboxes</button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#polluteMudkipz').click(function() {
$('.mudkipz').prop('checked', false);
});
$('#restoreMudkipz').click(function() {
$('#mudkipz1').prop('checked', true);
});
$('#polluteRock').click(function() {
$('#rock1').val('you keep on shouting');
$('#rock2').val('you keep on shouting');
});
$('#restoreRock').click(function() {
$('#rock1').val('I wanna rock and roll all night');
$('#rock2').val('and party every day');
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.