我正在尝试向表单添加复选框并通过电子邮件获得确认。我正在使用 POST 方法。 所有其他字段都可以正常工作,但复选框不...
谢谢!
<form class="form-contact" id="formulario" name="contacto" method="post" action="paraenviar.php">
<label for="nombre">Nombre</label>
<input name="nombre" id="nombre" type="text" required class="fill" size="20" /><br />
<label for="empresa">Empresa</label>
<input name="empresa" id="empresa" type="text" required class="fill" size="20" /><br />
<label for="email">eMail</label>
<input name="email" id="email" type="text" required class="fill" size="20" /><br />
<label for="mensaje">Mensaje</label>
<textarea name="mensaje" id="mensaje" cols="26" rows="3" required class="textarea"></textarea><br />
<input type="checkbox" id="protecciondatos" name="protecciondatos" value="protecciondatos" required>
<label for="proteccion-datos">He leído y acepto la <a href="#">política de protección de datos</a> </label>
<br><br>
<input class="sendbutton" name="insert" type="submit" value="Enviar"/>
</form>
<?php
$nombre = $_POST['nombre'];
$empresa = $_POST['empresa'];
$email = $_POST['email'];
$mensaje = "NOMBRE: " . $nombre ." \r\n";
$mensaje .= "EMPRESA: " . $empresa . " \r\n";
$mensaje .= "E-MAIL: " . $email . " \r\n";
$mensaje .= "MENSAJE: " . $_POST['mensaje'] . " \r\n";
$mensaje .= "FECHA DEL ENVÍO: " . date('d/m/Y', time());
$para = 'xxxx@xxx';
$asunto = 'Blablabla';
mail($para, $asunto, utf8_decode($mensaje));
?>
首先,正如大卫评论的那样,您实际上并没有从表格中获得价值。
关于你的问题,你的 php 代码将是
<?php
$nombre = $_POST['nombre'];
$empresa = $_POST['empresa'];
$email = $_POST['email'];
$protecciondatos=$_POST['protecciondatos'];
$mensaje = "NOMBRE: " . $nombre ." \r\n";
$mensaje .= "EMPRESA: " . $empresa . " \r\n";
$mensaje .= "E-MAIL: " . $email . " \r\n";
$mensaje .= "MENSAJE: " . $_POST['mensaje'] . " \r\n";
$mensaje .= "FECHA DEL ENVÍO: " . date('d/m/Y', time());
$para = 'xxxx@xxx';
$asunto = 'Blablabla';
if($protecciondatos=="protecciondatos"){
mail($para, $asunto, utf8_decode($mensaje));
}
else{
//error message, or whatever
}
?>
更好的方法是将所有内容都放在 if 语句中,如下所示:
<?php
$protecciondatos=$_POST['protecciondatos'];
if($protecciondatos=="protecciondatos"){
$nombre = $_POST['nombre'];
$empresa = $_POST['empresa'];
$email = $_POST['email'];
$mensaje = "NOMBRE: " . $nombre ." \r\n";
$mensaje .= "EMPRESA: " . $empresa . " \r\n";
$mensaje .= "E-MAIL: " . $email . " \r\n";
$mensaje .= "MENSAJE: " . $_POST['mensaje'] . " \r\n";
$mensaje .= "FECHA DEL ENVÍO: " . date('d/m/Y', time());
$para = 'xxxx@xxx';
$asunto = 'Blablabla';
mail($para, $asunto, utf8_decode($mensaje));
}
else{
//error message, or whatever
}
?>
还有,改变
<input type="checkbox" id="protecciondatos" name="protecciondatos" value="protecciondatos" required>
<label for="proteccion-datos">He leído y acepto la <a href="#">política de protección de datos</a> </label>
到
<input type="checkbox" id="protecciondatos" name="protecciondatos" value="protecciondatos" required>
<label for="protecciondatos">He leído y acepto la <a href="#">política de protección de datos</a> </label>
因为它不正确。