/    Sign up×
Community /Pin to ProfileBookmark

Html form validated by php

I have an html form that is validated by php, how do i set the php to check the html form fields and make sure they are not blank, meaning that people can hit the space bar and my php thinks that there is something in the field?

[code=php]<?
session_start();

$errors = array();
if( empty( $_POST[‘name’] ) )
{ // fail error checking
$errors[] = ‘Name is Required’;
}

if( empty( $_POST[’email’] ) )
{
$errors[] = ‘Email is Required’;
}

if( empty( $_POST[’email2′] ) )
{
$errors[] = ‘Confirm Email is Required’;
}

if( !empty( $_POST[’email’] ) && !empty( $_POST[’email2′] ) && $_POST[’email’] != $_POST[’email2′] )
{
$errors[] = ‘Your email addresses do not match’;
}

if( empty( $_POST[‘Company’] ) )
{
$errors[] = ‘Company is Required’;
}

if( empty( $_POST[‘address’] ) )
{
$errors[] = ‘Address is Required’;
}

if( empty( $_POST[‘city’] ) )
{
$errors[] = ‘City is Required’;
}

if( empty( $_POST[‘region’] ) )
{
$errors[] = ‘State is Required’;
}

if( empty( $_POST[‘zip’] ) )
{
$errors[] = ‘Zip is Required’;
}

if( empty( $_POST[‘country’] ) )
{
$errors[] = ‘Country is Required’;
}
// repeat for all validation

if( empty( $_POST[‘Security_Code’] ) )
{
$errors[] = ‘Plese fill in the security code’;
}
elseif( $_POST[‘Security_Code’] != $_SESSION[‘pass’] )
{
$errors[] = ‘The security code you entered is invalid’;
}

if( !empty( $errors ) )
{
?>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title>MARK-10 Force and Torque Measurement</title>

<meta name=”description” content=”Manufacturer of force and torque measuring instruments”>
<meta name=”keywords” content=”force gauges, force gages, torque gauges, torque gages, force indicators, torque indicators, needle sharpness testers, blade sharpness testers, puncture testers, spring testers, tensile testers, pull testers, compression testers, friction testers, friction meters, slip testers, slip meters, slipperiness testers, slipperiness meters, peel testers, bond strength testers, materials testers, force measurement, torque measurement, torque tool calibrators, peel testing, crimp testing, pull testing, compression testing, tension testing, torque analyzers, test stands, manual stands, manual test stands, motorized stands, morized test stands, universal test stands, FORCE GAUGES, FORCE GAGES, TORQUE GAUGES, TORQUE GAGES, FORCE INDICATORS, TORQUE INDICATORS, NEEDLE SHARPNESS TESTERS, BLADE SHARPNESS TESTERS, PUNCTURE TESTERS, SPRING TESTERS, TENSILE TESTERS, PULL TESTERS, COMPRESSION TESTERS, CHATILLON, AMETEK, DILLON, WAGNER, IMADA, SHIMPO, FRICTION TESTERS, FRICTION METERS, SLIP TESTERS, SLIP METERS, PEEL TESTERS, BOND STRENGTH TESTERS, MATERIALS TESTERS, FORCE MEASUREMENT, TORQUE MEASUREMENT, TORQUE TOOL CALIBRATORS, PEEL TESTING, CRIMP TESTING, PULL TESTING, COMPRESSION TESTING, TENSION TESTING, TORQUE ANALYZERS”>
<meta http-equiv=”refresh” content=”3;URL=/inquiries.html”>

<link rel=stylesheet href=”stylesheet2004.css” type=”text/css”>

<script language=”javascript” src=”hide.js” type=”text/javascript”></script>

</head>
<body>

<nobr><div align=”center”><img src=”images/top_force-torque_banner1.gif” alt=”MARK-10: The force gauge and torque gauge source” width=”385″ height=”65″ border=”0″><img src=”images/top_force-torque_banner2.gif” alt=”MARK-10 force gauges, torque gauges, and testing systems” width=”386″ height=”65″ border=”0″></div></nobr>
<nobr><div align=”center”><a href=”/index.html”><img src=”images/top_force-torque_banner3.gif” alt=”MARK-10: A leader in force measurement and torque measurement testing systems” width=”385″ height=”65″ border=”0″></a><img src=”images/top_force-torque_banner4.gif” alt=”Identify a MARK-10 force gauge or torque gauge for your force measurement and torque measurement requirements” width=”386″ height=”65″ border=”0″></div></nobr>

<table border=”0″ width=”770″ cellspacing=”0″ cellpadding=”0″ align=”center”>
<tr>
<td class=”tagline”><img src=”images/we make.jpg” width=”400″ height=”19″></td>
<td align=”right”><img src=”images/home_flag.gif” alt=”USA Flag” width=”19″ height=”14″ border=”0″><span class=”tagline_phone”>888-MARK-TEN</span></td>
</tr>
</table>

<table border=”0″ width=”770″ cellspacing=”0″ cellpadding=”0″ align=”center”>
<tr>
<td id=”navigation” valign=”top” width=”155″>
<!– navigation–>

<script language=”javascript” src=”navigation.js” type=”text/javascript”></script>
<noscript>
<div class=”nav”><a href=”/instruments/index.html”>Products</a></div>
<div class=”nav”><a href=”/applications/index.html”>Applications Gallery</a></div>
<div class=”nav”><a href=”/downloads.html”>Product Literature</a></div>
<div class=”nav”><a href=”/order.html”>How to Order</a></div>
<div class=”nav”><a href=”/about.html”>About Mark-10</a></div>
<div class=”nav”><a href=”/contact.html”>Contact Us</a></div>
<div class=”nav”><a href=”/faq.html”>Frequent Questions</a></div>
<div class=”nav”><a href=”/distributor.html”>Distributors</a></div>
<div class=”nav”><a href=”/tools/angle.html”>Conversion Calculators</a></div>
<div class=”nav”><a href=”/list.html”>Join Mailing List</a></div>
<div class=”nav”><a href=”/index.html”>Home</a></div></noscript>

<!– end navigation–>
</td>

<td id=”spacer” class=”divid”><img src=”images/spacer.gif” alt=”” width=”11″ height=”1″ border=”0″></td>

<td id=”content_table”>

<h1 style=”color: #F00″>Error </h1>

<?php
echo ‘<p>Please correct the following errors :</p>
<ul>
<li>’.implode( ‘</li>’.”n”.'<li>’, $errors ).'</li>
</ul>’.”n”;
?>

</td>
</tr>
</table>

<script language=”javascript” src=”../bottomnav.js” type=”text/javascript”></script>

</body>
</html>
<?php
}
else
{
//sends email via php to the following address
$mailuser = “[email protected]”;

//echo ‘default chosen address: ‘.$mailuser;

$header = “Return-Path: “.$mailuser.”rn”;
$header .= “From: Mark-10 Web Inquiry<“.$mailuser.”>rn”;
$header .= “Content-Type: text/html;”;

$mail_body =
‘The User: ‘.$_POST[name].’ has sent his input.
Name: ‘. $_POST[name] . ‘<br>
Mr:’ . $_POST[Mr] .'<br>
Ms:’ . $_POST[Ms] .'<br>
Title: ‘. $_POST[title] . ‘<br>
email: ‘. $_POST[email] . ‘<br>
confirm email: ‘. $_POST[email2] . ‘<br>
Company: ‘. $_POST[Company] . ‘<br>
Address: ‘. $_POST[address] . ‘<br>
Address: ‘. $_POST[address2] . ‘<br>
City: ‘. $_POST[city] . ‘<br>
State: ‘. $_POST[region] . ‘<br>
Zip: ‘. $_POST[zip] . ‘<br>
Country: ‘. $_POST[country] . ‘<br>
Telephone: ‘. $_POST[telephone] . ‘<br>
Extention: ‘. $_POST[ext] . ‘<br>
fax: ‘. $_POST[fax] . ‘<br>
Application or Question:’ . $_POST[comments] .'<br>
Sales call Needed:’ . $_POST[sales] .'<br>
Catalog:’ . $_POST[SendCatalog] .'<br>
Mailing List:’ . $_POST[AddToMailingList] .'<br>’;
mail ($mailuser, ‘Mark-10 Web Inquiry’, $mail_body, $header);
header(“Location: http://www.mark-10.com/thankyou-inquiry.html”);
}
?> [/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@OctoberWindApr 07.2011 — One method by using trim()
[code=php]

foreach($_POST as $postKY => $postVAL)
{
if ((!isset($_POST[$postKY])) || (trim($postVAL) == ""))
{
$error[$postKY] = "There was an error with " . $postKY . ". Please check your input and try again.";
}
}
[/code]
Copy linkTweet thisAlerts:
@BelrickApr 09.2011 — Suggest you use javascript to validate for this however.
Copy linkTweet thisAlerts:
@themartyApr 11.2011 — @OctoberWind

the !isset() is not necesarry: you're looping over the values of an array, so they key will always exist.

A much better approach would be to define a custom array of keys of required fields.

@Belrick: never ever rely on client-side validation! You can [B]add[/B] to make it more user friendly, but you always have to validate (again) server-side
Copy linkTweet thisAlerts:
@OctoberWindApr 11.2011 — 
the !isset() is not necesarry: you're looping over the values of an array, so they key will always exist.

A much better approach would be to define a custom array of keys of required fields.
[/QUOTE]


That's very true; I use a lot of lookup arrays when processing forms, so that's why I'm used to always checking of something exists before I do anything to/with it.

[code=php]
// $formDef = array of expected data
for each ($formDef as $ky => $val)
{
if (isset($_POST[$ky])) { // do whatever }
[/code]
Copy linkTweet thisAlerts:
@BelrickApr 11.2011 — I use client side to ensure theyve filled in * fields then i immunize/sterilize server side.
Copy linkTweet thisAlerts:
@mikeroqApr 11.2011 — To accomplish this server side use preg_match here is a [url=http://www.htmlcenter.com/blog/php-form-validation/]tutorial[/url] for you.
Copy linkTweet thisAlerts:
@hastxApr 11.2011 — Check this thread for similar discussion which might be helpful.
×

Success!

Help @Designer525 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...