【问题标题】:Where is my POST() data going我的 POST() 数据在哪里
【发布时间】:2016-10-25 12:44:59
【问题描述】:

我正在使用 Jquery Ajax 和 PHP 在 html 中开发一个常规表单。

当我通过 php 函数 mail 发送电子邮件时,我收到了下一封带有空白字段的电子邮件,我不知道为什么:

From: 
E-Mail: 
Message:

我正在使用的代码:

-PHP (sendMail.php):

<?php
if($_POST){
    $name    = $_POST['form_name'];
    $email   = $_POST['form_email'];
    $content = $_POST['form_content'];
    $from    = 'Koke Contact Form';
    $to      = 'wheretosendit@gmail.com';
    $subject = 'Someone is contacting';
    $body    = "From: $name\n E-Mail: $email\n Message:\n $content";

    //send email

    if (mail ($to, $subject, $body)) {
                $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }
?>

-HTML

<form method = "post" id = "message" action = "sendMail.php">
    <div class="row">
        <div class = "yourinfo col-md-6">
            <div class = "form-group" id = "nombre">
                <label for = "input-name"> Tu nombre: </label>
                <input id= "form-name" type= "text" class= "form-control gborder" name = "form-name">
            </div>          
            <div class = "form-group " id = "email">
                <label for = "input-email"> Tu email: </label>
                <input id = "form-email" type= "email" class= "form-control gborder" name = "form-email">
            </div>
        </div>                          
        <div class = "form-group col-md-6" id = "comment">
            <label for = "input-content"> Mensaje: </label>
            <textarea id = "form-content" class="form-control gborder" rows = "5" name = "form-content"></textarea>
        </div>
    </div>
    <div class="form-group">
        <div class = "text-center">
            <input id="submit" name="submit" type="submit" value="ENVIAR" class="btn btn-primary">
        </div>
    </div>
</form>

-JQUERY

$('#message').submit(function() {
    if($('#form-name').val() !== "" && $('#form-email').val() !== "" && $('#form-content').val() !== "") {
        var data = {
            name: $('#form-name').val(),
            email: $('#form-email').val(),
            content: $('#form-content').val()
        };
        $.ajax({
            type: 'POST',
            url: 'sendMail.php',
            data: data,
            success: function () {              
                $("html, body").animate({ scrollTop: 0 }, "slow");
                $('#form-content').val("");
                $('#form-name').val("");
                $('#form-email').val("");
            }
        });
    } return false;
});

正如我所说,我正在接收电子邮件,但发件人、电子邮件和消息字段为空。我从 2 天前开始就一直在和这个作斗争。我不知道如何解决它。

感谢您的帮助!

【问题讨论】:

    标签: php jquery ajax email


    【解决方案1】:

    你从 js 数组发送

    var data = {
        name: $('#form-name').val(),
        email: $('#form-email').val(),
        content: $('#form-content').val()
    };
    

    但是在 php 中你尝试访问其他字段

    $name    = $_POST['form_name'];
    $email   = $_POST['form_email'];
    $content = $_POST['form_content'];
    

    Ajax name 和 php 中的 form_name,但也必须是 name

    【讨论】:

    • 比你!!就是这样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多