Edmundo Santos
Edmundo Santos

Reputation: 613

PHP sendmail if elseif

I'm trying all day and can't get this to work.

I have four input type="radio" name="sector" value="value1" but can't get this to work.

function emailtotest($to) {
    if (strip_tags($_POST['sector']) == 'value1') {
        $to .= '[email protected]';
    } elseif (strip_tags($_POST['sector']) == 'value2') {
        $to .= '[email protected]';
    } elseif (strip_tags($_POST['sector']) == 'value3') {
        $to .= '[email protected]';
    } elseif (strip_tags($_POST['sector']) == 'value4') {
        $to .= '[email protected]';
    } else {
        $to .= '[email protected]';
    }
    return $to;
}

I already test the sendmail.php and it's working perfectly if I declare $to = [email protected], but with the radio inputs won't work.

Any help please?

The form code:

    <form id="contactForm" action="sendmail.php" method="post">
    <p>
        <label for="nome">Nome</label><br>
        <input type="text" id="nome" name="nome" required="required" class="input_full">
    </p>
    <p>
        <label for="tel">Telefone</label><br>
        <input type="tel" id="tel" name="tel" required="required" class="input_full">
    </p>
    <p>
        <label for="email">E-mail</label><br>
        <input type="email" id="email" name="email" placeholder="[email protected]" required="required" class="input_full">
    </p>
    <p>
        <label for="radio_1">
            <input type="radio" id="radio_1" name="sector" value="value1">
            Comercial / Marketing
        </label>
        <label for="radio_2">
            <input type="radio" id="radio_2" name="sector" value="value2">
            Produto / Manutenção
        </label>
        <label for="radio_3">
            <input type="radio" id="radio_3" name="sector" value="value3">
            Financeiro
        </label>
        <label for="radio_4">
            <input type="radio" id="radio_4" name="sector" value="value4">
            Administração
        </label>
    </p>
    <p>
        <label for="mensagem">Mensagem</label><br>
        <textarea id="mensagem" name="mensagem" rows="5" placeholder="Escreva aqui sua mensagem." required="required" class="input_full"></textarea>
    </p>
    <p>
        <input type="submit" value="Enviar &rarr;">
    </p>
</form>

Upvotes: 0

Views: 188

Answers (1)

StudioArena
StudioArena

Reputation: 402

Hm HTML part looks ok but try to correct this:

function emailtotest($to) {
if (strip_tags($_POST['sector']) == 'value1') {
    $to = '[email protected]';
} elseif (strip_tags($_POST['sector']) == 'value2') {
    $to = '[email protected]';
} elseif (strip_tags($_POST['sector']) == 'value3') {
    $to = '[email protected]';
} elseif (strip_tags($_POST['sector']) == 'value4') {
    $to = '[email protected]';
} else {
    $to = '[email protected]';
}
return $to;

}

So instead of $to .= "email.."; use just $to = "email"; so without dot before =

Why?

Because if you use .= it means that you add a value to existing value. So example if $to already contains this value: [email protected] and then you use $to.= "[email protected]"; then $to will contains BOTH values and will look like this: [email protected]@email.com Which is not really OK. The other (same) solution is also this:

function emailtotest($to) {
if (strip_tags($_POST['sector']) == 'value1') {
return '[email protected]';
} elseif (strip_tags($_POST['sector']) == 'value2') {
return '[email protected]';
} elseif (strip_tags($_POST['sector']) == 'value3') {
return '[email protected]';
} elseif (strip_tags($_POST['sector']) == 'value4') {
return '[email protected]';
} else {
return '[email protected]';
}
}

That's one thing and another try NOT to using $_POST in functions even it's global. Get the value from $_POST example:

$which = trim(strip_tags($_POST['sector'])); //get your checkbox value

And then call a function and take $which into function like:

$to_email = emailtotest($which); //call a function and take $which - value1, value2...
mail($to_email, "subject", "email txt"); //then send a mail to $to_email

function emailtotest($value){
if($value=='value1'){
return '[email protected]';
}
else if ($value=='value2'){
return '[email protected]'
.....
....
}

Upvotes: 1

Related Questions