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

时间:2012-02-07 05:21:44

标签: php

$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

我从http://www.pradipchitrakar.com.np/blog/dynamically-add-remove-textfield.html

下面的链接动态添加,删除和验证jQuery中的表单字段
<?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";
}

?>

3 个答案:

答案 0 :(得分:1)

建议使用foreach循环遍历数组。

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

答案 1 :(得分:0)

使用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 />";

}

答案 2 :(得分:0)

电子邮件消息可以包含不同格式的内容。默认情况下,它们是纯文本。要获得换行符的效果,最好的方法是发送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";                               
}