如何从html表单发送单选按钮和复选框值?

时间:2015-08-11 15:00:17

标签: php html checkbox radio-button html-form

我正在使用选择,输入,单选按钮和复选框等创建表单。 部分代码在哪里是我的问题的一部分是这样的:

formulario.html



<form action="suporte_email.php" method="post" name="formulario" id="formulario" >

    <div class="conjunto7" id="conjunto7">
    		   <label for="garantia">Garantia</label><br>
    		     <div>
    			 <input id="radio1" type="radio" name="radio"  value="Sim">
    			    <label for="radio1"><span><span></span></span> Sim </label>
    			 </div>
    			 
    			 
    		     <div>
    			   <input id="radio2" type="radio" name="radio"  value="Não">
    			    <label for="radio2"><span><span></span></span> Não </label>
    			 </div>
    </div>
    		  
    		  <br><br><br><br>
    		  
    <div class="conjunto8" id="conjunto8">
    		   <label for="contrato">Contrato</label><br>
    		      <div>
    			    <input id="checkbox1" type="checkbox" name="checkbox"  value="Sim">
    			     <label for="checkbox1"><span></span> Sim </label>
    			  </div>
                  
    			  <div>
    			    <input id="checkbox2" type="checkbox" name="checkbox"  value="Não">
    				 <label for="checkbox2"><span></span> Não </label>
    			  </div>
    </div>

</form>
&#13;
&#13;
&#13;

suporte_email.php

//Get Data
$nome = $_POST['nome'];                    
$empresa = $_POST['empresa'];              
$contacto = $_POST['contacto'];            
$email = $_POST['email'];                  
$marca = $_POST['marca'];                   
$other = $_POST['other'];                    
$serial_number = $_POST['serial_number'];     
$garantia = $_POST['garantia'];               
$contrato = $_POST['contrato'];             
$permissoes = $_POST['permissoes'];           
$descricao_avaria = $_POST['descricao_avaria'];
$checkbox = $_POST['checkbox'];
$radio = $_POST['radio'];

   // Parse/Format/Verify Data
   $to      = "teste@gmail.com"; 
   $from    = '';
   $subject = "Formulário de Suporte";

   $email_body = "$crlf De: $nome$crlf Email: $email$crlf Assunto: $subject$crlf$crlf Empresa: $empresa$crlf Contacto: $contacto$crlf Marca: $marca$crlf Outra: $other$crlf Número de Série: $serial_number$crlf Garantia: $garantia$crlf Contrato: $contrato$crlf Tipo de Suporte: $permissoes$crlf$crlf Descrição da Avaria: $descricao_avaria";

   // Setup EMAIL headers, particularly to support UTF-8
   // We set the EMAIL headers here, these will be sent out with your message
   // for the receiving email client to use.
   $headers = 'From: ' . $from  . $crlf .
           'Reply-To: ' . $email  . $crlf .
           'Content-Type: text/plain; charset=UTF-8' .  $crlf .
           'Para: Website'  .  $to . $crlf .
           'X-Mailer: PHP/' . phpversion();


   // Then we pass the headers into our mail function
   mail($to, $subject, $email_body, $headers);
   header('Location: agradecimentos.html');

}

基本上,当我收到电子邮件时,单选按钮和复选框中的变量都不会显示。我想从收音机和复选框中接收所选的值。

P:S:还尝试更改$garantia的{​​{1}}位置和$radio的{​​{1}}位置。

1 个答案:

答案 0 :(得分:2)

您的复选框名称相同:

<input id="checkbox1" type="checkbox" name="checkbox"  value="Sim">
<input id="checkbox2" type="checkbox" name="checkbox"  value="Não">

这意味着表单中的LAST复选框是其值在$ _POST中设置的复选框。

单选按钮可接受重复名称,因为它们是OR - 类型选择 - 只能选择组中的一个单一单选按钮。但是复选框是AND - 您可以选择多个复选框,这意味着每个复选框都必须具有唯一名称。

或者,您可以使用PHP的数组名称hack:

<input id="checkbox2" type="checkbox" name="checkbox[]"  value="Não">
                                                    ^^---

告诉PHP期望同一字段名称有多个不同的值,并且它将在每个值的$ _POST中生成一个数组。