Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple server-side validation routine for most PHP forms
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
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 <>.
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) {
//code to process form goes here
<!doctype html>
<title>Simple PHP form validation</title>
<link rel="stylesheet" type="text/css" href="../demo.css" />
label {
display: block;
text-align: left;
font-weight: normal;
input[type="submit"] {
padding: 5px;
input[type="submit"], label {
margin: 5px 0;
button {
margin: 5px;
<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"
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";
/> I wanna rock and roll all night.</label>
<label><input id="rock2" type="checkbox" name="rock[]" value="and party every day"
if(isset($_POST['rock'])) {
if(is_array($_POST['rock'])) {
foreach( $_POST['rock'] as $choice ) {
if($choice == "and party every day") {
echo "checked";
/> And party every day.</label>
<input type="submit" name="submit" value="Submit" />
<p><a href="">Handling PHP Form Postbacks: Same-Page Validation</a>
<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>
<script src=""></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');
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.