I am bemused by this problem, I've run the php code through an online tester and there are no errors but when I click Submit without filling out any fields it just refreshes the page, no errors are displayed. When I complete the form and click Submit the same thing happens.

I think the html form etc is all good so now I'm stumped, any help is much appreciated.

<?php include ('./includes/header.php');?>


<?php  

if (isset($_POST ['submit'])) {

    $name = $_POST ['name'];
    $email = $_POST ['email'];
    $phone = $_POST ['phone'];
    $message = $_POST ['message'];


    $name = mysql_real_escape_string($name);
    $email = mysql_real_escape_string($email);
    $phone = mysql_real_escape_string($phone);
    $message = mysql_real_escape_string($message);



    $to = "example@gmail.com";
    $subject = "Web Inquiry";
    $body = "From: $name\n\n E-Mail: $email\n\n Telephone: $phone\n\n Message:\n\n $message";


    $errors = array();


        if (empty($name) || empty($email) || empty($messge)) {



            if (empty($name)) {

                $errors[] = "You must enter a name.";

            } 

            if (strlen($name) > 50) {

                $errors[] = "The maximum amount of characters for name is 50.";
            }

            if (empty($email)) {

                $errors[] = "You must enter an email address.";

            } 

            if (filter_var($register_email, FILTER_VALIDATE_EMAIL)===false) {

                    $errors [] = "Email address invalid.";
                }

            if (strlen($message) < 5) {

                $errors [] = "Your messgae must be at least 25 characters.";
            }




            if (!empty($errors)) {

                    echo $errors;

            } else {

                // send message
                mail($to, $subject, $body);
                header('Location: message-sent.php');
                exit();


                //insert message into database
                }

            }

}


 ?>



<div id="form-wrap">

<form action="" method="post">


        <fieldset>

            <h2>Contact Form</h2>

                    <p>
                        Please complete the contact form and we will respond promptly to your enquiry.

                    </p>

            <label class="red" for=""><em>*</em>&nbsp;required fields.  </label>


            <label><?php  $errors['$name']; ?></label>
            <label class="labelone" for="name"><em>*</em>&nbsp;Name  </label>
            <input name="name" placeholder="Minimum 2 characters" />  

            <label><?php  $errors['$email']; ?></label>
            <label for="email"><em>*</em>&nbsp;Email  </label>
            <input name="email" placeholder="doe@example.com"/>   

            <label><?php  $errors['$phone']; ?></label>
            <label for="phone"> <em> &nbsp;</em>&nbsp;Phone  </label>
            <input name="phone" placeholder="Please include country code if outside the U.S."/>   

            <label><?php  $errors['$message']; ?></label>
            <label for="message"> <em>*</em>&nbsp;Message  </label>
            <textarea name="message">   </textarea>



            <input class="btn" type="submit" value="Submit" />   
            <input class="btn" type="reset" value="Reset" />   

        </fieldset>

</form>

</div>




        <footer><?php include ('./includes/footer.php');?></footer>

    </div><!-- /inner-wrapper -->

</div><!-- /outer-wrapper -->

</body>

Recommended Answers

All 3 Replies

I think the issue you are having is with your isset function in your first if staement and with the lack of IDs on your input elements.

I think this is what your code should look like. I have also added an Else clause to the first if statement this will echo out an error if your form fails during the isset evaluation.

<?php include ('./includes/header.php');?>
<?php  
if (isset($_POST['name'])&& isset($_POST['email'])&& 
    isset($_POST['phone'])&& isset($_POST['message'])) {
    $name = $_POST ['name'];
    $email = $_POST ['email'];
    $phone = $_POST ['phone'];
    $message = $_POST ['message'];
    $name = mysql_real_escape_string($name);
    $email = mysql_real_escape_string($email);
    $phone = mysql_real_escape_string($phone);
    $message = mysql_real_escape_string($message);
    $to = "example@gmail.com";
    $subject = "Web Inquiry";
    $body = "From: $name\n\n E-Mail: $email\n\n Telephone: $phone\n\n Message:\n\n $message";
    $errors = array();
        if (empty($name) || empty($email) || empty($messge)) {
            if (empty($name)) {
                $errors[] = "You must enter a name.";
            } 
            if (strlen($name) > 50) {
                $errors[] = "The maximum amount of characters for name is 50.";
            }
            if (empty($email)) {
                $errors[] = "You must enter an email address.";
            } 
            if (filter_var($register_email, FILTER_VALIDATE_EMAIL)===false) {
                    $errors [] = "Email address invalid.";
                }
            if (strlen($message) < 5) {
                $errors [] = "Your messgae must be at least 25 characters.";
            }
            if (!empty($errors)) {
                    echo $errors;
            } else {
                // send message
                mail($to, $subject, $body);
                header('Location: message-sent.php');
                exit();
                //insert message into database
                }
            }

}else{
    echo'there is an error with the isset statement';
}

 ?>
<div id="form-wrap">
<form action="" method="post">
        <fieldset>
            <h2>Contact Form</h2>
                    <p>
                        Please complete the contact form and we will respond promptly to your enquiry.
                    </p>
            <label class="red" for=""><em>*</em>&nbsp;required fields.  </label>
            <label><?php  $errors['$name']; ?></label>
            <label class="labelone" for="name"><em>*</em>&nbsp;Name  </label>
            <input name="name" id="name" placeholder="Minimum 2 characters" />  
            <label><?php  $errors['$email']; ?></label>
            <label for="email"><em>*</em>&nbsp;Email  </label>
            <input name="email" id="email" placeholder="doe@example.com"/>   
            <label><?php  $errors['$phone']; ?></label>
            <label for="phone"> <em> &nbsp;</em>&nbsp;Phone  </label>
            <input name="phone" id="phone" placeholder="Please include country code if outside the U.S."/>   
            <label><?php  $errors['$message']; ?></label>
            <label for="message"> <em>*</em>&nbsp;Message  </label>
            <textarea name="message" id="message">   </textarea>
            <input class="btn" type="submit" value="Submit" />   
            <input class="btn" type="reset" value="Reset" />   
        </fieldset>
</form>
</div>
        <footer><?php include ('./includes/footer.php');?></footer>
    </div><!-- /inner-wrapper -->
</div><!-- /outer-wrapper -->
</body>

Just spotted I inexplicable omitted name="submit", can't test it as server is down but hopefully this is the reason, but if you spot anything else I'd appreciate it.

Hello Sway, I just tried that but no errors are displayed, for some reson Array os printed on the screen but for the life of me I can't figure out why !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.