douyuefu1372 2018-04-19 10:57
浏览 175
已采纳

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

I have a big form, I serialize all the data and send them to a PHP page to then send them by e-mail.

My Question is:

How can I send the email all the data without calling it one by one?

I call just a few data, like name, email, but all the others data I would like to add automatically inside the email.

This is my code:

    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);
            }
        });

and the PHP is:

    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' . "
";
$headers    .= 'Content-type: text/html; charset=iso-8859-1' . "
";
$headers    .= "From: $from";

mail($to,$sbj,$msg,$headers); //Invio mail principale.
  • 写回答

1条回答 默认 最新

  • dongrongdao8902 2018-04-19 11:53
    关注

    As formulated in the Comments, what you'd want to do is access the keys and the values separately:

    <?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' . "
    "; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
    "; 
    $headers .= "From: $from"; 
    
    $to         = $tua_email;
    $sbj        = "Richiesta Informazioni - $sito_internet";
    
    mail($to,$sbj,$msg,$headers);
    
    ?>
    

    See it in action here: http://phpfiddle.org/lite/code/7ay5-wctf

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题