dragonfly9527 2014-01-24 21:08 采纳率: 100%
浏览 19
已采纳

空白错误ajax发布到发送电子邮件的php文件

I have an ajax call that sends some data (first name, last name, email) to a php file which is supposed to email me. The data I'm sending exists, I've checked that. When I run it however I am getting a blank error. Am I missing something easy? Any advice?

$.ajax({
    type: 'POST',
    url: 'signup.php',
    data: { first_name: 'first_name', last_name: 'last_name', email_address: 'email_address'},
    contentType: 'application/json; charset=utf-8',
    async: false,
    success: (function() {
        alert("Successful");
    }),
    error: (function(data){

    })
});

In signup.php I have the following:

<?php

// Get User Information From AJAX Post
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];

//Set Email Information
$to = 'example@example.com';
$subject = 'New User';
$message = "New User
$first_name
$last_name
$email_address";
$headers = 'From: example@example.com'. "
";

//Send Email
mail($to, $subject, $message, $headers);

?>
  • 写回答

1条回答 默认 最新

  • dongluanan7163 2014-01-24 22:09
    关注

    Your problems are

    1. You're attempting to post JSON(but not succeeding)
    2. You're attempting to read posted form data though you attempted to post JSON.

    I suggest not posting json, but normal url encoded form data, that way $_POST will be populated with the values sent.

    If you pass an object in the data field it will be encoded for you so you wont have to doing 'first_name='+encodeURIComponent(first_name)+'&last_name='+encodeURIComponent(last_name)+'&email_address'+encodeURIComponent(email_address)

    $.ajax({
        type: 'POST',
        url: '/signup.php',
        data: {'first_name': first_name, 'last_name': last_name, 'email_address': email_address},
        async: false,
        success: (function() {
            alert("Successful");
        }),
        error: (function(ts){
            alert(ts.responseText);
        })
    });
    

    Also in your php you try some string interpolation(putting variables in a string), but you use single quotes, you have to use double quotes.

    $message = "New User
    $first_name
    $last_name
    $email_address";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?