[英]How to send a radio button and a checkbox value from a html form?
I'm creating a form with select, input, radio buttons and checkboxes and others. 我正在创建一个包含选择,输入,单选按钮,复选框和其他内容的表单。 Part of the code where is the part of my question is like this:
我的问题所在的部分代码如下所示:
formulario.html 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>
suporte_email.php 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');
}
Basically the variables from the radio button and from the checkbox don't appear when I receive the email. 基本上,当我收到电子邮件时,单选按钮和复选框中的变量不会出现。 I want to receive the selected value from the radio and from the checkbox.
我想从收音机和复选框中接收选定的值。
P:S: Also tried to change where is $garantia
for $radio
and where is $contrato
for $checkbox
. P:S:还尝试更改
$radio
$garantia
和$checkbox
$contrato
在哪里。
Your checkboxes have the same name: 您的复选框具有相同的名称:
<input id="checkbox1" type="checkbox" name="checkbox" value="Sim">
<input id="checkbox2" type="checkbox" name="checkbox" value="Não">
That means the LAST checkbox in the form is the one whose value is set in $_POST. 这意味着表单中的LAST复选框是其值在$ _POST中设置的复选框。
Duplicate names are acceptable for radio buttons, because they're an OR
-type select - only one single radio button in a group can be selected. 单选按钮可以使用重复的名称,因为它们是“
OR
类型选择-组中只能选择一个单选按钮。 But checkboxes are an AND
- you can select multiple checkboxes, which means each one has to have a unique name. 但是复选框是
AND
,您可以选择多个复选框,这意味着每个复选框都必须具有唯一的名称。
Alternatively, you can use PHP's array-name hack: 另外,您可以使用PHP的数组名称hack:
<input id="checkbox2" type="checkbox" name="checkbox[]" value="Não">
^^---
which tells PHP to expect multiple different values for the same field name, and it'll produce an array in $_POST of each of those values. 它告诉PHP为同一个字段名称期望多个不同的值,并且它将在$ _POST中生成每个值的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.