【问题标题】:send AJAX json array mail with PHP mailer使用 PHP 邮件程序发送 AJAX json 数组邮件
【发布时间】:2017-12-12 22:22:05
【问题描述】:

我正在尝试使用 Json 和 AJAX 发送带有数据数组的 php 表单邮件程序,但由于某种原因,发送的邮件没有数据... (我测试了 jason 字符串并对其进行了验证以确保它包含数据)

这是我的代码:

json:

 function submitFields() {
    form_elements = [];
    $('.dataField').each(function () { 
        frm_name = $(this).attr('name'); 
        frm_qty = $(this).val();
        frm_price = $(this).data('price');
        current_frm_obj = {name:frm_name, qty:frm_qty, price:frm_price};
        form_elements.push(current_frm_obj);
    });

    $.ajax({
        type: "POST", 
        url: "mailer.php",
        data: { result:JSON.stringify(form_elements) },
        success : function(response) {
            console.log(form_elements);
            alert(response);
        }    
    });
}

PHP 邮件程序:

$data = $_POST["result"];
$decoded = json_decode($data);
$errors = '';
$myemail = 'office@studiodeshe.com';

$to = $myemail; 
$email_subject = "Form info";
$email_body = 
$decoded = json_decode($data);
foreach ($decoded as $curr_element) {
    $fieldName = $curr_element->name;
    $fieldQty = $curr_element->qty;
    $fieldPrice = $curr_element->price;

    if ( $fieldQty != 0 ) {
        $fieldName .': <br />' .  $fieldQty;
        if ($fieldPrice != 0) {
            'Qty: ' . $fieldQty . '<br />';
            'Price: ' . $fieldPrice . '<br />';
            'Total: ' . ($fieldPrice*$fieldQty) . '<br /><br />';
            }
    }
}

$headers = "From: studiodeshe <office@studiodeshe.com> \n"; 
$headers .= 'Content-type: text/html; charset=utf-8'; 

mail($to,$email_subject,$email_body,$headers);
header('Location: ty.html');

我猜测我尝试解码邮件中数据的方式存在一些问题,但找不到解决方案...

【问题讨论】:

    标签: php json ajax email


    【解决方案1】:

    好的,我解决了。如果将来有人需要解决方案,那就是:

    json的数据应该在开头解析,并保存到var中,像这样:

    $data = $_POST["result"];
    $decoded = json_decode($data);
    $mailData = "";
    foreach ($decoded as $curr_element) {
        $fieldName = $curr_element->name;
        $fieldQty = $curr_element->qty;
        $fieldPrice = $curr_element->price;
        if ( $fieldQty != 0 ) {
            if ($fieldPrice != 0) {
                $input = $fieldName .': <br />
                Qty: ' .$fieldQty. '<br />
                Price: ' . $fieldPrice . '<br />
                Total: ' . ($fieldPrice*$fieldQty) . 
                '<br /><br />';
                $mailData .= $input;
                }
        } 
    
    
    }
    
    
    $errors = '';
    $myemail = 'studio@studiodeshe.com';//<-----Put Your email address here.
    
    $to = $myemail; 
    $email_subject = "Configurator Lead";
    $email_body = $mailData;
    
    $headers = "From: studiodeshe <office@studiodeshe.com> \n"; 
    $headers .= 'Content-type: text/html; charset=utf-8'; 
    
    mail($to,$email_subject,$email_body,$headers); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-11
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 2021-02-27
      相关资源
      最近更新 更多