我已经搜索并查看了有关通过Array显示错误消息的不同联系表单示例和主题,但没有任何成功。
现在我的问题是?
有没有办法在其中包含另一个变量$errors
或array
将错误消息分发到每个特定字段以在每个失败的表单字段下方或侧面显示错误消息。
我的表格:
<!DOCTYPE html>
<html>
<head>
<title>Formulario de Contacto</title>
</head>
<body>
<?php
session_start();
$errors = '';
$nombre = '';
$email = '';
$seleccionado = '';
$telefono = '';
$mensaje = '';
if(isset($_POST['submit'])) {
if($_SESSION['vcode'] != $_POST['vcode']) {
$errors .= "Los caracteres no coincide con el código captcha";
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nombre = test($_POST["name"]);
$email = test($_POST["mail"]);
$telefono = test($_POST["phone"]);
$mensaje = test($_POST["message"]);
$seleccionado = test($_POST['producto']);
}
if(empty($nombre)) {
$errors .= "\n Por favor ingrese su nombre.";
}else {
if (!preg_match("/^[a-zA-Z ]*$/",$nombre)) {
$errors .= "\n Sólo se permiten letras y espacios en blanco.";
}
}
if(empty($email)) {
$errors .= "\n Por favor ingrese su email. ";
}else{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .="\n email no valido";
}
}
if(empty($telefono)) {
$errors .= "\n Por favor ingresar su número telefono. ";
}else{
if(!preg_match("/^\+?\d{1,3}?[- .]?\(?(?:\d{2,3})\)?[- .]?\d\d\d[- .]?\d\d\d\d$/", $telefono)) {
$errors .="\n Número no valido";
}
}
if($_POST['producto'] == 0){
$errors .= "\n Debe seleccionar un producto";
}
if(empty($mensaje)) {
$errors .= "\n Por favor ingrese su mensaje. ";
}
if(empty($errors)) {
$asunto = "";
$message = "Usuario:".$_POST['name']." Email:".$_POST['mail']." Telefono ".$_POST['phone']." Informacion ".$_POST['message'];
$destino = "[email protected]";
$remitente = "From: [email protected]";
mail($destino,$asunto,$message,$remitente);
unset($_POST['submit']);
$msg= "Gracias por sus comentarios";
}
}
?>
<?php
function test($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div class="error">
<?php
if(isset($msg)){
echo "<p class='err'>".$msg."</p>";
}else{
if (isset($errors))
echo "<p class='err'>".nl2br($errors)."</p>";
# code...
?>
</div>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" autocomplete="off" enctype="multipart/form-data">
<div class="touch">
<div class="name">
<input type="text" name="name" placeholder="Name" value='<?php echo htmlentities($nombre) ?>'>
</div>
<div class="email">
<input type="text" name="mail" placeholder="Email" value='<?php echo htmlentities($email) ?>'>
</div>
<div class="phone">
<input type="tel" name="phone" placeholder="Phone" value='<?php echo htmlentities($telefono) ?>'>
</div>
<div class="select-pro">
<select name="producto">
<?php
$datos = array("Asunto","TV","Internet");
for($i=0; $i<count($datos); $i++) {
if($i==$seleccionado) {
echo "<option value='".$i."' selected>".$datos[$i]."</option>";
}else {
echo "<option value='".$i."'>".$datos[$i]."</option>";
}
}
?>
</select>
</div>
<div class="Customer-message">
<textarea id="message" name="message" placeholder="Su consulta..."><?php echo htmlentities($mensaje) ?></textarea>
</div>
<div class="capcha">
<img src="image.php" name="vcode" id="phoca-captcha"/>
<input name="vcode" type="text" placeholder="Codigo captcha">
</div>
<input type="submit" name="submit" value="Enviar">
</div>
</form>
<?php } ?>
</body>
</html>
我将为您提供一个在
localhost
.首先,我将语句更改
if
else
if
为if
,elseif
else
,我认为它看起来更清晰,结构更好。要启动所有正确的消息,我们只需检查
variables
是否正确创建了所有表单。否则,它将根据情况启动每个input
错误填写或为空的错误消息。jQuery
这是一个简单的系统,您可以随时按、AJAX
、前进,或者如果您不了解,PHP
甚至可以快速创建一个小动画,并在特定时间显示这些错误消息。CSS3
我还稍微优化了代码
PHP
。我已经添加到验证码规则,如果它是空的,它只是添加我们的语句
if
elseif
else
。你可以添加你想要的,如果你需要做更多的检查,它可能是这样的if
elseif
elseif
……else
(一个例子是添加另一个检查来计算字符串的最小值或最大值等)。A los errores e añadido un
span
con el atributoclass
para así crear una regla estándar para dichos mensajes, en este caso una pequeña animación con un tiempo fácil de modificar (7 segundos), la animación tiene un pequeño efecto a su inicio y su fin.Ejemplo CSS:
Ejemplo HTML:
¡Suerte!
发生在我身上的是这样的事情,显然这是没有的,
style
但您可以将它们打印在一个<span>
或一个内部<p>
并给它类,以便它可以看到下方或侧面。这取决于您希望如何响应,无论是在您从一个文本表单跳转到另一个文本表单时立即还是在使用提交发送之后
在第一种情况下,您必须依赖 Jscript,在第二种情况下,使用根据错误设置的全局变量 ($_session[])