Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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 = 'xxx@xxx.com';
$subject = 'Contact Us Form Submission';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$headers = 'From: xxx@xxx.com' . "\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(''); 
            }

            });
        }

Posted
Comments
Sergey Alexandrovich Kryukov 17-Dec-15 21:22pm    
"Don't talk about rope in JavaScript developer's house"
Miguel de Cervantes Saavedra, El ingenioso hidalgo Don Quijote de la Mancha

—SA

1 solution

200 OK has nothing to do with successful mail sending. There could be hundreds of different reasons for failure. In your PHP code, I did not find a single line where you would even try to send mail. The fragment of code shown suggests that you probably planned to send mail using this function: http://php.net/manual/en/function.mail.php[^].

How would you expect some help if we don't know. But even from this fragment one can see you are doing something wrong. You are sending everything in one datastring. How can you ever expect that this data gets in the $_POST under three different keys? To use this object, you need to pass all data in key-value pairs. For that matter, I have no idea why you decided to use Ajax. A simple form would easily to it without any serious effort. Please study this: http://php.net/manual/en/tutorial.forms.php[^].

Of course, you could do the same with jQuery Ajax, I just don't know why. You can find a lot of code samples around.

But this is still not the worst thing. The worst thing is: if you work in this way, representing data with a string, instead of structured HTML data, and if you are not trying to sanitize it, even a dump malicious artist will turn your server host into a zombie sending spam or something like that. Please see my answer with detailed explanations: unable to send mail , it showing the error in below code .[^].

See also: In what way $('#myelement').valid(); works[^].

What to do? Well, I would through out your code and write it again, this time, understanding what you are doing. The problem is quite trivial. I would start with sending the mail using exact same credentials with some existing mail program. In case of any problems, debug and/or log all data as you go, on both client (it's not really needed if you simply use forms) and server side, and so on…

—SA
 
Share this answer
 
Comments
Member 12214724 17-Dec-15 22:22pm    
Forgive me, but I am relatively new to PHP and as I had mentioned above, I took this project over from someone else and was trying to complete the work. I will look into your suggestions. Thank you for your time.
Sergey Alexandrovich Kryukov 17-Dec-15 22:31pm    
Please do it, instead of continuing to complete it. Please, no need to apologize, everything is quite normal.
Hope you will accept this answer. In all cases, your follow-up questions will be welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900