Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

356
Visualizações
Sending an email with PHP/Javascript submit button

I will try and be brief. When a user selects items in a checkbox, I want them to be able to press a submit button and it will send an email of the ingredients to a static email address. I am trying to store the values of the check boxes in an array called finalOrder, and have that array in the contents of the email.

I am fairly new to both languages, can I use the submit button to call the PHP code? Is the PHP written correctly? I am not too sure how to go about this.

Here is what I have so far.

<div>
 <input type="checkbox" name="foodType" data-name="Chicken Thighs">  <label 
 for="chickenThighs">Chicken Thighs</label>
 <input type="checkbox" name="foodType" data-name="Chicken Breast">  <label 
 for="chickenBreast">Chicken Breast</label>
 <input type="checkbox" name="foodType" data-name="Chicken Leg">  <label 
 for="chickenLeg">Chicken Leg</label>
 </div>


 <div id='results'>

 </div>

 <script type="text/javascript">
 var finalOrder = [];

 document.querySelectorAll('input[name=foodType]').forEach(elem => {
    elem.addEventListener('change', updateList);
  });

  function buildList ()
  {
  let list = '<ul>';

  finalOrder.forEach(str => {
  list += "<li>" + str + '</li>';   
  });

 list += '</ul>';

 return list;
     }


function updateList ()
{
finalOrder = []; // Reset this array so that we can populate it with the new checkbox answers.

document.querySelectorAll('input[name=foodType]').forEach(v => {
    if (v.checked) {
    finalOrder.push(v.dataset.name);
}
});

document.querySelector('#results').innerHTML = buildList();
}


</script>
<input type="submit" value="Submit">

<?php
// the message
$msg = finalOrder;

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap(finalOrder($msg,70));

// send email
mail("my@email.com","New Order",$msg);
?>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is one thing, missing so far.

  1. A form tags to help post the data for the server for processing by PHP language so the file has to be saved as a PHP file and HTML. In the snippet I saved it as thisFile.php.

You can also consider the approach stated by @jaaba

 <div>
    <form method="POST" action="thisFile.php"> 
     <input type="checkbox" name="foodType" data-name="Chicken Thighs">  <label 
     for="chickenThighs">Chicken Thighs</label>
     <input type="checkbox" name="foodType" data-name="Chicken Breast">  <label 
     for="chickenBreast">Chicken Breast</label>
     <input type="checkbox" name="foodType" data-name="Chicken Leg">  <label 
     for="chickenLeg">Chicken Leg</label>
     </div>
    <input type="submit" value="Submit">
    </form>

     <div id='results'>

     </div>

     <script type="text/javascript">
     var finalOrder = [];

     document.querySelectorAll('input[name=foodType]').forEach(elem => {
        elem.addEventListener('change', updateList);
      });

      function buildList ()
      {
      let list = '<ul>';

      finalOrder.forEach(str => {
      list += "<li>" + str + '</li>';   
      });

     list += '</ul>';

     return list;
         }


    function updateList ()
    {
    finalOrder = []; // Reset this array so that we can populate it with the new checkbox answers.

    document.querySelectorAll('input[name=foodType]').forEach(v => {
        if (v.checked) {
        finalOrder.push(v.dataset.name);
    }
    });

    document.querySelector('#results').innerHTML = buildList();
    }


    </script>


    <?php
    // the message
    $msg = finalOrder;

    // use wordwrap() if lines are longer than 70 characters
    $msg = wordwrap(finalOrder($msg,70));

    // send email
    mail("my@email.com","New Order",$msg);
    ?>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda