-1

I'm using AJAX contact form but it doesn't send mails. I tried a lot of stuff, but I really don't know what the problem is.

    <!-- START CONTACT FORM -->         
            <div id="contact_form" class="grid_6" style="margin:0;">
                <div class="form-success">
                    <p>Ju falemnderit, mesazhi juaj eshte derguar!</p>
                </div>

                <div class="contact-form"> 
                    <form action="contact-form/send.php" method="post" class="form">    
                        <label>Emri dhe mbiemri</label> 
                        <input class="text" type="text" name="name"> 

                        <label>E-Mail</label> 
                        <input class="text" type="text" name="email"> 

                        <!--     
                        <label>Subject</label> 
                        <input class="text" type="text" name="subject"> 
                         -->

                        <label>Koment</label> 
                        <textarea name="message" rows="8" cols="60"></textarea> 

                        <a href="javascript:;" id="submit" class="button">Dergo Email</a>

                         <div class="loading"></div> 
                    </form> 
                </div>
            </div>
            <!-- END CONTACT FORM -->

and this is contact-form/send.php

<?php

//Your e-mail address goes here: 

$to = "lorentsh@hotmail.com";
//


//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$subject = ($_GET['subject']) ?$_GET['subject'] : $_POST['subject'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;

//Include email validator
    require 'email-validator.php';
    $validator = new EmailAddressValidator();

//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$comment) $errors[count($errors)] = 'Please enter your comment.'; 

$email = strip_tags($email);

if (!$validator->check_email_address($email)) {
    $errors[count($errors)] = 'Invalid email address.'; 
}

//if the errors array is empty, send the mail
if (!$errors) {

    //sender
    $from = $name . ' <' . $email . '>';

    //Structure of the message:
    $subject = 'Message from ' . $name; 
    $message = '
    <!DOCTYPE html>
    <head></head>
    <body>
    <table>
        <tr><td>Name:</td><td>' . $name . '</td></tr>
        <tr><td>Email:</td><td>' . $email . '</td></tr>
        <tr><td>Subject:</td><td>' . $subject . '</td></tr>
        <tr><td>Message:</td><td>' . nl2br($comment) . '</td></tr>
    </table>
    </body>
    </html>';

    //End of the message structure


    //send the mail
    $result = sendmail($to, $subject, $message, $from);

    //if POST was used, display the message straight away
    if ($_POST) {
        if ($result) echo 'Thank you! We have received your message.';
        else echo 'Sorry, unexpected error. Please try again later';

    //else if GET was used, return the boolean value so that 
    //ajax script can react accordingly
    //1 means success, 0 means failed
    } else {
        echo $result;   
    }

//if the errors array has values
} else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="../contact.html">Back</a>';
    exit;
}


//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";

    $result = mail($to,$subject,$message,$headers);

    if ($result) return 1;
    else return 0;
}

?>

Can you tell me wherethe problem is? To index.html or send.php. I need help from you guys.

mhatch
  • 4,441
  • 6
  • 36
  • 62
codeDevil
  • 99
  • 1
  • 1
  • 9
  • they told me this is ajax contact form, but this is all I got, need help – codeDevil Jul 19 '13 at 01:39
  • are you sure your server is configured properly to send mails? you might want to add some debugging statements in your sendmail function and check if control even goes there. – Maximus2012 Jul 19 '13 at 01:43
  • what happens when you change $from = $name . ' <' . $email . '>'; to $from = $name; ? – Maximus2012 Jul 19 '13 at 01:46
  • actually I dont know nothing about servers, cana you guide me about finding a server? – codeDevil Jul 19 '13 at 02:08
  • Hi Lorent, I don't understand why I get a not useful flag on my answer to your question. I tried your code with my suggestion on my server, I have also removed the validator on your php file since it does not exist and you did not provide it with your question, and it worked. Please find screenshots of results below in my edited answer. Thanks. – Morris Miao Jul 19 '13 at 10:42

2 Answers2

0

This isn't AJAX, it's just HTML and PHP.

To make it work, you need to submit the form; just add a button like this inside your form:

<input type="submit" value="Dergo Email">

That should replace the following line:

<a href="javascript:;" id="submit" class="button">Dergo Email</a>
SharkofMirkwood
  • 11,483
  • 2
  • 17
  • 25
  • thanks but I think there ain't no problem in code, but I'm using a free host, and I think I should have an extra php server that sends mail or something like that, I dont have an idea, so if you can help more, I'd appreciate it! Thanks a lot again! – codeDevil Jul 19 '13 at 02:15
  • Regardless of the server, if you don't have a submit button you aren't submitting the form. The link you have there doesn't do anything at all - it's just a link that does nothing. Try updating the code with my suggestion and see what happens. Based on that I can try to help you some more. – SharkofMirkwood Jul 19 '13 at 02:16
  • I tried your suggestion it happens the same, tells me that the message is sent but I'm not receiving it :S – codeDevil Jul 19 '13 at 02:25
  • Oh, I see. In that case, yes, it's probably the server itself that isn't sending the mail. – SharkofMirkwood Jul 19 '13 at 02:31
  • thanks a lot it was the problem at the server, thanks a lot again to everyone – codeDevil Aug 04 '13 at 22:29
0

The problem should be in line

<a href="javascript:;" id="submit" class="button">Dergo Email</a>

You need something like a button which submits your form with

type="submit"

Also please make sure your hosting service supports PHP.

You may also refer to Not receiving the mail.

I tested it and please see the screenshot below. Received

Sent

And the code is attached.

send.php

<?php

//Your e-mail address goes here: 

$to = "lorentsh@hotmail.com";
//


//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$subject = ($_GET['subject']) ?$_GET['subject'] : $_POST['subject'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;


//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$comment) $errors[count($errors)] = 'Please enter your comment.'; 

$email = strip_tags($email);


//if the errors array is empty, send the mail
if (true) {

    //sender
    $from = $name . ' <' . $email . '>';

    //Structure of the message:
    $subject = 'Message from ' . $name; 
    $message = '
    <!DOCTYPE html>
    <head></head>
    <body>
    <table>
        <tr><td>Name:</td><td>' . $name . '</td></tr>
        <tr><td>Email:</td><td>' . $email . '</td></tr>
        <tr><td>Subject:</td><td>' . $subject . '</td></tr>
        <tr><td>Message:</td><td>' . nl2br($comment) . '</td></tr>
    </table>
    </body>
    </html>';

    //End of the message structure


    //send the mail
    $result = sendmail($to, $subject, $message, $from);

    //if POST was used, display the message straight away
    if ($_POST) {
        if ($result) echo 'Thank you! We have received your message.';
        else echo 'Sorry, unexpected error. Please try again later';

    //else if GET was used, return the boolean value so that 
    //ajax script can react accordingly
    //1 means success, 0 means failed
    } else {
        echo $result;   
    }

//if the errors array has values
} else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="../contact.html">Back</a>';
    exit;
}


//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";

    $result = mail($to,$subject,$message,$headers);

    if ($result) return 1;
    else return 0;
}

?>

HTML file

 <!-- START CONTACT FORM -->         
            <div id="contact_form" class="grid_6" style="margin:0;">
                <div class="form-success">
                    <p>Ju falemnderit, mesazhi juaj eshte derguar!</p>
                </div>

                <div class="contact-form"> 
                    <form action="contact-form/send.php" method="post" class="form">    
                        <label>Emri dhe mbiemri</label> 
                        <input class="text" type="text" name="name"> 

                        <label>E-Mail</label> 
                        <input class="text" type="text" name="email"> 

                        <!--     
                        <label>Subject</label> 
                        <input class="text" type="text" name="subject"> 
                         -->

                        <label>Koment</label> 
                        <textarea name="message" rows="8" cols="60"></textarea> 

                        <button class="submit" id="submit" type="submit">Dergo Email</button>

                         <div class="loading"></div> 
                    </form> 
                </div>
            </div>
            <!-- END CONTACT FORM -->

Hence, as what I said, it may because of your free-hosting server doesn't support that.

You may want to test it yourself on http://goo.gl/ynTnE, I will keep it there for a few days.

Community
  • 1
  • 1
Morris Miao
  • 720
  • 1
  • 8
  • 19
  • thanks but I think there ain't no problem in code, but I'm using a free host, and I think I should have an extra php server that sends mail or something like that, I dont have an idea, so if you can help more, I'd appreciate it! Thanks a lot again! – codeDevil Jul 19 '13 at 02:15
  • Hi Lorent, I don't understand why I get a not useful flag on this answer. I tried your code with my suggestion on my server, I have also removed the validator on your php file since it does not exist and you did not provide it with your question, and it worked. Screenshot will be up in a second. – Morris Miao Jul 19 '13 at 02:36
  • thanks a lot it was the problem at the server, thanks a lot again to everyone – – codeDevil Aug 04 '13 at 22:30