Page 1 of 1

Need a PHP form makeover

Posted: Tue Sep 20, 2005 11:24 pm
by bruceg
Hello,

I have a form located here http://www.inspired-evolution.com/Contact_Form.php and it works as intended so far, but could use some improvement.

what I want to do is:

1) add code which checks for characters in the email field and if the proper characters aren't entered, sends an error.

2)when fields are left blank, return the fields with hilighted backgrounds preferably using CSS

3) have a generic error appear at the top of the form saying something like 'Not all fields have not been entered', and have maybe a ! appear beside these fields (which have hilighted backgrounds).

current php code is:

Code: Select all

<?php
$form_block=<<<END_FORM
<form method="post" action="{$_SERVER['PHP_SELF']}" class="info_request">
<fieldset class="left">
<legend title="About You">About You</legend>

<p><label for="firstname"><span class="red">*</span> First Name: </label><br />

<input id="firstname" name="firstname" type="text" value="{$_POST['firstname']}" /></p>

<p><label for="lastname"><span class="red">*</span> Last Name:</label><br />

<input id="lastname" name="lastname" type="text" value="{$_POST['lastname']}" /></p>

<p><label for="company"><span class="red">*</span> Company: </label><br />

<input id="company" name="company" type="text" value="{$_POST['company']}" /></p>

<p><label for="phone"><span class="red">*</span> Phone: </label><br />

<input id="phone" name="phone" type="text" value="{$_POST['phone']}" /></p>

<p><label for="email"><span class="red">*</span> e-mail: </label><br />

<input id="email" name="email" type="text" value="{$_POST['email']}" /></p>

<p><label for="email2"><span class="red">*</span> re-enter e-mail: </label><br />

<input id="email2" name="email2" type="text" value="{$_POST['email2']}" /></p>
</fieldset>
<fieldset>
<legend title="More Info.">More Info.</legend>
<p><label for="URL"><span class="red">*</span>  URL:</label><br />

<input id="URL" type="text" name="URL" value="{$_POST['URL']}"/> </p>

<p><label for="Contact_Preference"><span class="red">*</span>  Best way to reach:</label><br />

<select name="Contact_Preference" id="Contact_Preference">
<option value="email">email</option>
<option value="phone">phone</option>
<option value="snail_mail">snail mail</option>

</select>
</p>

<p><label for="Contact_Time"><span class="red">*</span>  Best time to contact:</label><br />

<select name="Contact_Time" id="Contact_Time">

<option value="morning">morning</option>
<option value="evening">evening</option>
<option value="anytime">anytime</option>

</select></p>

<input type="hidden" name="op" value="ds" />

<textarea name="message" id="message" rows="" cols="" >Send us a detailed message specifying what you wish to accomplish with your web site. </textarea>
<input class="submit" src="/images/submit.gif" alt="Submit" type="image" name="submit"  /> 

</fieldset>
</form>
</div>
<p><span class="red">*</span> indicates a required field (all fields are required).</p>
END_FORM;
if ($_POST['op']!='ds') { 
    echo "$form_block";
    } else if ($_POST["op"]  == "ds")  {
//Function saves time and space by eliminating unneccesary code
    $first_name_err = '<span class="red">Please enter your first name!</span><br />'; 
} 

    $last_name_err = '<span class="red">Please enter your last name!</span><br />'; 
} 
function check($fieldname)
	{
	global $err_msg;
	if($_POST[$fieldname] == "")

		if ( !isset($err_msg)) { $err_msg = "<span class='red'>You haven't entered your ".$fieldname."!</span><br />"; }
		elseif ( isset($err_msg)) { $err_msg .="\n<span class='red'>You haven't entered your ".$fieldname."!</span><br />"; }
		}
	return $err_msg;
	}

//////////////////////////
///Function execution/////
//////////////////////////

check('firstname');
check('lastname');
check('company');
check('phone');
check('email');
check('email2');
check('URL');
check('Contact_Preference');
check('Contact_Time');
check('message');  

//Validating Email Address
if ($_POST['email'] != $_POST['email2']) { $email_err = "\n<span class='red'>e-mail address fields do not match!</span>"; }

if (isset($err_msg) || isset($email_err)) { echo $err_msg.$email_err."\n\n".$form_block; }
else {
  //it's ok to send, so build the mail
        $msg = "E-mail sent from http://www.inspired-evolution.com\n";
        $msg .="Sender's first name:        {$_POST['firstname']}\n";
        $msg .="Sender's last name:        {$_POST['lastname']}\n";
        $msg .="Company name:        {$_POST['company']}\n";
        $msg .="Senders Phone number:        {$_POST['phone']}\n";
        $msg .="Senders email address:        {$_POST['email']}\n";
        $msg .="Senders email address (re-typed):        {$_POST['email2']}\n";
        $msg .="The website is :        {$_POST['URL']}\n";
        $msg .="I prefer to be contacted via: {$_POST['Contact_Preference']}\n";
        $msg .="The Best time to contact is: {$_POST['Contact_Time']}\n";
        $msg .="Message:        {$_POST['message']}\n\n";
        $to ="webguync@gmail.com";
        $subject ="There has been a disturbance in the force";
        $mailheaders ="From: Inspired-Evolution.com
        <http://www.inspired-evolution.com>\n";
        $mailheaders .="Reply-To: {$_POST['email']}\n";
        //send the mail
        mail ($to, $subject, $msg, $mailheaders);
        //display information to user
  echo "<p>Hola, <strong>$firstname</strong>!.<br /><br />
We have received your request for a web site review , and will respond shortly.<br />
Thanks for visiting inspired-evolution.com and have a wonderful day!<br /><br />
Regards,<br /><br />
<strong>Inspired Evolution</strong></p>";
}

}
?>
I am greatly appreciative if any or all of the iissues can be addressed

thanks!

Posted: Wed Sep 21, 2005 1:01 am
by s.dot
The following will make sure a valid email address has been entered.

Code: Select all

if(!eregi("^(.+)@(.+)\\.(.+)$", $_POST['email']))
{
  die('invalid email');
}
For the missing fields, choose a list of form fields that are required. Make a variable = true. if at the end of your form, your variable still = true, then process the form, else show the errors. Something like the following:

Code: Select all

// set variable

$ok = "true";

// set required form fields

if(!$field1 || !$field2 || !$field3)
{
   $error_msg = "You did not complete the required form entries";
   $ok = "false";
}

if(insertsomeotherbooleanhere)
{
   $error_msg .= "<BR />Some other error message.";
   $ok = "false";
}

// Decide to process the form or not by checking if $ok is still true

if($ok == "true")
{
  // process form
} ELSE
{
  // show form again along with echo $error_msg
}

Posted: Wed Sep 21, 2005 1:07 am
by feyd
to clarify.. that regex does not do official validation of an email address, only does a quick check for basic validity...

Posted: Wed Sep 21, 2005 1:13 am
by s.dot
Also, you could use the email as part of your error message if it is invalid.

Code: Select all

if(!eregi("regexabove",$_POST['email']))
{
  $error_msg .= "<BR />Your email appears to be invalid.";
  $ok = "false";
}

Posted: Wed Sep 21, 2005 9:48 pm
by bruceg
thanks for the tips about email. Any help on hilighting the backgrounds of the fields when there are not filled out?

Posted: Wed Sep 21, 2005 10:23 pm
by s.dot
write a css class in your style sheet for error forms (like red border or something) then when writing the form..

Code: Select all

<input class="<? if($error_msg){ echo "errorclass"; } ELSE { echo "normalformclass"; } ?>" name="name">
That way, if an error message was generated (using the code above) the class of that form element would be of class "error" and would provide the example red border.

Posted: Wed Sep 21, 2005 10:30 pm
by bruceg
o.k, so instead of what I have now

Code: Select all

<p><label for="firstname">First Name: </label><br /> 

<input id="firstname" name="firstname" type="text" value="{$_POST['firstname']}" /></p>
I would need to use what you have. Can I just use the existing ID to add in the CSS a red border (or whatever).

Posted: Wed Sep 21, 2005 10:33 pm
by s.dot
You could. But then it seems like it would have a red border around all form elements.

Just copy the css you have for regular (non error) form elements

and add

Code: Select all

border: solid 1px #FF0000;
to it ;)

Call this your 'error' class, and use that as the id for forms elements that have errored.

Posted: Wed Sep 21, 2005 10:59 pm
by bruceg
ah, so errorclass and normalformclass would be css classes

.errorclass{/*error form css stuff*/
}

.normalformclass{/*what I already have for the field css*/
}