0

I'm at the end of my rope and was hoping someone could help me out. I took over for someone and have been trying to have my form send via ajax POST but the mail.php I've been using does not send even though I get a 200 OK. I did a test on conetix and it sent but I must be missing something. Any help is greatly appreciated. Here is the relevant script:

     $("#send").click(function(){

                var valid = '';
                var isr = ' is required!</p>';
                var name = $("#name").val();
                var mail = $("#email").val();
                var messaggio = $("#message").val();

                if (name.length<1) {
                    valid += '<p>*Valid name'+isr;
                }
                if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
                    valid += '<p>*Valid email address'+isr;
                }

and here is my php:

<?php

/// Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$to = '[email protected]';
$subject = 'Contact Us Form Submission';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$headers = 'From: [email protected]' . "\r\n";


?>

Again, any help would really keep me from tearing my eyes out.

                if (valid!='') {
                    $("#response").fadeIn("slow");
                    $("#response").html(valid);

                    $('#send').removeClass('normal').addClass('error-button');
                    $('#send').val('');

                    setTimeout(function() {
                        $('#send').removeClass('error-button').addClass('normal');
                        $('#send').val('send');
                        $("#response").fadeOut("slow"); 
                    }, 3000);                         
                }

                else {
                    var datastr ='name=' + name + '&mail=' + mail + '&messaggio=' + encodeURIComponent(messaggio);
                    setTimeout("send('"+datastr+"')",1000, $('#send').val('wait...'));
                }
                return false;
            });

}(jQuery))

        function send(datastr){
            $.ajax({    
                type: "POST",
                url: "phptest.php",
                data: datastr,
                cache: false,

            success: function(html){
                $('#send').removeClass('normal, error-button').addClass('send-email');
                $('#send').val(''); 
            }

            });
        }
2
  • check for errors, check your console Commented Dec 17, 2015 at 16:47
  • I'm getting no errors on my console. Commented Dec 17, 2015 at 23:14

1 Answer 1

1

Your data string looks like this:

'&mail=' + mail
'&messaggio='

While your PHP is grabbing this:

$_POST['email'];
$_POST['message'];

'mail' != 'email' and 'messaggio' != 'message'

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the information, but can you clarify what you mean? What shold it be grabbing?
I believe you meant I should change the 'email' to mail and 'message' to 'messaggio'. I tried this but it still does not send. Thanks again for taking a look.
@redsoxburns You need to make them match. Either both as email or both as mail. One is setting the variable to a value, the other reads it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.