There are 2 ways I can think of, but your if and else statements doesn't look right.
Should look like this:
if(isset($_POST['submit_form']))
{
// do your checking, mail function, and you "successful" message.
}
else
{
//show your form to filled out
}
Here's the method's I would use for form checking.
<!-- check form -->
<script type="text/javascript">
function check(form)
{
var jserror="";
if (form.contact_name.value=="")
jserror+="Please enter a Contact Name!\r\n";
if (form.contact_phone.value.is_numeric(0-9)==-1"")
jserror+="Please Enter a Valid Contact Number!\r\n";
if (form.company_name.value=="")
jserror+="Enter a Company Name!\r\n";
if (jserror)
{
alert(jserror);
return false;
}
}
</script>
<form onSubmit="return check(this)" action="<? $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
Contact Name:<br />
<input type="text" name="contact_name" /><br /><br />
Company Name:<br />
<input type="text" name="company_name" /><br /><br />
Email Address:<br />
<input type="text" name="contact_phone" />
You can also check form like this, specially for e-mail address:
<?
if(isset($_POST['submit_form']))
{
if ($_POST['contact_name'] == "" or $_POST['company_name'] == "" or $_POST['contact_phone'] == "")
{
$msg1 = true;
}
$email_address = $_POST['email_address'];
if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", $email_address))
{
$msg2 = true;
}
}
?>
<form action="<? $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
Contact Name:<br />
<input type="text" name="contact_name" /><br /><br />
Company Name:<br />
<input type="text" name="company_name" /><br /><br />
Email Address:<br />
<input type="text" name="email_address" /><br /><br />
Contact Phone Number:<br />
<input type="text" name="contact_phone" />
<input type="submit" name="submit_form" value="Submit" />
<?
if ($msg1) {$msg = "Error, you must fill in all fields";}
if ($msg2) {$msg = "Error, you must enter a valid email address";}
echo $msg;
?>
</form>
I'm sure there's lots of other ways, which also might be better, but this works for me. Just check your if and else...