How to email php checkbox results?

Asked

Viewed 307 times

1

Every time I click the send button, I receive the email with the right data, but after sending, I receive on the page an error message: PHP Notice: Undefined index: modulocomercial in E:\Domains\diretivagestao.com\wwwroot\website\pageswebsite\enviar_orcamento.php on line 4 That is, whenever I do not check some checkbox from the list and submit, it gives error by I did not have it filled, but how do I remove this error and choose any combination of checkbox for sending without error?

<div class="campo">
    <label>Interesse(s):</label>
    <label>
        <input type="checkbox" name="modulocomercial" value="comercial" /> Módulo Comercial
    </label>
    <label>
        <input type="checkbox" name="moduloindustria" value="industria" /> Módulo Indústria
    </label>
    <label>
        <input type="checkbox" name="modulovendas" value="vendas" /> Módulo Vendas
    </label>
    <label>
        <input type="checkbox" name="modulooficina" value="oficina" /> Módulo Oficina
    </label>
    <label>
        <input type="checkbox" name="modulofinanceiro" value="financeiro" /> Módulo Financeiro
    </label>
    <label>
        <input type="checkbox" name="modulocontabil" value="contabil" /> Módulo Contábil
    </label>
    <label>
        <input type="checkbox" name="modulograos" value="graos" /> Módulo Grãos
    </label>
    <label>
        <input type="checkbox" name="modulofiscal" value="fiscal" /> Módulo Fiscal
    </label>
    <label>
        <input type="checkbox" name="modulocombustiveis" value="combustiveis" /> Módulo Combustíveis
    </label>
    <label>
        <input type="checkbox" name="modulotodos" value="todos" /> Todos
    </label>
</div>

Shipping form:

<?
      $modulecomercial  =   $_POST['modulocomercial']; //pega os dados que foi digitado no ID modulo.
      $moduleindustria  =   $_POST['moduloindustria'];
      $modulevendas  =   $_POST['modulovendas'];
      $moduleoficina  =   $_POST['modulooficina'];
      $modulefinanceiro  =   $_POST['modulofinanceiro'];
      $modulecontabil  =   $_POST['modulocontabil'];
      $modulegraos  =   $_POST['modulograos']; 
      $modulefiscal  =   $_POST['modulofiscal'];
      $modulecombustiveis  =   $_POST['modulocombustiveis'];
      $moduletodos  =   $_POST['modulotodos'];


      $corpo = "Este é um contato enviado pelo site com as seguintes informações sobre os modulos:\n\n\n";

      $corpo .= "Módulo(s): " . $modulecomercial . ", " . $moduleindustria . ", " . $modulevendas . ", " . $moduleoficina . ", " . $modulefinanceiro . ", " . $modulecontabil . ", " . $modulegraos . ", " . $modulefiscal . ", " . $modulecombustiveis . ", " . $moduletodos . "\n";

     $email_to = '[email protected]';

?>
  • If you do not mark any value is sent to php. In this my suggestions are to exchange all checkboxes for selects where the default value is não. Or leave the checkbox as an array. Instead of picking it individually will use a foreach.

  • Grateful for the contribution, but I did not understand what you said, I am beginner in php, I was already happy that my email is being sent hehehehe, some simple way to remove this error friend?

2 answers

2


Basically, the value of $_POST["X"] will only exist if the field X have a value on the form. In the case of checkbox, there will only be value if you select it and therefore if you do not select all its variables $_POST will not exist. That is, before sending the email you will need to check if there is any value to be sent using the function isset:

if (isset($_POST['modulocomercial'])) {
    $modulecomercial = $_POST['modulocomercial'];
} else {
    $modulecomercial = "";
}

Or using the ternary operator:

$modulecomercial = isset($_POST['modulocomercial']) ? $_POST['modulocomercial'] : "";

This should be done with all variables.


A more practical alternative is to define the checkbox with the same name, adding [] at the end:

<input type="checkbox" name="modulos[]" value="comercial" /> Módulo Comercial
<input type="checkbox" name="modulos[]" value="industria" />
...

And, in PHP, when sending the email, do:

$modulos = isset($_POST["modulos"]) ? implode(", ", $_POST["modulos"]) : "Nenhum módulo selecionado.";

And in the body of the e-mail:

$corpo .= "Módulos: " . $modulos;

And very carefully when using the short tags. Read more on:

What are the advantages and disadvantages of using <? instead of <?php?

  • My THANKS to you Anderson, elucidated my whole question with examples and yet a plus for the beginner here on the short tags, once again, THANK YOU SO MUCH FOR YOUR CONTRIBUTION, great hug!

1

You can simplify the creation of the body of the email the first step is to leave all the checkbox with the same name and add square brackets. When these are sent they will be understood as an array by, just make a check if there is something that has been marked and finally give a implode() to format (transform into string) all elements of the array separated by some delimiter.

1 - Step, change the names:

<label>
    <input type="checkbox" name="modulocomercial" value="comercial" /> Módulo Comercial
</label>
<label>
      <input type="checkbox" name="moduloindustria" value="industria" /> Módulo Indústria
</label>

For something like:

<label>
    <input type="checkbox" name="modulo[]" value="comercial" /> Módulo Comercial
</label>
<label>
    <input type="checkbox" name="modulo[]" value="industria" /> Módulo Indústria
</label>

2 - In php you can use the following code:

$corpo = "Este é um contato enviado pelo site com as seguintes informações sobre os modulos: <br>";
if(!empty($_POST['modulo'])){
    $corpo .= implode(', <br>', $_POST['modulo']);
    echo $corpo;
}   

Need to adapt as per use, in this example the output is something like:

Este é um contato enviado pelo site com as seguintes informações sobre os modulos:
comercial,
industria
  • THANK YOU very much rray, very grateful friend, I understood, I gave a change and gave straight, very grateful for your contribution guy, big hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.