【问题标题】:Send by e-mail a `parse_str` data array通过电子邮件发送 `parse_str` 数据数组
【发布时间】:2018-04-19 10:57:25
【问题描述】:

我有一个大表格,我serialize 将所有数据发送到 PHP 页面,然后通过电子邮件发送。

我的问题是:

如何在不一一调用的情况下将所有数据发送到电子邮件中?

我只调用了一些数据,例如姓名、电子邮件,但我想在电子邮件中自动添加所有其他数据。

这是我的代码:

    var data = $("#FormWorkspace").serialize();

  //chiamata ajax
    $.ajax({

        type: "POST",
        url: "form-engine.php",
          data: data,
          dataType: "html",
        success: function()
            {
          alert("success" + data);
            },
        error: function()
            {
          alert("no success " + data);
            }
        });

PHP 是:

    session_start();

  $nome = urldecode($_POST['nome']);
  $email = urldecode($_POST['email']);
  $phone = urldecode($_POST['phone']);
  $company = urldecode($_POST['company']);
  $nation = urldecode($_POST['nation']);
  $messaggio = urldecode($_POST['messaggio']);

  $myPostVar = array();
  parse_str($_POST['data'], $myPostVar);




//Send mail

$to         = $tua_email;
$sbj        = "Richiesta Informazioni - $sito_internet";
$msg        = "
<html>
<head>
<style type='text/css'>
body{
    font-family:'Lucida Grande', Arial;
    color:#333;
    font-size:15px;
}
</style>
</head>
<body>

<h1>Richiesta Preventivo</h1>
<br />
<h2>Dati udente</h2>
<p>Nome:    $nome</p>
<p>Email:   $email</p>
<p>Phone:   $phone</p>
<p>Company: $company</p>
<p>Paese:   $nation</p>

<h3>Messaggio</h3>
<p>$messaggio</p>

<h3>Prodotti selezionati</h3>

$myPostVar // How I can write here??


<p>Fine</p>

</body>
</html>
";

$from        = $email;
$headers     = 'MIME-Version: 1.0' . "\n";
$headers    .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers    .= "From: $from";

mail($to,$sbj,$msg,$headers); //Invio mail principale.

【问题讨论】:

  • $_POST["data"] 长什么样子?
  • Ciao Luca,理论上它不应该把所有的表单数据都插入到一个数组中吧?这样我就在电子邮件中返回了一个数组
  • $nome、$email、$phone、$company 和 $nation 的内容是什么?他们应该填写表格中的数据,而不是$_POST["data"] ...
  • 不,我直接从serialize 获取这些数据,但我还有其他数据想插入到电子邮件中,但我不能一一调用。我该怎么办?
  • 我会把这个放在答案中!

标签: javascript php html ajax forms


【解决方案1】:

如评论中所述,您要做的是分别访问键和值:

<?php

$POST = ["QNT-NIA_-_front_desk" => "0", "QNT-KARYA_-_desk_220" => "0", "QNT-KARYA_-_desk_150" => "0", "QNT-KARYA_-_desk_148" => "0", "QNT-KARYA_-_desk_140" => "0", "QNT-KARYA_-_desk_120" => "0", "QNT-KARYA_-_table_74" => "0", "QNT-KARYA_Bridge_-_148" => "0", "QNT-KARYA_Bridge_-_120" => "0", "QNT-UP+_-_riser" => "0", "QNT-LIFTY_-_laptop_riser" => "0", "QNT-SCACCOMATTO_-_shelf/locker" => "0", "QNT-CORAL_-_shelf" => "0", "QNT-ECHO_-_space_divider" => "0", "QNT-KANBAN_BOARD" => "0", "QNT-DAK_-_sofa" => "0", "QNT-FLORA_-_planter" => "0", "nome" => "Nome ", "email" => "marco@email.it", "phone" => "11 111111111", "company" => "PLY", "nation" => "Italy", "messaggio" => "", "fred" => "", "informativa" => "informativa" ];
$articles = [];



$nome = urldecode($POST['nome']);
$email = urldecode($POST['email']);
$phone = urldecode($POST['phone']);
$company = urldecode($POST['company']);
$nation = urldecode($POST['nation']);
$messaggio = urldecode($POST['messaggio']);

$msg = "
<html>
<head>
<style type='text/css'>
body{
    font-family:'Lucida Grande', Arial;
    color:#333;
    font-size:15px;
}
</style>
</head>
<body>

<h1>Richiesta Preventivo</h1>
<br />
<h2>Dati udente</h2>
<p>Nome:    $nome</p>
<p>Email:   $email</p>
<p>Phone:   $phone</p>
<p>Company: $company</p>
<p>Paese:   $nation</p>

<h3>Messaggio</h3>
<p>$messaggio</p>

<h3>Prodotti selezionati</h3>";

// here I begin a table for your products
$msg .= " 
<table style='width:100%' border='1'>
  <tr>
    <th>Nome Prodotto</th>
    <th>value</th> 
  </tr>
";

$unrelated_keys = ["email", "nome", "phone", "company", "nation", "messaggio", "fred", "informativa"]; // these are the keys that are not related to the data you want in the table

//and add the value and name of each prodcut
foreach($POST as $name => $value) {
    if (!array_intersect($unrelated_keys, array($name)))
    {
        $msg .= '<tr>
        <td>'.$name.'</td>
        <td>'.$value.'</td> 
        </tr>';
    } else 
    {
        // do nothing if the field is email/name/company/etc
    }

}

$msg .= "</table>"; // this ends the table

// and this is the rest of your html
$msg .= "<p>Fine</p>

</body>
</html>
";

$from = $email; 
$headers = 'MIME-Version: 1.0' . "\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n"; 
$headers .= "From: $from"; 

$to         = $tua_email;
$sbj        = "Richiesta Informazioni - $sito_internet";

mail($to,$sbj,$msg,$headers);

?>

在此处查看实际操作:http://phpfiddle.org/lite/code/7ay5-wctf

【讨论】:

  • 好吧,这太有趣了!但是如果我写的话不要拿数据:$_POST_data = urldecode($_POST['data'])$_POST_data = $_POST['data'] 我能做些什么来拿走所有的数据?
  • PHP 似乎会自动进行 url 解码,要使用您发布给它的所有数据,只需将所有出现的 $_POST_data 重命名为 $_POST["data"] ?
  • 看没有用 :( 我尝试以这种方式重命名$_POST_data = $_POST['data']; 或这个parse_str($_POST["data"], $articles);
  • 两者都应该工作。您的整个数据字符串的格式是否相同?我不明白为什么这不起作用,您是否显示任何错误(如果没有,您是否将它们关闭)?
  • 是的,我认为你是对的,我看到的每个网站都告诉我,这个需求必须有效!数据不会出现在电子邮件中。我使用的其他数据分离效果很好。我的意思是前:$nome = urldecode($_POST['nome']); 如果我复制你的示例中的字符串效果很好......
猜你喜欢
  • 2020-12-21
  • 2019-01-13
  • 2020-02-24
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多