JavaScript Tutorials
- What is Javascript?
- What is BOM?
- What is DOM?
- Variable
- check variable value is empty
- JavaScript Output
- Functions
- Javascript Events
- Input Events
- onchange Event
- Javascript Output methods
- If else statement
- Arrays
- Pattern Validation
- Form Validation
- Inner Html Form Validation
- Inline Form Validation
- Checkbox Validation
- Inline Inner Html Form Validation
- Server side php Validation
- Validate a HTML Form with PHP
- window and window location
- Get Text Value
- Get hidden field value
- JavaScript & PHP
- Date Format
- get php value in javascript
- Redirect page & Autoredirect page
- Auto Refresh page & Div
- How to get select text value from Dropdown box
- How to clear browser history in javascript
- Checkbox Problems
- Select option problems
- Popup Contact Form
- Sidebar Contact Form
- How to use a multistep Form or Form wizard
- Auto Calculate Price
- print Application Form
- Auto Calculate GST in Javascript by select price
- Calculate GST by input value in text box Jquery
- Calculate Discount
- onClick Checkbox
- autofil form data click on checkbox
- Show subcategory list
- Show city list as per state
- Show district list as per country and state
- Show good morning good night wish
- image upload with preview image
- Print Div Content
- Show modal popup on page load
- filter table data usign javascript.
- Character Limit Validation.
- Validate File 5MB Upload
- Validate Special character
- More File Upload
- Call JavaScript Function After Page Load
- Drop Down First option Value Disabled --- Please Select ---
- How to Disable Submit Button After Form Submission
- How to disable browser back button using Jquery?
- How to Remove selected item from second & third dropdown list?
- Interview Questions of JavaScript.
Important Link
Home » Javascript »
About Javascript
Validate a HTML Form with PHP
File Name:- index.php
<?phpinclude('validate.php');
function selected($blood_group, $choice) {
if($blood_group==$choice) echo "selected";
}
?>
<html>
<head>
<title>Validating Form with PHP - by Arpan Das (http://w3epic.net)</title>
<style>
body {
font-family: 'trebuchet ms';
font-size: 1.4em;
padding: 0 50px; color: #444;
}
input, textarea {font-size: 1em;}
p.error {background: #ffd; color: red;}
p.error:before {content: "Error: ";}
p.success {background: #ffd; color: green;}
p.success:before {content: "Success: ";}
p.error, p.success {font-weight: bold;}
</style>
</head>
<body>
<h1>Validating Form with PHP - by Arpan Das (http://w3epic.net)</h1>
<h2>Please fill up the form below and submit.</h2>
<?=$error?>
<form action="html_form_to_validate.php" method="post">
<table>
<tr>
<td>Username: </td>
<td><input type="text" name="username" value="<?=@$username?>"/> (3 to 20 alpha-numeric characters)</td>
</tr>
<tr>
<td>First name: </td>
<td><input type="text" name="first_name" value="<?=@$first_name?>"/> (3 to 20 alpha characters only)</td>
</tr>
<tr>
<td>Last name: </td>
<td><input type="text" name="last_name" value="<?=@$last_name?>"/> (3 to 20 alpha characters only)</td>
</tr>
<tr>
<td>Password: </td>
<td><input type="password" name="password" value="<?=@$password?>"/> (3 to 20 characters only)</td>
</tr>
<tr>
<td>Confirm password: </td>
<td><input type="password" name="confirm_password" value="<?=@$confirm_password?>"/> (3 to 20 characters only)</td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name="email" value="<?=@$email?>"/> (Valid email like name@domain.com)</td>
</tr>
<tr>
<td>Phone: </td>
<td><input type="text" name="phone" value="<?=@$phone?>"/> (10 digit mobile number)</td>
</tr>
<tr>
<td>Gender: </td>
<td><input type="radio" name="gender" value="male" <?php if(@$gender=='male')echo 'checked="true"';?>
<?php if(!isset($gender))echo 'checked="true"';?>/> male
<input type="radio" name="gender" value="female"
<?php if(@$gender=='female')echo 'checked="true"';?> /> female</td>
</tr>
<tr>
<td>Blood Group: </td>
<td>
<select name='blood_group'>
<option value="0" >Select Blood Group</option>
<option value="1" <?php selected(@$blood_group, 1) ?>>A Positive</option>
<option value="2" <?php selected(@$blood_group, 2) ?>>A Negative</option>
<option value="3" <?php selected(@$blood_group, 3) ?>>B Positive</option>
<option value="4" <?php selected(@$blood_group, 4) ?>>B Negative</option>
<option value="5" <?php selected(@$blood_group, 5) ?>>AB Positive</option>
<option value="6" <?php selected(@$blood_group, 6) ?>>AB Negative</option>
<option value="7" <?php selected(@$blood_group, 7) ?>>O Positive</option>
<option value="8" <?php selected(@$blood_group, 8) ?>>O Negative</option>
</select>
</td>
</tr>
<tr>
<td>Date of Birth: </td>
<td><input type="number" name="day" value="<?=@$day?>" size=2/>/
<input type="number" name="month" value="<?=@$month?>" size=2/>/
<input type="number" name="year" value="<?=@$year?>" size=4/> (DD/MM/YYYY)</td>
</tr>
<tr>
<td>Bio: </td>
<td><textarea name="bio"><?=@$bio?></textarea></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit"/> <input type="reset" name="reset" value="Reset"/>
</form>
<?php
if (isset($_POST['submit']) && $error == '') { // if there is no error, then process further
echo "<p class='success'>Form has been submitted successfully.</p>"; // showing success message
// hashing the password and sanitize data
$_POST['password'] = md5($_POST['password']);
foreach ($_POST as $key => $val) {
$_POST[$key] = mysql_real_escape_string($_POST[$key]);
// Or you can use $mysqli->real_escape_string() as above function is deprecated
// Or you can use prepared statements to sanitize
// Use stripslashes to do the opposite
}
// do stuffs with validated & safe data
//show the raw data (for practice)
var_dump($_POST);
}
?>
</body>
</html>
validate.php
File Name:- validate.php
<?php/*=============================================================*/
#### How to Validate Form with PHP - Server Side Validation ####
#### Author : Arpan Das ####
#### site : http://w3epic.com/ ####
#### email : admin@w3epic.com ####
/*=============================================================*/
$error = ""; // Initialize error as blank
if (isset($_POST['submit'])) { // check if the form is submitted
#### removing extra white spaces & escaping harmful characters ####
$username = trim($_POST['username']);
$first_name = trim($_POST['first_name']);
$last_name = trim($_POST['last_name']);
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$gender = $_POST['gender'];
$blood_group = $_POST['blood_group'];
// dob
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$dob = $day.$month.$year;
$age = date("Y")-$year;
$bio = $_POST['bio'];
#### start validating input data ####
#####################################
# Validate Username #
// if its not alpha numeric, throw error
if (!ctype_alnum($username)) {
$error .= '<p class="error">Username should be alpha numeric characters only.</p>';
}
// if username is not 3-20 characters long, throw error
if (strlen($username) < 3 OR strlen($username) > 20) {
$error .= '<p class="error">Username should be within 3-20 characters long.</p>';
}
# Validate First Name #
// if its not alpha numeric, throw error
if (!ctype_alpha(str_replace(array("'", "-"), "",$first_name))) {
$error .= '<p class="error">First name should be alpha characters only.</p>';
}
// if first_name is not 3-20 characters long, throw error
if (strlen($first_name) < 3 OR strlen($first_name) > 20) {
$error .= '<p class="error">First name should be within 3-20 characters long.</p>';
}
# Validate Last Name #
// if its not alpha numeric, throw error
if (!ctype_alpha(str_replace(array("'", "-"), "", $last_name))) {
$error .= '<p class="error">Last name should be alpha characters only.</p>';
}
// if first_name is not 3-20 characters long, throw error
if (strlen($last_name) < 3 OR strlen($last_name) > 20) {
$error .= '<p class="error">Last name should be within 3-20 characters long.</p>';
}
# Validate Password #
// if first_name is not 3-20 characters long, throw error
if (strlen($password) < 3 OR strlen($password) > 20) {
$error .= '<p class="error">Password should be within 3-20 characters long.</p>';
}
# Validate Confirm Password #
// if first_name is not 3-20 characters long, throw error
if ($confirm_password != $password) {
$error .= '<p class="error">Confirm password mismatch.</p>';
}
# Validate Email #
// if email is invalid, throw error
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { // you can also use regex to do same
$error .= '<p class="error">Enter a valid email address.</p>';
}
# Validate Phone #
// if phone is invalid, throw error
if (!ctype_digit($phone) OR strlen($phone) != 10) {
$error .= '<p class="error">Enter a valid phone number.</p>';
}
# Validate Gender #
// if gender is not selected or invalid, throw error
if ($gender != 'male' && $gender != 'female') {
$error .= '<p class="error">Please select your gender.</p>';
}
# Validate Blood Group #
// if blood group is not selected, throw error
if ($blood_group == 0) {
$error .= '<p class="error">Please select your blood group.</p>';
}
# Validate Date of Birth (DOB) #
// if day is not 1-31, throw error
if (intval($day)<1 OR intval($day)>31) {
$error .= '<p class="error">Enter a valid day between 1-31.</p>';
}
// if month is not 1-12, throw error
if (intval($month)<1 OR intval($month)>12) {
$error .= '<p class="error">Enter a valid month between 1-12.</p>';
}
// if age is below 18 , throw error
if ($age < 18) {
$error .= '<p class="error">You should be at least 18 years old.</p>';
}
# Validate Bio #
if (strlen($bio)==0 OR strlen($bio)>240) {
$error .= '<p class="error">Please write something about you withing 240 characters.</p>';
}
#### end validating input data ####
#####################################
}