0

I have this email code from a website template that I want to use for my site. The problem is that when I click on the "Send message" button, the form simply refreshes and nothing is sent. If there is any text data in the fields, it simply disappears.
I have looked at foums for a solution but to no avail. What am I doing wrong?

Form Html

<form name="sentMessage" id="contactForm" method='post' novalidate action="public_html/bin/contact_me.php">
   <div class="control-group form-group">
      <div class="controls">
         <label>Full Name</label>
         <input type="text" class="form-control" name="name" id="name" placeholder="Full Name" required data-validation-required-message="Please enter your name.">
         <p class="help-block"></p>
      </div>
   </div>
   <div class="row">
      <div class="col-md-6">
         <div class="form-group">
            <label>Email Address</label>
            <input type="email" class="form-control" name="email" id="email" placeholder="Email Address" required data-validation-required-message="Please enter your email address.">
         </div>
      </div>
      <div class="col-md-6">
         <div class="form-group">
            <label>Phone Number</label>
            <input type="tel" class="form-control" name="phone" id="phone" placeholder="Phone Number" required data-validation-required-message="Please enter your phone number.">
         </div>
      </div>
   </div>
   <div class="form-group">
      <label>Subject</label>
      <input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" required data-validation-required-message="Please enter your phone number.">
   </div>
   <div class="form-group">
      <label>Message</label>
      <textarea rows="10" cols="100" class="form-control" name="message" id="message" placeholder="Please Enter Your Message Here..." required data-validation-required-message="Please enter your message" maxlength="999" style="height:150px;"></textarea>
   </div>
   <div id="success"></div>
   <div>
      <button type="submit" class="btn btn-two">Send message</button>                          
      <p><br/></p>
   </div>
</form>

PHP Code

<?php
// check if fields passed are empty
if(empty($_POST['name'])        ||
   empty($_POST['phone'])       ||
   empty($_POST['email'])       ||
   empty($_POST['subject'])         ||
   empty($_POST['message'])         ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

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

// create email body and send it    
$to = 'info@mywebsite.com'; // PUT YOUR EMAIL ADDRESS HERE
$email_subject = "$subject"; // EDIT THE EMAIL SUBJECT LINE HERE
$email_body = "You have received a new message from your website's contact form.\n\n"."Here are the details:\n\nName: $name\n\nPhone: $phone\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: noreply@your-domain.com\n";
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

Js

$(function() {

    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // something to have when submit produces an error ?
            // Not decided if I need it yet
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var name = $("input#name").val();
            var phone = $("input#phone").val();
            var email = $("input#email").val();
            var subject = $("input#subject").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "bin/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    subject: subject,
                    message: message
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + " it seems that my mail server is not responding...</strong> Could you please email me directly to <a href='mailto:me@example.com?Subject=Message_Me from myprogrammingblog.com;>me@example.com</a> ? Sorry for the inconvenience!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            })
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

How do I solve this?

2
Contributors
8
Replies
67
Views
2 Years
Discussion Span
Last Post by broj1
0

The code is basically OK except for the return statements. Why do you use them? Are the PHP scripts being called using the include/require statements? And make sure your first script has the .php extension (not the .html).

0

Thanks for the reply broj1,

I got this code from a website template download. The edits I made were to include a subject field that was not present.
You mean that the contact.html should have a .php extension?
The PHP script, contact_me.php is not being called using the include/require statements. I have attached the entire contact.html

Edited by kkjaykamau: Attachment

Attachments
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="eohseal">
        <meta name="author" content="eohseal">       
        <title>EOH SEAL | Contact</title>
        <link rel="icon" type="image/png" href="assets/images/favicon.png">
        <link rel="stylesheet" media="screen" href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,700">
        <link rel="stylesheet" href="assets/css/bootstrap.min.css">
        <link rel="stylesheet" href="assets/css/font-awesome.min.css">
        <!-- Custom styles for our template -->
        <link rel="stylesheet" href="assets/css/bootstrap-theme.css" media="screen">
        <link rel="stylesheet" href="assets/css/style.css">     
        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
        <script src="assets/js/html5shiv.js"></script>
        <script src="assets/js/respond.min.js"></script>
        <![endif]-->
    </head>

    <body>
        <!-- Fixed navbar -->
        <div id="header-top">       
            <!--  HEADER TOP -->
            <div class="container">
                <div class="row">
                    <div class="col-md-6"> 
                        <div class="text">
                            <p>Telephone : +254 711 271970 / +254 774 349951 / +254 20 3970000</p>
                        </div>      
                    </div><!-- end -->
                    <div class="col-md-6">              	
                        <div class="social text-center pull-right">
                            <img src="assets/images/flags/southafrica.png" alt="southafrica">
                            <img src="assets/images/flags/kenya.png" alt="kenya">
                            <img src="assets/images/flags/uganda.png" alt="uganda">
                            <img src="assets/images/flags/tanzania.png" alt="tanzania">
                            <img src="assets/images/flags/ethiopia.png" alt="ethiopia">
                        </div>
                    </div><!-- end -->
                </div><!-- end .row -->
            </div> 
        </div>
        <div class="navbar navbar-inverse">
            <div class="container">
                <div class="navbar-header">
                    <!-- Button for smallest screens -->
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
                    <a class="navbar-brand" href="index.html">
                        <img src="assets/images/logo.png" alt="logo"></a>
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav pull-right mainNav">
                        <li class="active"><a href="index.html">Home</a></li>
                        <li><a href="about.html">About</a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Services<b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <li><a href="allinone.html">SAP Business All In One</a></li>
                                <li><a href="businessobjects.html">SAP Business Objects</a></li>
                                <li><a href="businessone.html">SAP Business One</a></li>
                                <li><a href="mobileplatform.html">SAP Mobile Platform</a></li>
                                <li><a href="businessintelligence.html">SAP Business Intelligence</a></li>
                            </ul>
                        </li>
                        <li><a href="whyus.html">Why Us</a></li>
                        <li><a href="portfolio.html">Portfolio</a></li>   
                        <li><a href="http://www.eohsapservices.co.za/about-sap-services/">EOH SAP Services</a></li>  
                        <li><a href="contact.html">Contact</a></li>
                    </ul>
                </div>
                <!--/.nav-collapse -->
            </div>
        </div>
        <!-- /.navbar -->

        <header id="head" class="secondary">
            <div class="container">
                <div class="row">
                    <div class="col-sm-8">
                        <h1>Contact us</h1>
                    </div>
                </div>
            </div>
        </header>

        <!-- container -->
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <h3 class="section-title">Your Message</h3>
                    <p>
                        We invite you join our growing portfolio of loyal and satisfied customers. We will be glad to hear 
                        from you. Contact us now: 
                    </p>

                    <form name="sentMessage" id="contactForm" novalidate>                    
                        <div class="control-group form-group">
                            <div class="controls">
                                <label>Full Name</label>
                                <input type="text" class="form-control" id="name" placeholder="Full Name" required data-validation-required-message="Please enter your name.">
                                <p class="help-block"></p>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Email Address</label>
                                    <input type="email" class="form-control" id="email" placeholder="Email Address" required data-validation-required-message="Please enter your email address.">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Phone Number</label>
                                    <input type="tel" class="form-control" id="phone" placeholder="Phone Number" required data-validation-required-message="Please enter your phone number.">
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label>Subject</label>
                            <input type="text" class="form-control" id="subject" placeholder="Subject" required data-validation-required-message="Please enter your phone number.">
                        </div>
                        <div class="form-group">
                            <label>Message</label>
                            <textarea rows="10" cols="100" class="form-control" id="message" placeholder="Please Enter Your Message Here..." required data-validation-required-message="Please enter your message" maxlength="999" style="height:150px;"></textarea>
                        </div>
                        <div id="success"></div>                      
                        <div>
                            <button type="submit" class="btn btn-two">Send message</button>                          
                            <p><br/></p>
                        </div>                    
                    </form>
                </div>
                <div class="col-md-6">
                    <div class="row">
                        <div class="col-md-6">
                            <h3 class="section-title">Office Address</h3>
                            <div class="contact-info">
                                <h5>Address</h5>
                                <p>Safaricom HQ 3,  1st floor (formerly Essar House), Muguga Green, Westlands.</p>
                                <p>P. O. Box 10496 00100, Nairobi, Kenya.</p>

                                <h5>Email</h5>
                                <p>info@eohseal.com</p>

                                <h5>Phone</h5>
                                <p>+254 774 349951 or</p>
                                <p>+254 20 3970000 or</p>
                                <p>+254 755 397000</p>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <h3 class="section-title">Timings</h3>
                            <div class="contact-info">
                                <h5>Monday - Friday</h5>
                                <p>07:30 AM - 5:00 PM</p>

                                <h5>Saturday</h5>
                                <p>Closed</p>

                                <h5>Sunday</h5>
                                <p>Closed</p>
                            </div>
                        </div>
                    </div>
                    <h3 class="section-title">Directions</h3>
                    <div id="map-canvas">
                    </div>
                </div>
            </div>
        </div>
        <!-- /container -->

        <footer id="footer" class="top-margin"> 
            <div class="footer2">
                <div class="container">
                    <div class="row">

                        <div class="col-md-6 panel">
                            <div class="panel-body">
                                <p class="simplenav">
                                    <a href="index.html">Home</a> | 
                                    <a href="about.html">About</a> |
                                    <a href="allinone.html">Services</a> |
                                    <a href="whyus.html">Why Us</a> |
                                    <a href="portfolio.html">Portfolio</a> |
                                    <a href="http://www.eohsapservices.co.za/about-sap-services/">EOH SAP Services</a> |
                                    <a href="contact.html">Contact</a>
                                </p>
                            </div>
                        </div
0

Yes, any script that has php code should have a php extension. And remove the return statements.

0

@Broj1. The only script that has php code is contact_me.php (php extension) and not the contact.html that contains code for the form.

0

Sorry, I was wrong. The form page does not need to have a php extension since there is no php code in it. All the code is in the contact_me-php script. There must be an error somewhere else. I will have a chance to look at it tomorrow. Sorry again for misleading you (it was not on purpose).

Edited by broj1

0

The form in the code for contact.html you posted above lacks the action attribute. It should be:

<form name="sentMessage" id="contactForm" novalidate action="contact_me.php">

If I add the action atribute, the form correctly posts to the contact_me.php script but I get No arguments Provided! error message (even I filled in all fields). This is because your form fields lack name attributes. Just add them to every form field and make it same as the ID attributes:

<input type="text" class="form-control" id="name" name="name" placeholder="Full Name" required data-validation-required-message="Please enter your name.">
...

In your previous post these errors were not present and everything worked.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.