Data not transfering from Ajax to php to mail

huangapple go评论78阅读模式
英文:

Data not transfering from Ajax to php to mail

问题

I followed this tutorial: https://www.youtube.com/watch?v=DKq1n-awLcw
On how to implement a working contact form in my website.

Everything went fine, until I sent the first test mails and discovered that the mails are indeed sent, but without any information: Empty mail received.

I'm not an expert in jQuery or PHP, but I get how it works, and I can't seem to find why it is not working as intended.

I tried to tweak some variables here and there, but without any results.

Here is the HTML:

<form id="contactform" class="form-element" action="php/contact.php" method="post">
          
    <label class="form-field">
        <span>Name</span>
        <input id="name" class="form-component" type="text" name="name" autocomplete="off" required>
    </label>

    <label class="form-field">
        <span>Email</span>
        <input id="email" class="form-component" type="email" name="email" autocomplete="off" required>
    </label>

    <label class="form-field">
        <span>Subject</span>
        <input id="subject" class="form-component" type="text" name="subjects" autocomplete="off" required>
    </label>

    <label class="form-field">
        <span>Message</span>
        <textarea id="message" class="form-text-area" name="message" autocomplete="off" required></textarea>
    </label>

    <div class="form-field">
        <button class="form-submit" type="button" onclick="sendEmail()" value="Send an email.">Submit</button>
    </div>
</form>

Here's the script:

<script type="text/javascript">
    function sendEmail(){
        var name = $("#name");
        var email = $("#email");
        var subject = $("#subject");
        var message = $("#message");

        if(isNotEmpty(name) && isNotEmpty(email) && isNotEmpty(subject) && isNotEmpty(message)){
            $.ajax({
                url: 'php/contact.php',
                method: 'POST',
                dataType: 'json',
                data:{
                    name: name.val(),
                    email: email.val(),
                    subject: subject.val(),
                    message: message.val() 
                }, success: function(response){
                    $('#contactform')[0].reset();
                    $('.confirm-title').text("Message sent successfully!");
                }
            });
        }
    }

    function isNotEmpty(caller){
        if(caller.val()=="") {
            caller.css('border', '1px solid red');
            return false;
        }
        else {
            caller.css('border', '');
            return true;
        }
    }
</script>

And finally, here is the PHP document:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

if(isset($_POST['name']) && isset($_POST['email'])){
    $name               =   $_POST['name'];
    $email              =   $_POST['email'];
    $subject            =   $_POST['subject'];
    $message            =   $_POST['message'];

    require_once $_SERVER['DOCUMENT_ROOT']."/php/includes/phpmailer/Exception.php";
    require_once $_SERVER['DOCUMENT_ROOT']."/php/includes/phpmailer/PHPMailer.php";
    require_once $_SERVER['DOCUMENT_ROOT']."/php/includes/phpmailer/SMTP.php";

    $mail = new PHPMailer();

    // SMTP Settings
    $mail->isSMTP();
    $mail->Host         =   "secret";
    $mail->SMTPAuth     =   true;
    $mail->Username     =   'secret';
    $mail->Password     =   'secret';
    $mail->Port         =   secret;
    $mail->SMTPSecure   =   "ssl";

    // Email settings
    $mail->isHTML(true);
    $mail->setFrom($email, $name);
    $mail->addAddress("secret");
    $mail->Subject      =   ("$email ($subject)");

    $MailBody           =   "From: ".$name."<br>";
    $MailBody           .=   "Email: ".$email."<br>";
    $MailBody           .=   "Message: ".$message."<br>";

    $mail->Body         =   $MailBody;

    if($mail->send()){
        $status = "success";
        $response = "Email is sent !";
    }
    else {
        $status = "failed";
        $response = "Something is wrong: <br>".$mail->ErrorInfo;
    }

    exit(json_encode(array("status" => $status, "response" => $response)));
}

I do receive the "Message sent successfully!" message on the website.

I already checked the "Transfer data from Ajax to PHP" post, and everything seems to be fine so...

英文:

I followed this tutorial : https://www.youtube.com/watch?v=DKq1n-awLcw
On how to implement a working contact form in my website.

Everything went fine, until I sent the first test mails and discovered that the mails are indeed sent, but without any informations : Empty mail received.

I'm not an expert in jquery or php, but I get how it works, and I can't seem to find why it is not working as intended.

I tried to tweek some variables here and there, but without any results.

Here is the HTML :

&lt;form id=&quot;contactform&quot; class=&quot;form-element&quot; action=&quot;php/contact.php&quot; method=&quot;post&quot;&gt;
          
                    &lt;label class=&quot;form-field&quot;&gt;
                        &lt;span&gt;Name&lt;/span&gt;
                        &lt;input id=&quot;name&quot; class=&quot;form-component&quot; type=&quot;text&quot; name=&quot;name&quot; autocomplete=&quot;off&quot; required&gt;
                    &lt;/label&gt;
            
                    &lt;label class=&quot;form-field&quot;&gt;
                        &lt;span&gt;Email&lt;/span&gt;
                        &lt;input id=&quot;email&quot; class=&quot;form-component&quot; type=&quot;email&quot; name=&quot;email&quot; autocomplete=&quot;off&quot; required&gt;
                    &lt;/label&gt;
            
                    &lt;label class=&quot;form-field&quot;&gt;
                        &lt;span&gt;Subject&lt;/span&gt;
                        &lt;input id=&quot;subject&quot; class=&quot;form-component&quot; type=&quot;text&quot; name=&quot;subjects&quot; autocomplete=&quot;off&quot; required&gt;
                    &lt;/label&gt;
            
                    &lt;label class=&quot;form-field&quot;&gt;
                        &lt;span&gt;Message&lt;/span&gt;
                        &lt;textarea id=&quot;message&quot; class=&quot;form-text-area&quot; name=&quot;message&quot; autocomplete=&quot;off&quot; required&gt;&lt;/textarea&gt;
                    &lt;/label&gt;
            
                    &lt;div class=&quot;form-field&quot;&gt;
                        &lt;button class=&quot;form-submit&quot; type=&quot;button&quot; onclick=&quot;sendEmail()&quot; value=&quot;Send an email.&quot;&gt;Submit&lt;/button&gt;
                    &lt;/div&gt;
                &lt;/form&gt;

Here the script :

&lt;script type=&quot;text/javascript&quot;&gt;
        function sendEmail(){
            var name = $(&quot;#name&quot;);
            var email = $(&quot;#email&quot;);
            var subject = $(&quot;#subject&quot;);
            var message = $(&quot;#message&quot;);

            if(isNotEmpty(name) &amp;&amp; isNotEmpty(email) &amp;&amp; isNotEmpty(subject) &amp;&amp; isNotEmpty(message)){
                $.ajax({
                    url: &#39;php/contact.php&#39;,
                    method: &#39;POST&#39;,
                    dataType: &#39;json&#39;,
                    data:{
                        name: name.val(),
                        email: email.val(),
                        subject: subject.val(),
                        message: message.val() 
                    }, success: function(response){
                        $(&#39;#contactform&#39;)[0].reset();
                        $(&#39;.confirm-title&#39;).text(&quot;Message sent successfully!&quot;);
                    }
                });
            }
        }

        function isNotEmpty(caller){
            if(caller.val()==&quot;&quot;) {
                caller.css(&#39;border&#39;, &#39;1px solid red&#39;);
                return false;
            }
            else {
                caller.css(&#39;border&#39;, &#39;&#39;);
                return true;
            }
        }
    &lt;/script&gt;

And finaly here is the PHP document :

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

if(isset($_POST[&#39;name&#39;]) &amp;&amp;  isset($_POST[&#39;email&#39;])){
    $name               =   $POST[&#39;name&#39;];
    $email              =   $POST[&#39;email&#39;];
    $subject            =   $POST[&#39;subject&#39;];
    $message            =   $POST[&#39;message&#39;];

    require_once $_SERVER[&#39;DOCUMENT_ROOT&#39;].&quot;/php/includes/phpmailer/Exception.php&quot;;
    require_once $_SERVER[&#39;DOCUMENT_ROOT&#39;].&quot;/php/includes/phpmailer/PHPMailer.php&quot;;
    require_once $_SERVER[&#39;DOCUMENT_ROOT&#39;].&quot;/php/includes/phpmailer/SMTP.php&quot;;

    $mail = new PHPMailer();

    // SMTP Settings
    $mail-&gt;isSMTP();
    $mail-&gt;Host         =   &quot;secret&quot;;
    $mail-&gt;SMTPAuth     =   true;
    $mail-&gt;Username     =   &#39;secret&#39;;
    $mail-&gt;Password     =   &#39;secret&#39;;
    $mail-&gt;Port         =   secret;
    $mail-&gt;SMTPSecure   =   &quot;ssl&quot;;

    // Email settings
    $mail-&gt;isHTML(true);
    $mail-&gt;setFrom($email, $name);
    $mail-&gt;addAddress(&quot;secret&quot;);
    $mail-&gt;Subject      =   (&quot;$email ($subject)&quot;);

    $MailBody           =   &quot;From: &quot;.$name.&quot;&lt;br&gt;&quot;;
    $MailBody           .=   &quot;Email: &quot;.$email.&quot;&lt;br&gt;&quot;;
    $MailBody           .=   &quot;Message: &quot;.$message.&quot;&lt;br&gt;&quot;;

    $mail-&gt;Body         =   $MailBody;

    if($mail-&gt;send()){
        $status = &quot;success&quot;;
        $response = &quot;Email is sent !&quot;;
    }
    else {
        $status = &quot;failed&quot;;
        $response = &quot;Something is wrong: &lt;br&gt;&quot;.$mail-&gt;ErrorInfo;
    }

    exit(json_encode(array(&quot;status&quot; =&gt; $status, &quot;response&quot; =&gt; $response)));
}

I do receive the "Message sent successfully!" message on the website.

I already checked the "Transfer data from Ajax to php" post, and everything seems to be fine so..

答案1

得分: 0

你没有正确获取$_POST的值

$name = $POST['name'];
$email = $POST['email'];
$subject = $POST['subject'];
$message = $POST['message'];

请尝试以下方式

$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
英文:

You are not correctly collecting the values ​​of $_POST

$name               =   $POST[&#39;name&#39;];
$email              =   $POST[&#39;email&#39;];
$subject            =   $POST[&#39;subject&#39;];
$message            =   $POST[&#39;message&#39;];

try this way

$name               =   $_POST[&#39;name&#39;];
$email              =   $_POST[&#39;email&#39;];
$subject            =   $_POST[&#39;subject&#39;];
$message            =   $_POST[&#39;message&#39;];

huangapple
  • 本文由 发表于 2023年5月11日 02:06:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定