I dont know where i am going wrong tried to solove this but cant
Index.php

<form class="form-horizontal" role="form" method="post" action="index.php">
    <div class="form-group">
        <label for="name" class="col-sm-2 control-label">Name</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
            <?php echo "<p class='text-danger'>$errName</p>";?>
        </div>
    </div>
    <div class="form-group">
        <label for="email" class="col-sm-2 control-label">Email</label>
        <div class="col-sm-10">
            <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
            <?php echo "<p class='text-danger'>$errEmail</p>";?>
        </div>
    </div>
    <div class="form-group">
        <label for="contactno" class="col-sm-2 control-label">Contact No</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="contactno" name="contactno" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['contactno']); ?>">
            <?php echo "<p class='text-danger'>$errcontactno</p>";?>
        </div>
    </div>
    <div class="form-group">
        <label for="city" class="col-sm-2 control-label">City</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="city" name="city" placeholder="city" value="<?php echo htmlspecialchars($_POST['city']); ?>">
            <?php echo "<p class='text-danger'>$errcity</p>";?>
        </div>
    </div>
    <div class="form-group">
        <label for="datepicker" class="col-sm-2 control-label">Date</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="datepicker" name="datepicker" placeholder="Date" value="<?php echo htmlspecialchars($_POST['datepicker']); ?>">
            <?php echo "<p class='text-danger'>$errdatepicker</p>";?>
        </div>
    </div>
    <div class="form-group">
        <label for="persons" class="col-sm-2 control-label">No Of Persons</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="persons" name="persons" placeholder="persons" value="<?php echo htmlspecialchars($_POST['persons']); ?>">
            <?php echo "<p class='text-danger'>$errpersons</p>";?>
        </div>
    </div>

    <div class="form-group">
        <label for="message" class="col-sm-2 control-label">Message</label>
        <div class="col-sm-10">
            <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
            <?php echo "<p class='text-danger'>$errMessage</p>";?>
        </div>
    </div>
    <div class="form-group">
        <label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
            <?php echo "<p class='text-danger'>$errHuman</p>";?>
        </div>
    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-2">
            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-2">
            <?php echo $result; ?>    
        </div>
    </div>
</form>

// php code

if ($_POST["submit"]) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $contactno = $_POST['contactno'];
        $city  = $_POST['city'];
        $datepicker = $_POST['datepicker'];
        $persons = $_POST['persons'];
        $message = $_POST['message'];
        $from = 'Demo Contact Form';
        $to = 'imti321@gmail.com';
        $subject = 'Message from Contact';

        $body = "From: $name\n E-Mail: $email\n Ph-No:$contactno\n City:$city\n Date:$datepicker\n No-Of-Persons:$persons\n Message:\n $message ";
         // Check if name has been entered
        if (!$_POST['name']); {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)); {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']); {
            $errMessage = 'Please enter your message';
        }
      //  Check if simple anti-bot test is correct
        if ($human !== 5); {
         $errHuman = 'Your anti-spam is incorrect';
        }

// If there are no errors, send the email
    if(mail($to, $email, $body, $headers)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}

?>

It is giving me
Sorry there was an error sending your message. Please try again later
Thanks in advance
This is a bootstrap php form

The $headers variable is not initialized, if you check the error log you should see a notice:

PHP Notice:  Undefined variable: $headers in ...

for this reason the mail() function will fail, to solve the problem you can remove it (since it's an optional argument), set it to NULL or check the examples in the documentation:

I have already checked even that it still does not work.the problem lies i think somewhere here

 // Check if name has been entered
        if (!$_POST['name']); {
            $errName = 'Please enter your name';
        }
        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)); {
            $errEmail = 'Please enter a valid email address';
        }
        //Check if message has been entered
        if (!$_POST['message']); {
            $errMessage = 'Please enter your message';
        }
      //  Check if simple anti-bot test is correct
        if ($human !== 5); {
         $errHuman = 'Your anti-spam is incorrect';
        }
// If there are no errors, send the email
    if(mail($to, $email, $body, $headers)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}

Well, even though you are setting error messages, you don't check if those error messages are set anywhere, and you don't generate an error based on the error messages. So in your code, it seems to me that it is a waste of lines, time and resources to even set them :p. You should generate/throw errors instead of just setting error messages. The error message you are seeing, is because the mail() function is giving an error, since that's the only thing that's being checked in the if/else.

I've added the error check in the example below:

<?php
if ($_POST["submit"]) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $contactno = $_POST['contactno'];
    $city  = $_POST['city'];
    $datepicker = $_POST['datepicker'];
    $persons = $_POST['persons'];
    $message = $_POST['message'];
    $from = 'Demo Contact Form';
    $to = 'imti321@gmail.com';
    $subject = 'Message from Contact';
    $body = "From: $name\n E-Mail: $email\n Ph-No:$contactno\n City:$city\n Date:$datepicker\n No-Of-Persons:$persons\n Message:\n $message ";

    $errors = array();

    // Check if name has been entered
    if (!$_POST['name']); {
        $errors[] = 'Please enter your name';
    }
    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)); {
        $errors[] = 'Please enter a valid email address';
    }
    //Check if message has been entered
    if (!$_POST['message']); {
        $errors[] = 'Please enter your message';
    }
    //  Check if simple anti-bot test is correct
    if ($human !== 5); {
        $errors[] = 'Your anti-spam is incorrect';
    }

    // Check for errors!
    if ($errors) {
        echo '<p class="errors">';
        foreach ($errors as $error) {
            echo 'Error: ' . $error . '<br>';
        }
        echo '</p>';
    }
    else {
        // If there are no errors, send the email
        if(mail($to, $email, $body)) {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }

}

Now, as for the question why your mail function is giving an error.. Maybe mail settings haven't been configured properly? Are you working on a local server? Is the problem also occuring on an online server that has been configured properly?

Another thing: you are passing the $headers variable to your mail() function, but it seems like you haven't defined it anywhere, so in the example I've left it out for you.

IT is giving errors on name.email.message and $human(anti Spam).
It was already giving same errors even before only difference is this time errors were not shown where i had set them up.Below text boxes.I had also cleared $headers from mail function.I don have problem in normal mail function.I had been doing it for years.My only problem is this time i am doing it in Twitter bootstrap.
Error:Please enter your name
same for message email and Anti spam.
Thanks for reply

sorry i am not on localhost.Other form is working on same host which is not in bootstrap

I have checked the mail goes to my hosting account email but not to gmail

Can anybody tell me why.Code is correct but it does not go to gmail.I found this when i sent mail from my hosting account to my gmail account it returned as permanant spam.Then i changed my email in php code to my hosting account email it worked .I got the mail

Member Avatar for iamthwee

Ah this is an issue with gmail.

For whatever reason I also noticed emails bouncing back when using php_mail() function. I in the end resorted to using gmail's smpt.

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.