-1

I found a tutorial for a simple contact form using fancyBox. I was able to apply the form into my website, and then modify the code of the form to meet my needs. But my form is not working. Once I put in all of the information into the form and I press the Send Email button, the button then disappears and I get a "sending..." which is what it supposed to do if all of the fields are "true". But it does not want to do the last step which is to run the php file.

The form also came with the php file necessary for this to work, but it doesn't send it.

Does the php file have to be in an specific place in the website? I have tried to place it from several different locations but to no avail.

<div id="inline">

<form id="contact" name="contact" action="#" method="post">

    <label for="name">Your Name </label>
    <input type="text" id="name" name="name" class="txt">
    <br>
    <label for="email">Your E-mail</label>
    <input type="email" id="email" name="email" class="txt">
    <br>
    <label for="msg">Enter a Message</label>
    <textarea id="msg" name="msg" class="txtarea"></textarea>

    <button id="send">Send E-mail</button>
</form>
</div>


<script type="text/javascript">
function validateEmail(email) { 
    var reg = /^(([^<>()[\]\\.,;:\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,}))$/;
    return reg.test(email);
}

$(document).ready(function() {
    $(".modalbox").fancybox();
    $("#contact").submit(function() { return false; });


    $("#send").on("click", function(){
        var nameval   = $("#name").val();

        var emailval  = $("#email").val();
        var msgval    = $("#msg").val();
        var msglen    = msgval.length;
        var mailvalid = validateEmail(emailval);

        var namelen   = nameval.length;

        if(namelen < 2) {
            $("#name").addClass("error");
        }
        else if(namelen >= 2) {
            $("#name").removeClass("error");
        }

        if(mailvalid == false) {
            $("#email").addClass("error");
        }
        else if(mailvalid == true){
            $("#email").removeClass("error");
        }

        if(msglen < 4) {
            $("#msg").addClass("error");
        }
        else if(msglen >= 4){
            $("#msg").removeClass("error");
        }

        if(mailvalid == true && msglen >= 4 && namelen >= 2) {
            // if both validate we attempt to send the e-mail
            // first we hide the submit btn so the user doesnt click twice
            $("#send").replaceWith("<em>sending...</em>");

            $.ajax({
                type: 'POST',
                url: 'sendmessage.php',
                data: $("#contact").serialize(),
                success: function(data) {
                    if(data == "true") {
                        $("#contact").fadeOut("fast", function(){
                            $(this).before("<p><strong>Success! We will respond to your request as soon as possible.</strong></p>");
                            setTimeout("$.fancybox.close()", 1000);
                        });
                    }
                }
            });
        }
    });
});
</script>

sendmessage.php

<?php
$sendto   = "antiques47@aol.com";
$username = $_POST['name'];
$usermail = $_POST['email'];
$content  = nl2br($_POST['msg']);

$subject  = "More information request";
$headers  = "From: " . strip_tags($usermail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($usermail) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";

$msg  = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$username."</p>\r\n";
$msg .= "<p><strong>Email:</strong> ".$usermail."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n";
$msg .= "</body></html>";


if(@mail($sendto, $subject, $msg, $headers)) {
echo "true";
} else {
echo "false";
}

?>

I tried changing the ACTION on the form to the specific file, but it didn't work either.

I noticed that the ajax part also has the URL to send message.php, I tried to change that so it has a specific directory to the file but that did not work.

jhovapple
  • 31
  • 4
  • What aspect of this is not working? Is the request being sent to the `sendmessage.php` script? Is that script not returning a response? If not are you getting error messages from PHP? Is `sendmessage.php` in the same directory as the page which the form is on? – Mike Brant Jan 24 '14 at 01:44
  • Are you sure that your question isn't, `how do I send an HTML email with php's sendmail` – Ohgodwhy Jan 24 '14 at 01:47
  • I have the sendmessage.php file on the same page as the form and also on the root folder. I have practically placed it everywhere and then call the specific location of the file. – jhovapple Jan 24 '14 at 02:15
  • Are you running a web server? Apache? IIS? – Travesty3 Jan 24 '14 at 02:20

1 Answers1

0

Updated:

You are changing the From: attribute in your mail headers. Some ISPs will block outgoing emails that do that. This could be your problem. Comment out the line:

$headers  = "From: " . strip_tags($usermail) . "\r\n";

and try again.

If that fails, check each step of the AJAX:

.1. In sendmessage.php, change the very top of the file to read:

<?php
    die('Got to here');

.2. Then, back in your ajax code block, amend it (temporarily) to read:

        $.ajax({
            type: 'POST',
            url: 'sendmessage.php',
            data: $("#contact").serialize(),
            success: function(data) {
               alert(data);
            }
        });

This will at least tell you if it is communicating.

.3. Then, echo back the POST items that you received, to ensure there isn't a problem there:

PHP:

$username = $_POST['name'];
$usermail = $_POST['email'];
$content  = nl2br($_POST['msg']);

$out = 'username [' .$username. ']';
$out .= 'usermail [' .$usermail. ']';
$out .= 'content [' .$content. ']';
echo $out;

Again, see what is echo'd out by the ajax success function.

cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • The moment I did the changes the form no longer appears at all. – jhovapple Jan 25 '14 at 01:29
  • Something odd is going on. Echoing out `die('got to here')` should not affect the form... it should affect the output from the AJAX -- unless there is more going on that we understand on our end. You need to do some simple experiments with AJAX. [Try these three](http://stackoverflow.com/questions/17973386/ajax-request-callback-using-jquery/17974843#17974843) -- should take about 5 mins each, and you should learn important stuff about (1) how it works, and (2) how simple it is. – cssyphus Jan 25 '14 at 21:40
  • I finally figured it out! my server requires to specify smtp – jhovapple Jan 31 '14 at 23:53