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 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?