如何使用动态表单将结果发送到电子邮件


how to send results to email using dynamic form

$ids = explode(',', $_POST['count']);
for($i = 0; $i < count($ids); $i++) {
    $level = $_POST['level' . $ids[$i]];
    $institution = $_POST['institution' . $ids[$i]];
    $board = $_POST['board' . $ids[$i]];
    $division = $_POST['division' . $ids[$i]];
    echo "level: " . $level;
    echo " institution: " . $institution;
    echo " board: " . $board;
    echo " division: " . $division;
    echo "<br />";
}

如何将上述所有内容组合成一个字符串:$message = "code here";?我已经尝试过$message = "$ids";但它向我显示"数组"。它应该将所有结果发送到电子邮件,但如果我使用$message = "level: $level institution: $institution board: $board division: $division";,它只会发送第一行结果

所需结果示例:

// only sends the first line
level: test1 institution: test2 board: test3 division; test4
level: test10 institution: test20 board: test30 division; test40

我从下面的链接动态添加、删除和验证 jQuery 中的表单字段 http://www.pradipchitrakar.com.np/blog/dynamically-add-remove-textfield.html

<?php
// Contact subject
$subject ="new order "; 
// Details
$message="**cant work out code here for all results to display**";
// Mail of sender
$mail_from="$email";
// From 
$header="from: $name <$email>"; 
// Enter your email address
$to ='test@mail.com'; 
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email 
// display message "We've recived your information"
if($send_contact){
  echo "We've recived your contact information";
} else {
  echo "ERROR";
}
?>

建议使用 foreach 循环 thorugh 数组。

http://php.net/manual/en/control-structures.foreach.php

电子邮件可以包含不同格式的内容。默认情况下,它们是纯文本。要获得换行符的效果,最好的方法是发送 HTML 格式的电子邮件。更好的是,您可以将数据格式化为表格。

这是添加到您的代码段 http://php.net/manual/en/function.mail.php HTML 邮件示例:

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';
// subject
$subject = 'My data';
// message -> start creatin a table and a nice header
$message = '
<html>
  <head>
    <title>My Data</title>
  </head>
  <body>
    <p>Nice message here</p>
    <table>
      <tr>
        <th>Level</th><th>Institution</th><th>Board</th><th>Division</th>
      </tr>';
$ids = explode(',', $_POST['count']); 
foreach ($ids as $id) { 
  $level = $_POST['level' . $id]; 
  $institution = $_POST['institution' . $id]; 
  $board = $_POST['board' . $id]; 
  $division = $_POST['division' . $id]; 
  //add each data row to the message as a table row
  $message .= "<tr><td>$level</td><td>$institution</td><td>$board</td><td>$division</td></tr>"; 
}
//close the html table and page
$message .= '
  </table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "'r'n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "'r'n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "'r'n";
$headers .= 'Cc: birthdayarchive@example.com' . "'r'n";
$headers .= 'Bcc: birthdaycheck@example.com' . "'r'n";
// Mail it
$send_contact=mail($to,$subject,$message,$headers);                               
// Check, if message sent to your email                                
if($send_contact){                               
   echo "We've recived your contact information";                               
} else {                               
   echo "ERROR";                               
} 

使用 foreach 在 PHP 中循环访问数组值。

喜欢这个

$ids = explode(',', $_POST['count']);
// if u need to filter ur array again then do before passing values in foreach ..
foreach ($ids as $v) {
    $level = 'level' . $v['$level'];
    $institution = 'institution' . $v['institution'];
    $board = 'board' .  $v['$board'];
    $division = 'division' .  $v[$division];
    // now echo the value
    echo "level: " . $level;
    echo " institution: " . $institution;
    echo " board: " . $board;
    echo " division: " . $division;
    echo "<br />";
}