Php form help

trying out this new php form to help reduce the amount of spam, but I can’t get rid of the “Your submission has been sent sucessfully.” from the top of the form before the form was actually sent.

link to page:
http://andrewshemo.com/test/form/pazrt/contact.php

actual form code within the .php file:


<div id="contact">
<img src="images/leftlogo.png" alt="left logo" width="440px" style="float: left" />
<?php if($_POST['fred'] != "") {
    echo('<p style="color: #8B2323; font-size: 16px; font-weight: bold;">You may be using a text-only browser or you are a spambot. Sorry this has not been submitted</p>');    
}

else {
    if(isset($_POST['name'])) {

        if(($_POST['name'] == "") or ($_POST['email'] == "") or ($_POST['message'] == "")) {
            echo('<p style="color: #8B2323; font-size: 16px; font-weight: bold;">Please fill in all fields</p>');
}

    else {

        $name = $_POST['name'];
        $email = $_POST['email'];
        $problem = $_POST['problem'];
        $comments = $_POST['comments'];
        $emailmessage = "You have received a submission from your contact form.
Name: $name
Email: $email
Problem: $problem
Comments: $comments
        ";


        //Defining mail settings

        $to = "Enter your email address here";
        $subject = "Form submission sent";
        $headers = "From: $email";


            if(isset($name)) {
                mail($to,$subject,$emailmessage,$headers);

}

}

}

}

?>
    
    <?php if(isset($name)) {
    
    echo('<p style="color: #8B2323; font-size: 15px; font-weight: bold;">Your submission has been sent successfully</p>');
    
    } ?>
    
<form action="" method="post" id="contact">
<ul>
<li>Name:<?php echo $yourname; ?><br />
<input type="text" size="30" name="name" id="name" /></li>
<li><br /></li>
<li>Email:<?php echo $email; ?><br />
<input type="text" size="30" name="email" id="email" /></li>
<li><br /></li>
<li>Problem:<?php echo $problem; ?><br />
<select name="problem">
<option value="website problem">Website Problem</option>
<option value="forum problem">Forum Problem</option>
<option value="misc problem">Miscellaneous</option>
</select></li>
<li><br /></li> 
<li>Comments:<?php echo $comments; ?><br />
<textarea name="comments" id="comments" rows="3" cols="45"></textarea></li>
<li><br /></li>
<li><br /></li>
<li><input type="text" id="fred" name="fred" style="visibility: hidden;"/></li>
<li><input type="submit" name="action" value="Submit" />
&nbsp;
<input type="reset" name="reset" value="Reset" /></li>
</ul>
</form>
</div>

entire page code:


<?php
$name = htmlspecialchars($_POST['yourname']);
$email    = htmlspecialchars($_POST['email']);
$problem   = htmlspecialchars($_POST['problem']);
$comments = htmlspecialchars($_POST['comments']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Contact the PAZRT- Pennsylvania Zombie Response Team</title>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="Pennsylvania Zombie Response Team" />
<meta name="copyright" content='All Copyright Pennsylvania Zombie Response Team, all rights reserved' />
<meta name="description" content="PAZRT- Pennsylvania Zombie Response Team is an elite group of people dedicated to the preservation of the human race in the face of a looming zombie outbreak" />
<meta name="keywords" content="pazrt, zrt, pennsylvania zombie response team, pennsylvania zrt, zombies, zombie response team, guns, knives, survival" />
<link href="layout.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="validation.js"></script>
</head>
<body>
<!-- This is the main div -->
<div id="wrapper">
<!-- Start logo coding -->
<div id="logo">
<img src="images/logo.png" alt="main logo" height="168px" style="width: 100%" />
</div>
<!-- Start navigation bar coding -->
<div id="navigation">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about-the-pennsylvania-zombie-response-team">About Us</a></li>
<li><a href="http://www.pazrt.com/forum">Forums</a></li>
<li><a href="http://www.pazrt.com/gallery">Gallery</a></li>
<li><a href="http://www.pazrt.com/forum/forum-zrt-wiki">Wiki</a></li>
<li><a href="#">Support<!--[if gte IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="donate-to-the-pennsylvania-zombie-response-team">Donate</a></li>
<li><a href="share-the-pennsylvania-zombie-response-team">Share</a></li>
<li><a href="http://www.cafepress.com/pazrt">Store</a></li>
</ul>
<!-- End Support drop-down menu -->
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li><!-- End support -->
<li><a href="#">Social<!--[if gte IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="http://www.twitter.com/pazrt">Twitter</a></li>
<li><a href="http://www.facebook.com/pages/Pennsylvania-Zombie-Response-Team/10150095905020602">Facebook</a></li>
<li><a href="http://www.youtube.com/pazrt">Youtube</a></li>
</ul>
<!-- End Social drop-down menu -->
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li><!-- End social -->
<li class="selected"><a href="contact-the-pennsylvania-zombie-response-team">Contact Us</a></li>
</ul>
</div>
<!-- Start contact content coding -->
<div id="contact">
<img src="images/leftlogo.png" alt="left logo" width="440px" style="float: left" />
<?php if($_POST['fred'] != "") {
    echo('<p style="color: #8B2323; font-size: 16px; font-weight: bold;">You may be using a text-only browser or you are a spambot. Sorry this has not been submitted</p>');    
}

else {
    if(isset($_POST['name'])) {

        if(($_POST['name'] == "") or ($_POST['email'] == "") or ($_POST['message'] == "")) {
            echo('<p style="color: #8B2323; font-size: 16px; font-weight: bold;">Please fill in all fields</p>');
}

    else {

        $name = $_POST['name'];
        $email = $_POST['email'];
        $problem = $_POST['problem'];
        $comments = $_POST['comments'];
        $emailmessage = "You have received a submission from your contact form.
Name: $name
Email: $email
Problem: $problem
Comments: $comments
        ";


        //Defining mail settings

        $to = "Enter your email address here";
        $subject = "Form submission sent";
        $headers = "From: $email";


            if(isset($name)) {
                mail($to,$subject,$emailmessage,$headers);

}

}

}

}

?>
    
    <?php if(isset($name)) {
    
    echo('<p style="color: #8B2323; font-size: 15px; font-weight: bold;">Your submission has been sent successfully</p>');
    
    } ?>
    
<form action="" method="post" id="contact">
<ul>
<li>Name:<?php echo $yourname; ?><br />
<input type="text" size="30" name="name" id="name" /></li>
<li><br /></li>
<li>Email:<?php echo $email; ?><br />
<input type="text" size="30" name="email" id="email" /></li>
<li><br /></li>
<li>Problem:<?php echo $problem; ?><br />
<select name="problem">
<option value="website problem">Website Problem</option>
<option value="forum problem">Forum Problem</option>
<option value="misc problem">Miscellaneous</option>
</select></li>
<li><br /></li> 
<li>Comments:<?php echo $comments; ?><br />
<textarea name="comments" id="comments" rows="3" cols="45"></textarea></li>
<li><br /></li>
<li><br /></li>
<li><input type="text" id="fred" name="fred" style="visibility: hidden;"/></li>
<li><input type="submit" name="action" value="Submit" />
&nbsp;
<input type="reset" name="reset" value="Reset" /></li>
</ul>
</form>
</div>
<!-- Start footer coding -->
<div id="footer">
<img src="images/footer.png" alt="footer logo" width="1000px" />
<p><a href="index.html">Home</a> | <a href="about-the-pennsylvania-zombie-response-team">About Us</a> | <a href="contact-the-pennsylvania-zombie-response-team">Contact Us</a><br /></p><p style="color: #FFF">Site by-<a href="http://www.webfinitydesign.com">Webfinity Design</a></p>
</div>
</div>
<script type="text/javascript">var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));</script>
<script type="text/javascript">try { var pageTracker = _gat._getTracker("UA-10934935-1"); pageTracker._trackPageview(); } catch(err) {}</script>
</body>
</html>

Try this

    if(isset($_POST['action'])) {
        echo('<p style="color: #8B2323; font-size: 15px; font-weight: bold;">Your submission has been sent successfully</p>');
    }

that got rid of the initial message, but if you try to submit the form without filling out all the required fields it shows this:
Please fill in all fields

Your submission has been sent successfully

instead of just:
Please fill in all fields

Try this

    if(isset($_POST['action']) && !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])) {
        echo('<p style="color: #8B2323; font-size: 15px; font-weight: bold;">Your submission has been sent successfully</p>');
    }

You know, the form itself works, but when you incorporated the form into your webpage, looks like you added some code at the top.

<?php
$name = htmlspecialchars($_POST[‘yourname’]);
$email = htmlspecialchars($_POST[‘email’]);
$problem = htmlspecialchars($_POST[‘problem’]);
$comments = htmlspecialchars($_POST[‘comments’]);
?>

Now the script sets $name to something the very first time in, so
if(isset($_POST[‘name’]))
will always be true.

The code at the top should really replace the lines just before
$emailmessage = “You have received a submission . . .”

Ideally, you would split the code for the form, putting the PHP at the very top, followed by your original HTML page, followed by the code for the form itself. If you get the page to work and then want to clean it up, post a follow up here. Good luck!

that worked…thanks.

not really sure what you mean…I’m not really knowledged in php.

the php at the very top of the script is the form sanitization I used from my original php script…the php in the script I posted below was from a script someone wrote…the only stuff that is mine is the html code and the form itself, not the php.

Well, if it’s working it’s working, but what’s actually causing the problem is the form sanitization from your original php script. Those lines need to replace the lines below, the ones just before $emailmessage = “You have received a submission . . .” In a roundabout way, however, the fix you applied corrects the error.