PHP&html: emailing form's checkboxes -


i'm trying make simple form sends user input email. don't know php i'm having trouble here. can't make form include checkboxes' results in mail. tried several times can't make work. it's in spanish, sorry that! here code:

contactoformescritorio.php:

<?php $where_form_is = "contacto.html".$_server['server_name'].strrev(strstr(strrev($_server['php_self']),"/"));  mail("my@mail.com","formulario de pedido de copias","form data:  nombre: " . $_post['cd-name'] . "  email: " . $_post['cd-email'] . "  tamanio: " . $_post['tamanio'] . "  acabado: " . $_post['acabado'] . "  incluir en la cotizacion: " .implode(',',$_post['agregados'])."\n" . " foto elegida e información adicional: " . $_post['cd-textarea'] . "  . ");  include("confirm.html");  /*  * procesar el formulario unicamente si el usuario lo envió. en cambio,  * si se accede directamente esta página, redirigir al formulario.  */ if ($_server['request_method'] == "post") {      function check_input_value($input_value) {         // remove spaces of strings (beginning , end)         $input_value = trim($input_value);         // prevent xss         $input_value = htmlspecialchars($input_value);          return $input_value;     }      // obtenemos los valores que el usuario ingresó     $tamanio = $_post['tamanio'];     $acabado = $_post['acabado'];     $tamanio = check_input_value($tamanio);     $acabado = check_input_value($acabado);      if (empty($tamanio) || (empty($acabado)) || (empty($agregados))) {         echo "error: sin completar";         exit;     }      echo $tamanio . "<br />";     echo $acabado . "<br />";      // muestra los checkbox seleccionados por el usuario     if (!empty($_post['agregados'])) {         foreach ($_post['agregados'] $agregados) {             echo $agregados . "<br />";         }     }  } else {     header("location: formulario.php"); } ?> 

a friend helped me form may little messy copied , pasted parts.

contacto.html

<form class="cd-form floating-labels" name="htmlform" method="post" action="contactoformescritorio.php">      <fieldset>         <legend>información personal</legend>         <div class="error-message">             <p>por favor ingresa un email valido</p>         </div>         <div class="icon">             <label class="cd-label" name="cd-name" for="cd-name">nombre</label>             <input class="user" type="text" name="cd-name" id="cd-name" required>         </div>          <div class="icon">             <label class="cd-label" name="cd-email" for="cd-email">email</label>             <input class="email error" type="email" name="cd-email" id="cd-email" required>         </div>     </fieldset>     <fieldset>         <legend>informacion de la impresion</legend>         <div>             <h4>tamaño</h4>             <p class="cd-select icon">                 <select class="size" name="tamanio" id="cd-size">                     <option value="0">seleccionar tamaño</option>                     <option value="1">impresion 20x30</option>                     <option value="2">iman de 6 fotos 5x5</option>                     <option value="3">tamaño 3</option>                 </select>             </p>         </div>          <div>             <h4>acabado</h4>             <ul class="cd-form-list">                 <li>                     <input type="radio" name="acabado" value="mate" id="mate" checked="checked"/><label for="mate">mate</label>                 </li>                 <li>                     <input type="radio" name="acabado" value="brillo" id="brillo"/><label for="brillo">brillo</label>                 </li>             </ul>         </div>         <div>             <h4>agregar la cotización</h4>             <ul class="cd-form-list">                 <li><input type="checkbox" name="agregados[]" value="marco" id="marco"><label for="marco">marco</label></li>                  <li><input type="checkbox" name="agregados[]" value="envio" id="envio"><label for="envio">envio (indicar direccion)</label></li>             </ul>         </div>         <div class="icon">             <label class="cd-label" for="cd-textarea">foto elegida e información adicional</label>             <textarea class="message" name="cd-textarea" id="cd-textarea" required></textarea>         </div>         <div>             <input type="submit" value="enviar mensaje">         </div>     </fieldset> </form> 

your checkboxes both of name agregados[], makes them array. handle them via implode, later foreach.

before mail, run foreach this

if (!empty($_post['agregados'])) {     foreach ($_post['agregados'] $key=>$value) {         if ($key > 0) {             $agregados .= ", $value";         } else {             $agregados .= "$value";         }     } } else {     $agregados .= "(nothing selected)"; } 

this set variable $agregados values selected checkboxes, , if nothing selected, display such message instead.

then, in mail, replace line handles checkboxes variables.

replace:

 incluir en la cotizacion: " .implode(',',$_post['agregados'])."\n" . " 

with

$agregados 

also, please know mail sent regardless when enter contactoformescritorio.php, there no checking if form has been sent it. check performing (if ($_server['request_method'] == "post")) comes after that.


as final note, not 1 that's related question directly, select has no require-attribute, php script exits if nothing selected (where checking of 1 of 3 statements empty). put required attribute in select, such this

<select class="size" name="tamanio" id="cd-size" required> 

then seleccionar tamaño can set line this

<option value="" selected style="display:none;">seleccionar tamaño</option> 

this make form more "user-friendly", , not make user fill in form on again if forget select dropdown. tip, nothing have do.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -