1

NEW - discovered that I have a javascript validating and submitting the form on my website - have added the relevant js

Hope someone can help! I am not a coder but stitch together elements to achieve my ends. Have a web form and am using PHP to submit to my email but it is not happening. Email is not sending and the success or failure text is not appearing - Solutions please.

Website is www.ogracing.eu

HTML

<!-- CONTACT FORM -->
<div class="col-sm-7 col-md-9">
    <!-- IMPORTANT: change the email address at the top of the php/mail.php file to the email address that you want this form to send to -->
    <form class="form-style validate-form clearfix" action="php/mail.php" method="POST" role="form">
        <!-- form left col -->
        <div class="col-md-6">
            <div class="form-group">
                <input class="text-field form-control validate-field required" data-validation-type="string" id="name" placeholder="Name" name="name" type="text">
            </div>  
            <div class="form-group">
                <input class="text-field form-control validate-field required" data-validation-type="email" id="email" placeholder="Email" name="email" type="email">
            </div>
            <div class="form-group">
                <input class="text-field form-control validate-field required" data-validation-type="phone" id="phone" placeholder="Telephone" name="phone" type="tel">
            </div>
        </div><!-- end: form left col -->
        <!-- form right col -->
        <div class="col-md-6">
            <div class="form-group">
                <textarea placeholder="Message..." class="form-control validate-field required" name="message"></textarea>
            </div> 
            <div class="form-group">
                <img src="images/loader-form.GIF" class="form-loader">
                <button type="submit" name="submit" class="btn btn-sm btn-outline-inverse">Submit</button>
            </div> 
            <div class="form-group form-general-error-container">
                <?php echo $result; ?>
            </div>           
        </div><!-- end: form right col -->
    </form>
</div><!-- end: CONTACT FORM -->  

JS this is only the validate and submit portion of the script - functions.js. I have removed the captcha elemnts from the HTML as I did not want to leave it in - this may or may not be causing a validation issue.

function validate_and_submit_forms(b){var a=(b!==undefined&&b.length>0)?b:$("form.validate-form");a.each(function(){var 
c=$(this);c.find(".validate-field").each(function(){$(this).change(function(){$(this).siblings(".alert").fadeOut("fast",function()
{$(this).remove()});if($(this).val().trim()!=""){var e=validate_fields(c,$(this));if(e.length>0){if(e[0]["message"]!==undefined&&e[0]
["message"]!=""&&e[0]["message"]!="success"){var d='<div class="alert">'+e[0]["message"]+"</div>";$(this).after(d);$(this).siblings
(".alert").fadeIn("fast")}}}})});c.find("#form-captcha-refresh").click(function(){reset_captcha(c)});c.submit(function(e)
{e.preventDefault?e.preventDefault():e.returnValue=false;$(this).find(".form-loader").fadeIn("fast");var d=$(this).attr("action");if
(d===undefined&&d==""){return false}$(this).find(".alert").fadeOut("fast",function(){$(this).remove()});$(this).find(".form-general-
error-container").fadeOut("fast",function(){$(this).empty()});var f=false;$(this).find(".validate-field").each(function(){var 
h=validate_fields(c,$(this));if(h.length>0){if(h[0]["message"]!==undefined&&h[0]["message"]!=""&&h[0]["message"]!="success"){var 
g='<div class="alert">'+h[0]["message"]+"</div>";$(this).after(g);$(this).siblings(".alert").fadeIn("fast");f=true}}});if(f==true)
{$(this).find(".form-loader").fadeOut("fast");return false}$.ajax({type:"POST",url:d,data:$(this).serialize
(),dataType:"html",success:function(k){c.find(".form-loader").fadeOut("fast");var l=(k=="success")?true:false;var h=(k=="captcha")?
false:true;var g="";switch(k){case"success":g="Form submitted successfully.";break;case"captcha":g="Incorrect text entered. (Case-
sensitive)";break;case"incomplete":g="Please fill in all required fields.";break;case"error":g="An error occured. Please try again 
later.";break}var j='<div class="alert ';j+=(l==true)?"success":"error";j+='">'+g+"</div>";if(!h){c.find("#form-captcha").parent
(".form-group").append(j);c.find("#form-captcha").siblings(".alert").fadeIn("fast")}else{c.find(".form-general-error-container").html
(j).fadeIn("fast",function(){$(this).delay(10000).fadeOut("fast",function(){$(this).html("")})})}reset_captcha(c);if(l==true){c.find
(".form-control").val("")}},error:function(h){c.find(".form-loader").fadeOut("fast");var g='<div class="alert">An error occured. Please 
try again later.</div>';c.find(".form-general-error-container").html(g).fadeIn("fast")}})})})}function reset_forms(b){if(b!
==undefined&&b.length>0){var a=b;a.find("input").val("");a.find(".alert").remove();a.find(".form-general-error-container").empty
().hide();reset_captcha(b)}}function reset_captcha(b){var a=(b!==undefined&&b.length>0)?b:$("form.validate-form");a.each(function(){var 
e=$(this);var c=e.find("#form-captcha-img");if(c.length>0&&e.is(":visible")){var f=new Date().getTime();c.replaceWith('<img id="form-
captcha-img" src="assets/php/form_captcha/captcha_img.php?t='+f+'" style="display:none">');e.find("#form-captcha").val("");setTimeout
(function(){e.find("#form-captcha-img").show()},500)}})}function validate_fields(d,a){if(d!==undefined&&d.length>0){var b=(a!
==undefined&&a.length>0)?a:d.find(".validate");var c=new Array();b.each(function(){var e=$(this).attr("data-validation-type");var 
h=$(this).hasClass("required");var g=$(this).val().trim();var f=new Array();f.field_object=$(this);f.message="success";if(h==true&&
(g==""||g===null||g===undefined)){f.message="This field is required"}if(e=="string"&&(g!=""&&g!==null&&g!==undefined)){if(g.match(/^[a
-z0-9 .\-]+$/i)==null){f.message="Invalid characters found."}}else{if(e=="email"&&(g!=""&&g!==null&&g!==undefined)){if(g.match(/^
(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]
 +\.)+[a-zA-Z]{2,}))$/)==null){f.message="Please enter a valid email address."}}else{if(e=="phone"&&(g!=""&&g!==null&&g!==undefined))
{if(g.match(/^\(?\+?[\d\(\-\s\)]+$/)==null){f.message="Invalid characters found."}}}}c.push(f)});return c}}function 
contact_form_IE9_placeholder_fix(){var a=$("form");a.each(function(){var b=$(this);$(this).find(".form-control").each(function(){var 
c=$(this).attr("placeholder");if(c!==undefined&&c!=""){$(this).val(c);$(this).focus(function(){if($(this).val()==c){$(this).val
("")}});$(this).blur(function(){if($(this).val()==""){$(this).val(c)}})}})})}function change_bg_check(){return
((jQuery.browser.mobile&&background_settings.change_on_mobile)||(!jQuery.browser.mobile&&background_settings.change_on_nonmobile))?
true:false}function tabs_uniform_height(){var g=$(".tabpanel.uniform-height");for(var f=0,a=g.length;f<a;f++){var e=0;var h=$(g
[f]).find(".tab-pane");for(var d=0,b=h.length;d<b;d++){var c=$(h[d]).attr("style");$(h[d]).css
({position:"absolute",visibility:"hidden",display:"block"});e=($(h[d]).outerHeight(true)>e)?$(h[d]).outerHeight(true):e;$(h[d]).attr
("style",c?c:"")
}h.css({"min-height":e+"px"})}};

PHP

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $from = 'Contact Form'; 
    $to = 'race@ogracing.eu'; 
    $subject = 'Message from website ';
    $body = "From: $name\n E-Mail: $email\n Telephone: $phone\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 telephone number has been entered
    if (!$_POST['phone']) {
        $errMessage = 'Please enter a telephone number';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }

    // If there are no errors, send the email
    if (!$errName && !$errEmail  && !$errPhone && !$errMessage) {
        if (mail($to, $subject, $body, $from)) {
            $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>';
        }
    }
}
?>

Update

I have found some responses from my mail.php - shown below. These are appearing in a window within firebug!!

Response headers

  • Content-Length 0
  • Content-Type text/html
  • Date Fri, 21 Aug 2015 11:14:10 GMT
  • Server Microsoft-IIS/8.5
  • X-Powered-By-Plesk PleskWin
  • x-powered-by ASP.NET

Request headers

  • Accept text/html, /; q=0.01
  • Accept-Encoding gzip, deflate
  • Accept-Language en,en-US;q=0.5 Content-Length 69
  • Content-Type application/x-www-form-urlencoded; charset=UTF-8
  • Cookie _ga=GA1.2.1848665366.1439996290
  • DNT 1 Host www.ogracing.eu
  • Referer http://www.ogracing.eu/
  • User-Agent Mozilla/5.0 (Windows NT 6.0; rv:40.0) Gecko/20100101 Firefox/40.0 X-
  • Requested-With XMLHttpRequest

Post

  • email aidan@pulce.co.uk
  • message more
  • name Aidans analytics
  • phone 56
  • Source name=Aidans+analytics&email=aidan%40pulce.co.uk&phone=56&message=more

HTML Nothing Nada and Nix - guess this is part of the problem

Cookies are being sent

So it appears we are not generating any HTML for the email thus it is not being sent. Any more help and advice is welcome. Thanks.

Aidan
  • 11
  • 3
  • What is the error you get? – DanielV Aug 18 '15 at 08:07
  • 1
    Everything should work. Maybe you have a php error in other part of your mail.php file. You can check your log files to find out it. – Hrant Muradyan Aug 18 '15 at 08:09
  • `if (mail($to, $subject, $body, $from))` May be an issue?? why are you doing an if statement on the mail send? – Joshua Nightingale Aug 18 '15 at 08:10
  • @JoshuaNightingale The mail() function returns true or false depending on if it succeeds, so in this case, for example, the if statement is used to display a success or error message on the page. – harris Aug 18 '15 at 08:18
  • Show the rest of your code, it's hard to say what's wrong with only a small bit of it. – harris Aug 18 '15 at 08:23
  • @ DanielV - I do not know what the error is. The response appears as a white box on the website but there is no visible text. Using firebug I see the detail from the filled in form but it fails to send. – Aidan Aug 19 '15 at 14:34
  • @ Harris the php file is all that I have - I did include a link to the website should you have enough time to look there. – Aidan Aug 19 '15 at 14:35
  • To all of you who have commented - I am humbled that the web society gives so much time and effort for nothing. – Aidan Aug 19 '15 at 14:36
  • There is some of the php file missing - for some reason it is not displaying have tried editing it but it is the first 6 lines – Aidan Aug 19 '15 at 14:56
  • You do not have any form element named `submit`, so you can’t expect `$_POST["submit"]` to be set either. – CBroe Aug 19 '15 at 17:36
  • @CBroe thanks for that info but exactly what do I type and where?? TIA – Aidan Aug 19 '15 at 22:48
  • You need to set a `name` attribute on your submit button. – CBroe Aug 20 '15 at 08:01
  • @CBroe have set the name attribute on the submit button and have updated the file above but it still does not work. – Aidan Aug 21 '15 at 10:32
  • Well then you’ll need to do some more debugging, to find out what your code does (and doesn’t.) Add debug outputs to see which if/else branches are stepped into, control the values of variables via `var_dump`, etc. – CBroe Aug 21 '15 at 11:16
  • You're not actually `echo`ing the `$result` anywhere which is why it's not appearing. – harris Aug 21 '15 at 11:57
  • @harris - there is a line at the end of the php "" this is in a div container in the HTML file – Aidan Aug 21 '15 at 21:46
  • @Aidan Ah so it is, but it won't do much, since when you're loading the main page `$result` is not defined. It only exists in `mail.php` when the form is submitted. – harris Aug 21 '15 at 22:03
  • So @harris what do I do and where please. Considering going back to square one with this project!! – Aidan Aug 22 '15 at 14:38
  • Your JavaScript is a pain to read, but apparently you're submitting the form via Ajax. You need to make `mail.php` echo the responses and then handle those responses in the Ajax call. Here's an example: http://stackoverflow.com/questions/14918462/get-response-from-php-file-using-ajax – harris Aug 25 '15 at 11:34
  • This of course doesn't solve the problem of the mail not being sent, which is something you might need to debug server-side. – harris Aug 25 '15 at 11:35

1 Answers1

0

In the PHP code that you have posted you have this:

if (!$errName && !$errEmail  && !$errPhone && !$errMessage)

But the $errPhone variable is not declared anywhere. Probably in your log file you will find a notice:

Notice: Undefined variable: $errPhone in file_path/file_name.php on line...

And giving that you said "the success or failure text is not appearing" I can say that the error is there, because due to the Notice, your nested if is never executed.

If you don't have a log file try setting error_reporting(E_ALL) at the top of your script and you will see the error directly on your page.

To give you a solution, you must also check the existence of $_POST["phone"] and declare a variable $errPhone (as you did for the other fields name,email and message) to allow the if statement to return true and continue with the execution.

Moreover, as @CBroe said in this comment, you must give a name to your submit button:

<button type="submit" name="submit" class="btn btn-sm btn-outline-inverse">Submit</button>

In this way you'll have $_POST['submit'] available in your PHP script.

Community
  • 1
  • 1
Cliff Burton
  • 3,414
  • 20
  • 33
  • 47
  • Thanks Cliff - will give it a try and decode what you have actually said and get back to you. – Aidan Aug 19 '15 at 14:37
  • You are welcome @Aidan - if you have any problem understanding, I can try to write it in Spanish. I am Italian and our languages are similar **;)** – Cliff Burton Aug 19 '15 at 15:57
  • have tried your suggestions and edited the code above to show changes - still no luck. No need for Spanish - english mother tongue. – Aidan Aug 20 '15 at 15:45
  • @Aidan ahah ok, so I'm not too able to talk in english, sorry XD Anyway, in your updated code, inside the `if(!$_POST["phone"])` you have a typo: You are declaring `$errMessage` instead of `$errPhone`, so the `$errPhone` variable is still not available. – Cliff Burton Aug 20 '15 at 16:08