0

I'm new to Bootstrap (apologies in advance) and what I want to do is quite simplistic. Capture details from FORM in HTML > call javascript on submit > call php to initiate POST > send email to my mail server.

My code to achieve this works in isolation, but when I embed these elements it into my main HTML/JS/PHP environment, I keep getting 'Sorry..mail server not responding' message from my PHP script.

This question is similar in nature to a previous thread but differs as my script always returns an error.

Here is the HTML handling FORM:

    <div class="container"> 
    <div id="contacts">
        <div class="row">   
     <!-- Alignment -->
            <div class="col-sm-offset-3 col-sm-6">
       <!-- Form itself -->
                <form name="sentMessage" class="well" id="contactForm"  novalidate>
                <legend>Contact Us v0.1</legend>
                <div class="control-group">
                    <div class="controls">
                        <input type="text" class="form-control" 
                        placeholder="Name" id="name" required
                        data-validation-required-message="Please enter your name" />
                        <p class="help-block"></p>
                    </div>
                </div>  
                <div class="control-group">
                    <div class="controls">
                        <input type="email" class="form-control" 
                        placeholder="Email" id="email" required
                        data-validation-required-message="Please enter your email" />
                    </div>
                </div>  
                <div class="control-group">
                    <div class="controls">
                        <input type="text" class="form-control" 
                        placeholder="Mobile No (Optional)" id="mobile"/>
                    </div>
                </div>                

               <div class="control-group">
                 <div class="controls">
                 <textarea rows="10" cols="100" class="form-control" 
                       placeholder="Message" id="message" required
               data-validation-required-message="Please enter your message" minlength="5" 
                       data-validation-minlength-message="Min 5 characters" 
                        maxlength="999" style="resize:none"></textarea>
        </div>
               </div>        
         <div id="success"> </div> <!-- For success/fail messages -->
        <button type="submit" class="btn btn-primary pull-right">Send</button><br />
          </form>
    </div>
      </div>
    </div>
   </div>

    <!-- Core JavaScript Files -->
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jqBootstrapValidation.js"></script>
    <script src="js/jquery.easing.min.js"></script> 
    <script src="js/jquery.scrollTo.js"></script>
    <script src="js/wow.min.js"></script>

    <!-- Custom Theme JavaScript -->
    <script src="js/custom.js"></script>
    <script src="js/contact_me.js"></script>

Here is contact_me.js

/*
  Jquery Validation using jqBootstrapValidation
   example is taken from jqBootstrapValidation docs 
  */
$(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 email = $("input#email").val(); 
       var mobile = $("input#mobile").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, email: email, mobile: mobile, 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 to dont.reply@cheerful.com. </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:dont.reply@cheerful.com?Subject=Message_Me from dontreply.com'>dont.reply@cheerful.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('');
  });

Here is contact_me.php

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

$name = $_POST['name'];
$email_address = $_POST['email'];
$mobile = $_POST['mobile'];
$message = $_POST['message'];

// create email body and send it    
$to = 'dont.reply@cheerful.com'; // put your email
$email_subject = "Contact form submitted by:  $name";
$email_body = "Hi, you have received a new message. \n\n".
                  "Here are the details:\n \nName: $name \n ".
                  "Mobile: $mobile \n ".
                  "Email: $email_address\n Message \n $message";
$headers = "From: Contact_Form_v1\n";
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

my index.html is at root level, the contact_me.js is in folder root/js and my contact_me.php is in folder root/bin. Not sure if there is an issue with the php script not being found. My Host provide uses Linux servers.

Any help much appreciated.

h davies
  • 1
  • 1
  • Have you looked at the error logs? Is mail enabled on your host? – Jay Blanchard Feb 12 '15 at 13:21
  • I would suggest creating a test page `without the $_POST` set default variables to test and going direct to the page. This should display any errors and give you some clue of the issue. – NewToJS Feb 12 '15 at 13:22
  • In your JS you want to retrieve the following URL: `url: "./bin/contact_me.php",`. Is this the (relative) URL to `contact_me.php`? – Pepijn van Leeuwen Feb 12 '15 at 13:25
  • Too many questions to be asked. @JayBlanchard *Mornin' Sam* – Funk Forty Niner Feb 12 '15 at 13:29
  • Yup. Too many questions and I am not yet in the proper mindset @Fred-ii- *Mornin' Ralph* – Jay Blanchard Feb 12 '15 at 13:35
  • Check your file paths. I had the same problem a month ago when I too downloaded the same Bootstrap files. Especially `url: "./bin/contact_me.php",` - The error message is coming from one of the `.js` files. – Funk Forty Niner Feb 12 '15 at 13:36
  • Espresso machine's not yet available at this time @JayBlanchard Therefore, mindset will have to be contented by a caffè americano; *I'll just make it a double* ;-) – Funk Forty Niner Feb 12 '15 at 13:39
  • I have a quadruple café au lait sitting in front of me, but to no avail. Perhaps some fresh air will get things rolling @Fred-ii- – Jay Blanchard Feb 12 '15 at 13:44
  • Oh, a double-double; you really want to groove @JayBlanchard yeah... a bit 'o fresh air does the mind/body good ;-) – Funk Forty Niner Feb 12 '15 at 13:45
  • 1
    I am *groovin'* @Fred-ii- ;-) with funk music coming from the speakers. – Jay Blanchard Feb 12 '15 at 13:47
  • Hi Fred, can you expand a little on what you did to resolve a similar problem you had with the same sample files a month or so ago please? – h davies Feb 12 '15 at 17:57

0 Answers0