I am trying to get my form to only submit when the submit button is pressed and all variables are correct. I am checking for a variable $actionflag in a hidden input on the form. but it seems to be ignoring that and mailing the form out as soon as a visitor gets visits the page, so I get empty forms. It appears the code checks that all the messages are empty right away and then just emails out the empty form. Here's the code, should I be checking a different variable?
<?php
$message1="";
$message2="";
$message3="";
if ( isset( $actionflag ) && $actionflag=="contact")
{
if ( empty( $form[name] ) ) {
$message1 .="Please enter your name<br>\n";
}
if ( empty( $form[phone] ) ) {
$message3 .="Please enter your phone number<br>\n";
}
if ( empty( $form[email] ) ) {
$message2 .="Please enter your email address <br>\n";
}
else if (ereg(".@...*",$form[email])){
$message2 .="";
}
else
{
$message2 .="Incorrect email address format<br>\n";
}
}
if (($message1 == "" ) && ($message2 == "" ) && ($message3 == "" ))
{
$receiver = "myname@mydomain.com";
$i_am_an = $form['I_am_an'];
$name = $form['name'];
$address = $form['address'];
$city = $form['city'];
$state = $form['state'];
$zip = $form['zipcode'];
$email = $form['email'];
$phone = $form['phone'];
$inquiry = $form['comments'];
$ip = $REMOTE_ADDR;
$browser = $_SERVER['HTTP_USER_AGENT'];
$headers = "MIME-Version: 1.0";
$headers .= "Content-type: text/html; charset=iso-8859-1";
$headers .= "From: ".$email."";
$message =
"I_am_an: $i_am_an\n" .
"Name: $name\n" .
"Address: $address\n" .
"City: $city\n" .
"State: $state\n" .
"Zip: $zip\n" .
"Email: $email\n" .
"Phone: $phone\n" .
"Inquiry: $inquiry\n" .
"IP: $ip\n" .
"Browser: $browser";
mail($receiver, "Inquiry From", $message);
}
?>
the form has the hidden field to check...
<form name="form" action="<?php print $PHP_SELF;?>" method="post">
<input type="hidden" name="actionflag" value="contact">