Foros del Web » Programando para Internet » Jquery »

[SOLUCIONADO] ReCaptcha 2 y jQuery Serialize

Estas en el tema de ReCaptcha 2 y jQuery Serialize en el foro de Jquery en Foros del Web. Hola gente. Estoy intentando implementar el reCaptcha v2 mediante un ajax. Parece que todo va bien pero luego no funciona correctamente. Mi form Código HTML: ...
  #1 (permalink)  
Antiguo 21/08/2015, 03:04
 
Fecha de Ingreso: enero-2010
Mensajes: 63
Antigüedad: 14 años, 2 meses
Puntos: 7
ReCaptcha 2 y jQuery Serialize

Hola gente.

Estoy intentando implementar el reCaptcha v2 mediante un ajax. Parece que todo va bien pero luego no funciona correctamente.

Mi form
Código HTML:
<form id="form" method="post" action="<?=RUTA;?>inc/email.php" enctype="multipart/form-data">
                            	<div class="flt">
                                	<label>Nombre:</label><input type="text" name="nombre">
                                </div>
                                <div class="flt">
                                    <label>E-Mail:</label><input class="email" type="text" name="mail">
                                </div>
                                <div class="flt">
                                	<label>Telefono:</label><input type="text" name="telf">
                                </div>
                                <div class="flt">
                                	<label>Asunto:</label><input type="text" name="asunto"><br>
                                </div>
                                <div class="flt">
                                	<label>Mensaje:</label><br><textarea name="mensaje"></textarea>
                                </div>
                                <div class="flt">
                                	<label>Captcha:</label><br>
                                    <div class="g-recaptcha" data-sitekey="CLAVE DE SITIO"></div>
                                </div>
                                <input type="submit" class="c_submit" value="Enviar">
                            </form> 
Mi JS

Código:
$(function() {	
		$('.c_submit').click( function(e){
			e.preventDefault();
			var formimg = $('#form');
			var formimg_array = $(formimg).serialize();
			var formimg_url = $(formimg).attr('action');
			var	formmail = $('.email').val();
			console.log(formimg_array);
			if ( !(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(formmail)) ) { alert("Por favor, introduzca un e-mail válido."); }
			else {
				
				$.ajax( {
				  url: formimg_url,
				  type: 'POST',
				  data: formimg_array,
				  processData: false,
				  contentType: false,
				  success: function(data) {
					console.log(data);
					$('.mensaje').show();
					setTimeout(function(){ $('.mensaje').hide();	},5000);
					$(':input', formimg).each(function() {
						var type = this.type;
						var tag = this.tagName.toLowerCase();
						if (type == 'text' || tag == 'textarea')
						this.value = "";
					
					});
				  },
				  error: function (xhr, ajaxOptions, thrownError) {
					console.log('MAL');
					}
				} );
			};
		});
	});
y mi email.php

Código PHP:
<?
if(isset($_POST['nombre'])){ $nombre=$_POST['nombre'];}
if(isset(
$_POST['mail'])){ $email=$_POST['mail'];}
if(isset(
$_POST['telf'])){ $tel=$_POST['telf'];}
if(isset(
$_POST['asunto'])){ $asunto2=$_POST['asunto'];}
if(isset(
$_POST['mensaje'])){ $consulta=$_POST['mensaje'];}

if(isset(
$_POST['g-recaptcha-response'])){  $captcha=$_POST['g-recaptcha-response'];}
if(!
$captcha){  echo '<script>alert("Indica el captcha '.$_POST['g-recaptcha-response'].'")</script>';  exit;}

$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECURE(eliminada para internet)&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
 


$para '[email protected]';
$mio '[email protected]';

$asunto "Contacto - ".$nombre;

$cabeceras  "MIME-Version: 1.0" "\r\n";
$cabeceras .= "Content-type: text/html; charset=utf-8" "\r\n";
$cabeceras .= "From: Mi Web <".$mio.">" "\r\n" "Reply-To: ".$email."" "\r\n";

$mensaje "
<html>
<head>
    <title>Contacto Web</title>
</head>
<body>
<div>
<strong>Nombre:<br /></strong>"
.$nombre."<br /><br />
<strong>E-Mail:<br /></strong>"
.$mail."<br /><br />
<strong>Teléfono:<br /></strong>"
.$tel."<br /><br />
<strong>Asunto:<br /></strong>"
.$asunto2."<br /><br />
<strong>Consulta:<br /></strong>"
.$consulta."<br /><br />
</div>

</body>
</html>"
;


if(
$response.success) {
    
mail($para,utf8_decode($asunto),$mensaje,$cabeceras);
} else {
    echo 
'<script>alert("No eres un humano!!");</script>';
}

El problema es que el console.log del serialize me muestra esto.

Código:
nombre=&mail=neo%40neo.com&telf=&asunto=&mensaje=&g-recaptcha-response=03AHJ_VuvodDwdjTroGPMSdUuySUTETzqfwyEUqKYpl_oXO0bz_6CFU8OPNk9tJGsovIZ
Pero el $_POST['g-recaptcha-response'] me sale vacio, asique no consulta y da el error de "Introduzca el captcha".

Alguna idea.

Gracias.
__________________
Manz Soluciones On/Off
  #2 (permalink)  
Antiguo 16/10/2015, 06:28
 
Fecha de Ingreso: enero-2010
Mensajes: 63
Antigüedad: 14 años, 2 meses
Puntos: 7
Respuesta: ReCaptcha 2 y jQuery Serialize

El problema era de pasarle las variables por POST, al pasarlas por GET no da ningun problema.
__________________
Manz Soluciones On/Off

Etiquetas: recaptcha, serialize
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:15.