-1

I have a problem with using mail(). To be more precise, when AJAX code is on - email is sent but with no message (subject etc are there normally).

But when I delete AJAX code, email is sent with no problems. I don't know much about AJAX, I copied js code.

Beside that, can someone explain why is there "return false" at the end of js?

HTML

<form name="contactform" method="post" action="mail.php" class="o4">
<table width="450px">
    <tr>
        <td align="right">
        <label for="name" class="pr-3 font-weight-bold tdsm">Name</label>
        </td>
        <td style="font-size:0;">
            <div class="kii nopm">
                <img src="style/contact.svg">
            </div>
            <input type="text" name="name" id="name" maxlength="50" size="30" placeholder="Name" class="imp1">
        </td>
    </tr>
    <tr>
        <td align="right" class="pr-3 font-weight-bold">
        <label for="email" class="tdsm">Email</label>
        </td>
        <td style="font-size:0;">
            <div class="kii nopm">
                <img src="style/email1.svg">
            </div>
            <input type="text" name="email" id="email" maxlength="50" size="30" placeholder="Email" class="imp1">
        </td>
    </tr>
    <tr>
        <td valign="top" align="right" class="pr-3 pt-2 font-weight-bold kni">
            <label for="msg" class="tdsm">Message</label>
        </td>
        <td valign="top">
            <textarea name="msg_text" id="msg" maxlength="1000" cols="25" rows="6" placeholder="Message" class="imp2"></textarea>
        </td>
    </tr>
    <tr>
        <td></td>
        <td style="text-align:left;">
            <input type="submit" name="submit" value="Send" class="submit_contact">
        </td>
    </tr>
    <tr>
        <td></td>
        <td style="text-align:left;">
            <p class="success pt-3">Success!<p>
        </td>
    </tr>
</table>

JQuery

 $(".submit_contact").click(function() {
    var data = {
        name: $("#name").val(),
        email: $("#email").val(),
        message: $("#msg").val()
    };

    $.ajax({
        type: "POST",
        url: "mail.php",
        data: data,
        success: function(){
            $('.success').css("visibility","visible");
        }
    });
    return false;
});

mail.php

<?php
if($_POST){
$to = "contact@mail.com";
$from = $_POST['email'];
$name = $_POST['name'];
$msg = $_POST['msg_text'];

$subject = "Message from" . $from;
$subject = '=?UTF-8?B?'.base64_encode($subject).'?=';

$headers = "Reply-to: contact@mail.com <contact@mail.com>\r\n";
$headers .= "From: contact@mail.com <contact@mail.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";

$message = $name . " " ."wrote:" . "\n\n" . $msg;

mail($to,$subject,$message,$headers);
}
?>
Czmielu
  • 1
  • 1
  • I think there is nothing to affect `mail()` of Php by `Ajax`. – saddam Oct 15 '18 at 13:01
  • https://stackoverflow.com/questions/40798203/send-mail-mail-to-php-post-method-using-ajax-and-jquery – Ragupathi Oct 15 '18 at 13:04
  • To start, `msg_text` should be `message`. You should use error reporting, you'll get undefined index notices. Your second `$subject =` overwrites the first assignment, you might have meant `$subject .=` You `return false` because you don't want the form actually submitting; AJAX already did that. I think you also could use `preventdefault`. – user3783243 Oct 15 '18 at 13:07

1 Answers1

0

As per your question-

**1.**I have a problem with using mail(). To be more precise, when AJAX code is on - email is sent but with no message (subject etc are there normally).

var data = {
    name: $("#name").val(),
    email: $("#email").val(),
    message: $("#msg").val()
};

In mail.php use $_POST['message'] instead of $_POST['msg_text'];

$to = "contact@mail.com";
$from = $_POST['email'];
$name = $_POST['name'];
$msg = $_POST['message'];

2. When I delete AJAX code, email is sent with no problems. I don't know much about AJAX, I copied js code.

Yes, After delete AJAX Code ,Email will work properly because you are using action="mail.php" in <form> tag and input type="submit" so it directly submit form and action performed by mail.php. so you should remove action from form tag and make input type="button" instead of input type="submit".

<form name="contactform" method="post" class="o4">
<input type="button" name="submit" value="Send" class="submit_contact">
    </td>
Rakhi Prajapati
  • 880
  • 1
  • 12
  • 24