How to send by email in php checkbox results?

1

Every time I click the send button, I get the email with the correct data, but after sending, I get an error message on the page: 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 any checkbox in the list and send it, it gives error because I did not fill it in, 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]';

?>
    
asked by anonymous 27.06.2017 / 20:05

2 answers

2

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

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

Or by using the ternary operator:

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

This should be done with all variables.

A more practical alternative is to define checkbox with the same name, adding [] to 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 email:

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

And very careful when using short tags . Read more at:

     

What are the advantages and disadvantages of using

27.06.2017 / 20:31
1

You can simplify the creation of the body of the email The first step is to leave all the checkboxes with the same name and add brackets. When these are sent will be understood as an array by, just make a check if there is something that was 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;
}   

You need to adapt according to 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
    
27.06.2017 / 20:32