简体   繁体   English

PHP sendmail如果elseif

[英]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. 我有四个input type="radio" name="sector" value="value1"但无法正常工作。

function emailtotest($to) {
    if (strip_tags($_POST['sector']) == 'value1') {
        $to .= 'email1@domain.com';
    } elseif (strip_tags($_POST['sector']) == 'value2') {
        $to .= 'email2@domain.com';
    } elseif (strip_tags($_POST['sector']) == 'value3') {
        $to .= 'email3@domain.com';
    } elseif (strip_tags($_POST['sector']) == 'value4') {
        $to .= 'email4@domain.com';
    } else {
        $to .= 'email5@domain.com';
    }
    return $to;
}

I already test the sendmail.php and it's working perfectly if I declare $to = email@dominio.com , but with the radio inputs won't work. 我已经测试过sendmail.php,如果我声明$to = email@dominio.com ,它可以正常工作,但是使用无线电输入将无法正常工作。

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="nome@exemplo.com" 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>

Hm HTML part looks ok but try to correct this: Hm HTML部分看起来不错,但请尝试纠正此问题:

function emailtotest($to) {
if (strip_tags($_POST['sector']) == 'value1') {
    $to = 'email1@domain.com';
} elseif (strip_tags($_POST['sector']) == 'value2') {
    $to = 'email2@domain.com';
} elseif (strip_tags($_POST['sector']) == 'value3') {
    $to = 'email3@domain.com';
} elseif (strip_tags($_POST['sector']) == 'value4') {
    $to = 'email4@domain.com';
} else {
    $to = 'email5@domain.com';
}
return $to;

} }

So instead of $to .= "email.."; 因此,而不是$ to。=“ email ..”; use just $to = "email"; 仅使用$ 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@email.com and then you use $to.= "email1@email.com"; 例如,如果$ to已包含以下值:email@email.com,然后使用$ to。=“ email1@email.com”; then $to will contains BOTH values and will look like this: email@email.comemail1@email.com Which is not really OK. 那么$ to将包含两个值,并且将如下所示:email @ email.comemail1 @ email.com这不是很确定。 The other (same) solution is also this: 另一个(相同)解决方案也是这样:

function emailtotest($to) {
if (strip_tags($_POST['sector']) == 'value1') {
return 'email1@domain.com';
} elseif (strip_tags($_POST['sector']) == 'value2') {
return 'email2@domain.com';
} elseif (strip_tags($_POST['sector']) == 'value3') {
return 'email3@domain.com';
} elseif (strip_tags($_POST['sector']) == 'value4') {
return 'email4@domain.com';
} else {
return 'email5@domain.com';
}
}

That's one thing and another try NOT to using $_POST in functions even it's global. 这是一回事,另一回事即使是全局的,也不要在函数中使用$ _POST。 Get the value from $_POST example: 从$ _POST示例获取值:

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

And then call a function and take $which into function like: 然后调用一个函数并将$ which放入函数中,例如:

$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 'email1@domain.com';
}
else if ($value=='value2'){
return 'email2@domain.com'
.....
....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM