通过电子邮件发送`parse_str`数据数组

时间:2018-04-19 10:57:25

标签: javascript php html ajax forms

我有一个大表单,我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.

1 个答案:

答案 0 :(得分: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