Griphon
Griphon

Reputation: 29

Separating variables from foreach

i have problem for using jemsmailform as a contact form. I've tried looking for a solution and i think i've managed to narrow the possibilites down. My problem is that once the email is sent, my mail has "name, number, email and submit:send + 3 parts of irrelevant info". Anyway, i'm not able to remove the "submit:send" part of the mail.

here is the code:

    $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;

so i'm receiving email like the following:

Name: name
email: [email protected]
number: number
Submit:

send IP: xx.xxx.xx.xx. Browser: Mozilla/5.0 (Windows NT 6.3; WOW64;> rv:33.0) Gecko/20100101 Firefox/33.0 Points: 2

how do i get rid of the "submit: send" line?

Upvotes: 0

Views: 37

Answers (3)

vaso123
vaso123

Reputation: 12391

Just add a condition when you check the string:

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

Upvotes: 1

Jerodev
Jerodev

Reputation: 33186

Add a check in the foreach for this key. If the key is submit, skip to the next key.

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

    // ... code ...
}

Upvotes: 1

Shaunak Shukla
Shaunak Shukla

Reputation: 2347

$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;

Upvotes: 1

Related Questions