Page 1 of 1

some issues with error checking

Posted: Tue Aug 02, 2005 10:54 pm
by bruceg
I am getting close to getting my form working located at http://www.inspired-evolution.com/Contact_Form.php

but there are some issues with the required fields. The errors should be showing up when you don't put in first name, last name, company name, phone number and twice entering email address. Right now it looks like the error msgs. skip over email and company, and only displays two errors at a time. I want all of the errors to display when the data isn't entered/

my php form code currently is as follows:

Code: Select all

<?php
$form_block=<<<END_FORM
<form method="POST" action="{$_SERVER['PHP_SELF']}" class="info_request">
<fieldset>
<legend title="additional information request">Additional Information Request</legend>
<table>
<tr>
<td>
<label for="firstname"><span class="red">*</span> First Name: </label></td>
<td>
<input id="firstname" name="firstname" type="text" value="{$_POST['firstname']}" />
</td>
</tr>
<tr>
<td>
<label for="lastname"><span class="red">*</span> Last Name:</label></td>
<td>
<input id="lastname" name="lastname" type="text" value="{$_POST['lastname']}" />
</td>
</tr>
<tr>
<td>
<label for="company"><span class="red">*</span> Company: </label></td>
<td>
<input id="company" name="company" type="text" value="{$_POST['company']}" /></td>
</tr>
<tr>
<td>
<label for="phone"><span class="red">*</span> Phone: </label></td>
<td>
<input id="phone" name="phone" type="text" value="{$_POST['phone']}" /></td>
</tr>
<tr>
<td>
<label for="email"><span class="red">*</span> e-mail: </label></td>
<td>
<input id="email" name="email" type="text" value="{$_POST['email']}" /></td>
</tr>
<tr>
<td>
<label for="email2"><span class="red">*</span> re-enter e-mail: </label></td>
<td>
<input id="email2" name="email2" type="text" value="{$_POST['email2']}" />
</td>
</tr>
<tr>
<td>
<label for="URL"> URL:</label></td>
<td>
<input id="URL" type="text" name="URL" /> </td>
</tr>
<tr>
<td>
<label for="Contact_Preference"> Best way to reach:</label></td>
<td>
<input id="Contact_Preference" name="Contact_Preference" type="text" value="{$_POST['Contact_Preference']}" /></td>
</tr>
<tr>
<td>
<label for="Contact_Time"> Best time to contact:</label></td>
<td>
<input id="Contact_Time" name="Contact_Time" type="text" value="{$_POST['Contact_Time']}" />
</td>
</tr>
<tr>
<td>
<input type="hidden" name="op" value="ds" />
</td>
</tr>
<tr>
<td>
<textarea name="Textarea" rows="25" cols="50">Send me 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"  /> 
</td>
</tr>
</table>
</fieldset>
</form>
<p><span class="red">*</span> indicates a required field.</p>
END_FORM;
if ($_POST['op']!='ds') { 
    // they need to see the form
    echo "$form_block";
    } else if ($_POST["op"]  == "ds")  {
        // check value of $_POST['firstname']
        if ($_POST['firstname'] == "")  {
            $name_err = '<span class="red">Please enter your first name!</span><br />';
            $send = "no" ;
    }
	// check value of $_POST['lastname']
	if ($_POST['lastname'] == "")  {
            $name_err = '<span class="red">Please enter your last name!</span><br />';
            $send = "no" ;
    }
	
	// check value of $_POST['company']
	if ($_POST['company'] == "")  {
            $name_err = '<span class="red">Please enter your company name!</span><br />';
            $send = "no" ;
    }
	
	// check value of $_POST['phone']
	if ($_POST['phone'] == "")  {
            $name_err = '<span class="red">Please enter your phone number!</span><br />';
            $send = "no" ;
    }
// check value of $POST['email']
if ($_POST ['email'] == "")  {
    $email_err ='<span class="red">Please enter your email address!</span><br />';
    $send="no";
}

// check value of $POST['email2']
if ($_POST ['email2'] == "")  {
    $email_err ='<span class="red">Please re-enter your email address!</span><br />';
    $send="no";
}
if ($send != "no") {
        //it's ok to send, so build the mail
        $msg = "E-mail sent from www site\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 .="URL :        {$_POST['URL']}\n";
        $msg .="Contact_Preference: {$_POST['Contact_Preference']}\n";
                $msg .="Contact_Time {$_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 additional information, 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>";
 
} else if ($send =="no") {
        //print error messages
        echo "$name_err";
        echo "$email_err";
        echo "$message_err";
        echo "$form_block";
        }
}
?>
if anyone has time, take a look and let me know if there are any obvious errors I am missing.

thanks!

Posted: Wed Sep 07, 2005 4:39 pm
by J_Iceman05
first thing i see wrong, is that someone can just enter spaces " " and it will accept, you need to check for those.

on your code here, the reason it would only show a few, is because you use the same variable for multiple things, $name_err is used for first name, last name, company name, and phone number.... i think you caught that though, i checked your site and it displays them all..
for your phone number... you might want to check for letters, or do a bit more verification on that.
since you are just checking if the fields exist.. they can actually just put whatever they want in the field and your form will accept it.

something i found to be very useful is the eregi() function and its complimentary functions

Code: Select all

// force them to fill in the field and allow for only characters no numbers or symbols

if (!eregi('([a-z])', $_POST['firstname'])) {
    $first_name_err = '<span class="red">Please enter your first name!</span><br />';
}
if (!eregi('([a-z])', $_POST['lastname'])) {
    $last_name_err = '<span class="red">Please enter your last name!</span><br />';
}
rather than me trying to explain everything, i think php.net does a much beter job at it than i could
http://us2.php.net/eregi
it also has links to the complimentary functions such as ereg()

I don't know what else you have fixed or changed since you posted this, so I dont what else I might be able to help with... Let me know how it's going

Posted: Wed Sep 07, 2005 7:18 pm
by feyd
advice: use preg_* instead of ereg*. ereg runs very slow compared to preg_*

Posted: Thu Sep 08, 2005 11:52 am
by J_Iceman05
preg_ versus ereg_... that all depends on the situation. although i think for most of the time, you may be right. ereg is better for certain situations

Posted: Thu Sep 08, 2005 11:55 am
by feyd
I've never had it work better, or faster. All my tests have shown that preg will be damn near twice as fast as ereg