Form Validation Trouble

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

brentaserio
Forum Newbie
Posts: 8
Joined: Sun Dec 21, 2008 6:10 pm

Form Validation Trouble

Post by brentaserio »

Hello Everyone,

Created a great form, emailing works great, but no validation yet. I have tried different things and it works for text boxes but not drop downs and ect. I'm trying to find some basic validation to make sure there is data in the field and a drop down is selected. No security is really needed since this will be for internal AD protected use. I have an htm form submitting to this php page to send email. Any tips on validation for these.

Code: Select all

 
<?php
 
// REQUEST FORM DATA \\
 
$remail = $_REQUEST['remail'] ; // Text Box - Required
$rlname = $_REQUEST['rlname'] ; // Text Box - Required
$rfname = $_REQUEST['rfname'] ; // Text Box - Required
$nalname = $_REQUEST['nalname'] ; // Text Box - Required
$nafname = $_REQUEST['nafname'] ; // Text Box - Required
$napoffice = $_REQUEST['napoffice'] ;// Combo Box - Required - (Values = 0 to 9) - (0 = Select Field)
$najtitle = $_REQUEST['najtitle'] ;// Combo Box - Required - (Values = 0 to 14) - (0 = Select Field)
$na1a = $_REQUEST['na1a'] ;
$na1b = $_REQUEST['na1b'] ;
$na1c = $_REQUEST['na1c'] ;
 
// HEADER INFORMATION \\
$to = "support@azvent.com"; 
$subject = "New User Request"; 
$header = "From: $rfname \r $rlname";
 
// BODY CONTENT \\
$message = ":::Requestee Information::: \n\n". "$rlname, $rfname \n".
       "$remail \n\n". ":::New User Information::: \n\n". "$nalname, $nafname \n".
       "$napoffice, $najtitle \n\n". "Requires Phone Extention:\n".
       "$na1a, $na1b, $na1c";
 
// FORM VALIDATION \\
 
echo "$message";
 
?>
 
Thank You!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Form Validation Trouble

Post by califdon »

Are you only trying to insure that inputs are not left empty? If so, you can just test each one with the PHP isempty() function. Probably a smoother way (for the user), though, would be to validate in Javascript on the browser, so that they don't fill in a complex form and press Submit, only to have it come back from the server telling them to start again because something wasn't filled in.
brentaserio
Forum Newbie
Posts: 8
Joined: Sun Dec 21, 2008 6:10 pm

Re: Form Validation Trouble

Post by brentaserio »

I tried that initially, and could not get the $napoffice and $najtitle to work using the below format. Maybe I am using the improper format for this.
I also hope there is a much shorter way to do this. Something like if (empty($rlname, $rfname, $remail)) but that didn't work. Obviously I am new
at this. Tried to figure it out before posting. Appreciate the help.
if (empty($rlname)) {
echo "<h3>A PROBLEM HAS BEEN ENCOUNTERED</h3>The following field has no data:<br><u>Requestee - Last Name</u>
<p>Please click the button below to complete the form.</p>";
echo "<a href='javascript:history.back()'>Return To Previous Page</a>";
}
elseif (empty($rfname)) {
echo "<h3>A PROBLEM HAS BEEN ENCOUNTERED</h3>The following field has no data:<br><u>Requestee - First Name</u>
<p>Please click the button below to complete the form.</p>";
echo "<a href='javascript:history.back()'>Return To Previous Page</a>";
}
elseif (empty($remail)) {
echo "<h3>A PROBLEM HAS BEEN ENCOUNTERED</h3>The following field has no data:<br><u>Requestee - Email Address</u>
<p>Please click the button below to complete the form.</p>";
echo "<a href='javascript:history.back()'>Return To Previous Page</a>";
}
elseif (empty($nalname)) {
echo "<h3>A PROBLEM HAS BEEN ENCOUNTERED</h3>The following field has no data:<br><u>New User - Last Name</u>
<p>Please click the button below to complete the form.</p>";
echo "<a href='javascript:history.back()'>Return To Previous Page</a>";
}
elseif (empty($nafname)) {
echo "<h3>A PROBLEM HAS BEEN ENCOUNTERED</h3>The following field has no data:<br><u>New User - First Name</u>
<p>Please click the button below to complete the form.</p>";
echo "<a href='javascript:history.back()'>Return To Previous Page</a>";
}
else {
mail($to, $subject, $message, $header);
echo "<h3>The <u>New User Request Form</u> Has Ben Sent Successfully</h3><p>:::Requestee Information:::</p>$rlname, $rfname<br>$remail";
}
Last edited by brentaserio on Sun Dec 21, 2008 7:05 pm, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Form Validation Trouble

Post by VladSun »

Instead of using elseif use if in this way:

Code: Select all

$errors = Array();
 
if (empty($rlname)) 
{
    $errors[]  = "<h3>A PROBLEM HAS BEEN ENCOUNTERED</h3>The following field has no data:<br><u>Requestee - Last Name</u>
<p>Please click the button below to complete the form.</p>";
}
 
if (empty($rfname)) 
{
    $errors[] = "<h3>A PROBLEM HAS BEEN ENCOUNTERED</h3>The following field has no data:<br><u>Requestee - First Name</u>
<p>Please click the button below to complete the form.</p>";
}
 
.......
 
if (count($errors)) // Has some errors
{
    foreach($errors as $error)
        echo $error;
}
else
{
..... // everything is fine - proceed
}
 
This way you will inform the user for ALL errors he has made, in oposite of informing for errors one by one
There are 10 types of people in this world, those who understand binary and those who don't
brentaserio
Forum Newbie
Posts: 8
Joined: Sun Dec 21, 2008 6:10 pm

Re: Form Validation Trouble

Post by brentaserio »

I will give that a shot. Is there a way to simplify the code. Seems to be too dirty. something like if (empty($rlname, $rfname, $remail)
or asigh something like $validate = ($rlname), ($rfname), ($remail);
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Form Validation Trouble

Post by VladSun »

Of course you can, but at the end you will have a not so good validation library and there are a lot of nice coded ones ;)
So, instead of writing your own validation/filtering/error-provider library just choose one of the many ready-to-use.

If you don't want it - here you are an example:

Code: Select all

$validaionConfig= array(
    'remail'    => '<h3>A PROBLEM HAS BEEN ENCOUNTERED</h3>The following field has no data:<br><u>Requestee - Last Name</u>',
    'rname'    => '<h3>A PROBLEM HAS BEEN ENCOUNTERED</h3>The following field has no data:<br><u>Requestee - Last Name</u>',
.......
);
 
$errors = array();
foreach($validationConfig as $field => $errMsg)
    if(empty($_POST[$field]))
        $errors[] = $errMsg;
 
if (count($errors))
......
 
There are 10 types of people in this world, those who understand binary and those who don't
brentaserio
Forum Newbie
Posts: 8
Joined: Sun Dec 21, 2008 6:10 pm

Re: Form Validation Trouble

Post by brentaserio »

Any recommendations? The problem I ran into finding a coded one was that they applied to mostly forms that where self containing meaning they didn't use a htm form then submit to php form.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Form Validation Trouble

Post by VladSun »

Look at the CodeIgniter validation library - it can be used as a standalone library and I like it (although I've extended it a litle) :)

http://codeigniter.com/user_guide/libra ... ation.html
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Form Validation Trouble

Post by califdon »

First of all, let me admit my syntax error in my earlier response. I was thinking of isset(), but the function you would probably use would be empty() as both of you indicated.

Two points:
(1) as I pointed out before, you really should be doing this on the client side, using Javascript. Unless you do a lot more coding, to populate the form with correctly entered data, you're likely to <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> off a lot of users, doing it in PHP on a new page after they submit their form. Additional PHP validation before updating the database is always good, especially when security is an issue, but it should be only after having caught obvious omissions before the user submits the form.

(2) in PHP or Javascript, you could use a user defined function with the repeated text, using parameters for the specifics.

I would recommend something like this in Javascript:

Code: Select all

<script type="text/javascript">
  function validate( fld ) {
     alert("A PROBLEM HAS BEEN ENCOUNTERED - The following required field has no data: " + fld );
     return;
  }
</script> 
...
 
<input type='text' name='rlname' onBlur='validate("Last Name");'>
...
 
<input type='text' name='rfname' onBlur='validate("First Name");'>
...
 
That's really not good programming, for several reasons, but my point is that you should be checking for omissions BEFORE you submit the form back to the server.
brentaserio
Forum Newbie
Posts: 8
Joined: Sun Dec 21, 2008 6:10 pm

Re: Form Validation Trouble

Post by brentaserio »

I just put this in play, it seems to be working, but it echo's "Success!" once for each field.
If i put mail(field1, field2, ect) it will email it like 6 times.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Form Validation Trouble

Post by VladSun »

brentaserio wrote:I just put this in play, it seems to be working, but it echo's "Success!" once for each field.
If i put mail(field1, field2, ect) it will email it like 6 times.
???
Please, explain...

I must warn you - mail() is one of the most exploited functions in PHP - you should really validate and FILTER the values of arguments you pass to it!
There are 10 types of people in this world, those who understand binary and those who don't
brentaserio
Forum Newbie
Posts: 8
Joined: Sun Dec 21, 2008 6:10 pm

Re: Form Validation Trouble

Post by brentaserio »

Sorry about that, I just put this in play:
$errors = array();
foreach($validateOffice as $field => $errMsg)

if(empty($_POST[$field]))
{
$errors[] = $errMsg;
echo "$errMsg";
}
else
{
echo "Success!";
}

?>
It works but if the fields are filled out completely and i submit, it echos "Success!" for each required field.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Form Validation Trouble

Post by VladSun »

You should use parts of my first code snippet and my second one together ;)
There are 10 types of people in this world, those who understand binary and those who don't
brentaserio
Forum Newbie
Posts: 8
Joined: Sun Dec 21, 2008 6:10 pm

Re: Form Validation Trouble

Post by brentaserio »

The way it looks, i would either have to use one technique or the other.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Form Validation Trouble

Post by VladSun »

brentaserio wrote:The way it looks, i would either have to use one technique or the other.
Compile! ;)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply