PHP form sent before before page error have been fixed

I have been working on this code for awhile know. I have my validation running but not as well I like it. When I go and fill out the form i purposely miss some input and text box to test my validation and press send on the form which work and tell me what I am missing but in the mean time the PHP script send the email before I fix the missing field can anyone tell me why? and how I can fix this problem.

[php]

<?php If($_POST["submit"]){ If (isset ($_POST['name'] , $_POST['siteArea'] , $_POST['problem'] , $_POST['email'] , $_POST['comment'])) { if (empty($_POST['name'])){ //Validate user Name $errors[] = "Please enter a Name"; } else{ $name = htmlentities($_POST['name']); } if (empty($_POST['siteArea'])){ //Validate user Name $errors[] = "You have not entered where the stie problem is"; } else{ $siteArea = htmlentities($_POST['siteArea']); } if (empty($_POST['problem'])){ //Validate user Name $errors[] = "You have not entered what the problem is"; } else{ $problem = htmlentities($_POST['problem']); } // Validate sending email If(empty($_POST['email'])){ $errors[] = "please enter a validate eMail Address."; }else if(strlen($_POST['email']) > 350){ $errors[] = "Your eMail maybe to long. Please review and correct."; }else if (filter_var($_POST['email'] , FILTER_VALIDATE_EMAIL) === false){ $errors[] = "Please provide your right email address."; }else { $email = "<" .htmlentities($_POST['email']) . ">"; } //// Validate the input message text If(empty($POST['comment'])){ $errors[] = "Please enter in a message."; }else{ $comment = htmlentities($_POST['comment']); } } $myemail = 'root@localhost'; $to = $myemail; $subject = "Your Lady of interest name: $firstName $lastNamt"; $name = $_POST["name"]; $siteArea = $_POST["siteArea"]; $problem = $_POST["problem"]; $email = $_POST["email"]; $comment = $_POST["comment"]; //--------------------------------------------------Main Message------------------------------------------////// $message = " Dear Dean, My name is $name I have been having a good like though you dating site and I come across this problem which is located in the $siteArea. I found the problem being $problem, I will try to explain a bit better for you about the problem that I have come across. $comment Kind regards $name Email: $email "; $ok = mail($to, $subject,$message ); } ?>

[/php]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Sending a Technical Message</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="robots" content="noindex" />
<meta name="robots" content="nofollow" />
</head><body class="mainB" leftmargin="1" topmargin="1" bgproperties="fixed" marginwidth="0">
<?php
	If(empty($errors)=== false){?>
		<ol>
			<?php
				foreach ($errors as $error){
					echo "<li>", $error,"</li>";
				}
			?>
			</ol>
			<?php
			}else{
				if(isset ($ok)) {
					$ok;
  	
			}
			
	}
?>
 <table width="75%" align="center" border="0"><form action="" method="post"  name="feedback">
  <tr>
<td> <table width="88%" border="0" align="center" cellpadding="2" cellspacing="3" bordercolor="#FF0066">
                <tr bordercolor="#FF00FF"> 
                  <td align="right"><label for="name" class="dateFormFieldNames">Name</label><br><font class="red">Required</font></td>
                  <td width="79%"><input name="name" id="name" size="16" maxlength="16" class="select2" placeholder="Enter your name" />

                  </td>
                </tr>
                <tr bordercolor="#FF00FF"> 
                  <td width="21%" align="right"><label for="siteArea" class="dateFormFieldNames">Site earea</label><br><font class="red">Required</font></td>
                  <td colspan="3"> <div> 
                      <select name="siteArea" id="siteArea" class="select">
                        <option value="" selected>Select Site 
                        Area</option>
                        <option value="My Full Bio">My Full Bio</option>
                        <option value="Image  Gallary">Image Gallary</option>
                        <option value="Send me a message">Send me a message</option>
                        <option value="Techical Issues">Techical Issues</option>
                      </select>
                    </div>
</td>
                </tr>
                <tr> 
                  <td align="right"><label for="problem" class="dateFormFieldNames">Problems</label><br><font class="red">Required</font></td>
                  <td colspan="3"><div> 
                      <select name="problem" id="problem" class="select">
                        <option value="" selected>Select the Problem</option>
                        <option value="A Broken link">A Broken link</option>
                        <option value="Page(s) missing">Page(s) missing</option>
                        <option value="Photo(s) missing">Photo(s) missing</option>
                        <option value="Link(s) not working">Link(s) not working</option>
                        <option value="other">>other</option>
                      </select>
                  </div></td>
                </tr>
                <tr bordercolor="#FF00FF"> 
                  <td align="right" bordercolor="#FF00FF" ><label for="email" class="dateFormFieldNames">Email Address</label><br><font class="red">Required</font></td>
                  <td colspan="3"><input name="email" type="text" id="email" class="email" placeholder="Enter your email address" /></td>
                </tr>
                <tr> 
                  <td rowspan="2" align="right"><label for="explenation" class="dateFormFieldNames">Explenation</label><br><font class="red">Required</font></td>
                  <td colspan="3">Please explain in further detail what the problem 
                    maybe and where it is or I have not listed any of the problems 
                    above. Please tell me what they are </td>
                </tr>
                <tr> 
                  <td colspan="3"><textarea name="comment" cols="50" rows="5" placeholder="Enter in your best explaination to where the problem is and what the problem is"></textarea>  </td>
                </tr>
                <tr bordercolor="#FF00FF"> 
                  <td colspan="4" align="center" valign="middle" ><input type="submit" name="submit" value="Submit"/> 
                  </td>
                </tr>
              </table>
              <br>
              </form>
</td></tr>
</table>

</html>
</body>

Simple enough. First, don’t place your errors into an array. takes too long to display it that ways.

Use a simple variable.

So, set the $errors = “”; first.
Then, everywhere you want to add an error use $errors .= "name is missing or whatever…
";
In this manner, you append the error on top of any others. Note the line break!

Next, place lines 52 to 72 INSIDE this:

if ($errors != “”) {
echo “Your form entries have errors:
”;
echo $errors;
} else {
// place lines 52 to 72 here to handle sending emails if no errors found…
}

Something like that should work. No sense in setting up the email at all if there are validation errors…
Hope that is what you were looking for. Good luck!

thank you for you suggestion I will give this ago

Well, we are here after you test it all again…

Let us know if it works or doesn’t… We can help…

hi ErnieAlex
I only just back to this problem been busy error checking and tried your solution but but not working still posting the form even though there are error

Well, one more thing, on the HTML section, you can just echo the new $errors instead of parsing
thru an array of them.

SO, please repost your current code so we can see what changes you made.

Also, is this one big file where you separated the posts into two parts? If so, just post the one full file…

dear Ernie
I have post an attachment so you can see my whole code as it is a lot bigger then 20000 characters. I tried to cut it down but did do as much as I may be removing something that could of given you the idea where I have problems.
hope you can find the attached file it is:
conactPage.php in side of the conactPage.zip file


conactPage.zip (8.96 KB)

hi Ernie
I still have the problem. i have played with the code a bit more and pulling it apart I have now found that the problem lays in the code where i am trying to attach a file.

[php]$att = $_FILES[‘att’];
$att_path = $_FILES[‘att’][‘tmp_name’];
$att_name = $_FILES[‘att’][‘name’];
$att_size = $_FILES[‘att’][‘size’];
$att_type = $_FILES[‘att’][‘type’];

#open, read then close the file
$fp = fopen( $att_path, "rb");
$file = fread( $fp, $att_size );
fclose( $fp );

#create a boundary string
$num = md5(time());
$str = "==Multipart_Boundary_x{$num}x";

#encode the data for safe transit 
#and intersperse 76-character chunks with \r\n
$file = chunk_split(base64_encode($file));

#define header
$hdr  = "www.deanross.com.au\n";
$hdr .= "MIME-Version: 1.0\n";
$hdr .= "Content-Type: multipart/mixed;\n ";
$hdr .= "boundary=\"{$str}\"";

#define message
$msg = "This is a multi-part message in MIME format\n\n";
$msg .= "--{$str}\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$msg .= "Content-Transfer-Encoding: 16bit\n\n";
$msg .= "$message\n\n";
$msg .= "--{$str}\n";

#define the non-text attachment
$msg .= "Content-Type: {$att_type};\n";
$msg .= "name=\"{$att_name}\"\n";
$msg .= "Content-Disposition: attachment;\n";
$msg .= "filename=\"{$att_name}\"\n";
$msg .= "Content-Transfer-Encoding: base64\n\n";
$msg .= "$file\n\n";
$msg .= "--{$str}";[/php]

I have remove that bit of the code and work fine tells me that I have errors and does not send the form, but when I put the bit code back it has gone back to sending the form when there are still errors or filling fields.

I found a few errors and posted the code, but, it looks like it did not post.

(I hope I didn’t post it to the wrong thread…)

Anyway, I found a lot of errors that had to do with printing the errors.
Attached is my version. One main issue is that you check for fields to see if they are there.
Then, you check if the are not there and post error messages. So, you do not need both of these.
Try this version and I will look at your upload files code now…
Okay, found it did not send because I didn’t zip it back up… Here it is


contactPage.zip (9.01 KB)

hi Ernie
I want to thank you very much for helping out with this, much appreciated

No trouble at all. Glad I can help. Let us know if that version works. Off to bed now.
Will check in with you in the morning. Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service