【发布时间】:2017-05-23 17:16:10
【问题描述】:
我对 php 很陌生,但我正在制作一个带有联系表格和验证码 v.2 的网页,但要使验证码正常工作,它要求我添加一个 <input type="button"...
而不是 type="submit",这会使 html 中的必需属性不起作用,我希望输入成为必填字段
验证码正在工作,并且在选中“不是机器人”框时正在发送邮件,但是即使您没有填写电子邮件、电话、姓名等字段,您也可以发送它...
有什么我可以做的吗?还是我做错了什么?
这是我的代码:
html:
<p>Leave us a message and we'll contact you.</p>
<div class="col-md-6 col-md-offset-3 wow fadeInUp" data-wow-delay=".3s">
<form action="contactoEn.php" method="post" required>
<div class="form-group">
<input name="nombre" type="text" class="form-control" id="Name" placeholder="Name" required>
</div>
<div class="form-group">
<input name="email" type="email" class="form-control" id="Email" placeholder="Email" required>
</div>
<div class="form-group">
<input name="telefono" type="text" class="form-control" id="Tel" placeholder="Phone">
</div>
<div class="form-group">
<input name="asunto" type="text" class="form-control" id="Title" placeholder="Title">
</div>
<div class="form-group">
<textarea name="comentario" rows="3" class="form-control" id="comentario" placeholder="Message"></textarea>
</div>
<div class="g-recaptcha" data-sitekey="6LeIjB4UAAAAAAe31rx6o6-QewbEFhxbvoyNBZAj"></div>
<input type="button" value="Send" id="enviar" class="btn-block" onclick="submit()">
</form>
</div>
php:
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => '***********',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);
if (!$res['success']) {
// What happens when the CAPTCHA wasn't checked
echo '<script>alert("Please go back and be sure to check the section I am not a Robot. ");</script>';
} else {
// If CAPTCHA is successfully completed...
$EmailFrom = "***********";
$EmailTo = "***********";
$EmailTo2 = "***********";
$Nombre = Trim(stripslashes($_POST['nombre']));
$Nombre2 = Trim(stripslashes($_POST['email']));
$Nombre3 = Trim(stripslashes($_POST['telefono']));
$Nombre4 = Trim(stripslashes($_POST['asunto']));
$Nombre5 = Trim(stripslashes($_POST['comentario']));
$Body = "";
$Body .= "<html>";
$Body .= "<head>";
$Body .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
$Body .= "<title>Untitled Document</title>";
$Body .= "<style type=\"text/css\">";
$Body .= "<!--";
$Body .= "body {";
$Body .= " margin-left: 0px;";
$Body .= " margin-top: 0px;";
$Body .= "}";
$Body .= "-->";
$Body .= "</style>";
$Body .= "</head>";
$Body .= "<body>";
$Body .= "<table width=\"655\" height=\"24\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
$Body .= " <tr>";
$Body .= " <td><img src=\"http://***********/img/logo/masARO_LogoTopReferente.png\" /></td>";
$Body .= " </tr>";
$Body .= " <tr>";
$Body .= " <td class=\"texto_tit_chico\">Hemos recibido un mensaje de: $Nombre<br><br></td>";
$Body .= " </tr>";
$Body .= " <tr>";
$Body .= " <td class=\"texto_fecha_contenido\">Email:$Nombre2<br>Telefono:$Nombre3<br>asunto:$Nombre4<br>Mensaje:$Nombre5<br> </td>";
$Body .= " </tr>";
$Body .= " <tr>";
$Body .= " <td><div align=\"center\" class=\"texto_mas_eventos\"><br>Enviado desde pagina de español<br></div></td>";
$Body .= " </tr>";
$Body .= " <tr>";
$Body .= " <td> </td>";
$Body .= " </tr>";
$Body .= "</table>";
$Body .= "</body>";
$Body .= "</html>";
$Subject = "*********** - Contáctanos";
$success = mail($EmailTo, $Subject, $Body, "From: *********** <$EmailFrom>\nContent-type: text/html; charset=utf-8\n");
$success = mail($EmailTo2, $Subject, $Body, "From: *********** <$EmailFrom>\nContent-type: text/html; charset=utf-8\n");
echo '<script>alert("Message sent, Thank you");</script>';
}
我希望这是问这个问题的正确方法 提前致谢
【问题讨论】:
-
您正在使用
submit()函数检查验证码,然后提交表单。您需要对该函数中所需的输入执行所有检查。 -
这在 php 中吗?对吗?顺便说一句,非常感谢
-
不,它在 Javascript
function submit()中。 -
既然你用
onclick="submit()"调用它 -
这就是我最终得到的结果,它可以工作: