游.程 2016-10-25 12:44 采纳率: 0%
浏览 35

我的POST()数据在哪里

I'm developing a regular form in html using Jquery Ajax and PHP.

When i send the email via php function mail, i'm receiving the next email with the empty fields and I don't know why:

From: 
E-Mail: 
Message:

The code I'm using:

-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
 E-Mail: $email
 Message:
 $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;
});

As I said, I'm receiving the email but the From, E-mail and Message fields are empty. I've been fighting with this since 2 days ago.. I don't know how to fix it.

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • Didn"t forge 2016-10-25 12:50
    关注

    You send from js array

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

    But in php you try to access to other fields

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

    Ajax name and in php form_name, but must be name too.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)