将变量与foreach分开

时间:2014-11-17 10:21:59

标签: php email

我使用 jemsmailform 作为联系表单时遇到问题。我已经尝试寻找解决方案,我想我已经设法缩小了可能性。我的问题是,一旦发送电子邮件,我的邮件就有"姓名,号码,电子邮件和提交:发送+ 3部分不相关的信息" 。无论如何,我无法删除"提交:发送" 部分邮件。

这是代码:

    $message = "subject: \n\n";
    foreach ($_POST as $key => $val) {
        if (is_array($val)) {
            foreach ($val as $subval) {
              $message .= ucwords($key) . ": " . clean($subval) . "\r\n";
            }
        } else {
            $message .= ucwords($key) . ": " . clean($val) . "\r\n";

        }
    }
    $message .= "\r\n";
    $message .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n";
    $message .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n";
    $message .= 'Points: '.$points;

所以我收到如下邮件:

  

姓名:姓名
  电子邮件:something@someyhing.com
  号码:号码   提交:

     

发送IP:xx.xxx.xx.xx.   浏览器:Mozilla / 5.0(Windows NT 6.3; WOW64;> rv:33.0)Gecko / 20100101 Firefox / 33.0积分:2

如何摆脱"提交:发送" 行?

3 个答案:

答案 0 :(得分:1)

$message = "subject: \n\n";
    foreach ($_POST as $key => $val) {
      if(strtolwer($key) != 'submit'){ // Add this condition
        if (is_array($val)) {
            foreach ($val as $subval) {
              $message .= ucwords($key) . ": " . clean($subval) . "\r\n";
            }
        } else {
            $message .= ucwords($key) . ": " . clean($val) . "\r\n";

        }
      }
    }
    $message .= "\r\n";
    $message .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n";
    $message .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n";
    $message .= 'Points: '.$points;

答案 1 :(得分:1)

在foreach中为此密钥添加一个检查。如果密钥已提交,请跳至下一个密钥。

foreach ($_POST as $key => $val) {
    if ($key === "submit") continue;

    // ... code ...
}

答案 2 :(得分:1)

检查字符串时添加条件:

foreach ($_POST as $key => $val) {
    if (is_array($val)) {
        foreach ($val as $subval) {
            $message .= ucwords($key) . ": " . clean($subval) . "\r\n";
        }
    } else {
        //Add this condition
        if (strtolwer($key) != 'submit') {
            $message .= ucwords($key) . ": " . clean($val) . "\r\n";
        }
    }
}