I've a double form with jQuery Validate, but when the ajax call its supposed to be done, I get a error, $.validate.submitHandrer
on main.js
.
My code is next:
'use strict';
var titulo;
var email;
var keyword;
var landing;
var keyword2;
var landing2;
var medio;
var extra;
$("#form_pedido").validate({
rules: {
titulo: {
required: true
},
keyword: {
required: true
},
landing: {
required: true
}
}, //fin rules
messages: {
email: {
remote: "Este correo no esta registrado."
}
},
submitHandler: function () {
//añadir aqui codigo ajax
titulo = $('#titulo').val();
email = $('#email').val();
keyword = $('#keyword').val();
landing = $('#landing').val();
keyword2 = $('#keyword2').val();
landing2 = $('#landing2').val();
medio = $('#periodico').val();
extra = $('#texto').val();
$.ajax({
type: 'GET',
dataType: 'json',
url: 'php/crear_publi.php',
data: {
email: email,
keyword: keyword,
landing: landing2,
keyword2: keyword2,
landing2: landing2,
medio: medio,
extra: extra
}
});
}
});
$("#formulario").validate({
rules: {
email: {
required: true,
minlength: 4,
email: true,
remote: "php/validar_email_db.php"
},
}, //fin rules
messages: {
email: {
remote: "Este correo no esta registrado."
}
},
submitHandler: function () {
$("#validar").click(function () {
$("#form_pedido").fadeIn("fast", function () {
$("#formulario").fadeOut("slow", function () {
});
});
});
}
});
//cargar medios en el select
$.ajax({
type: "POST",
dataType: 'json',
url: "php/listar_medios.php",
async: false,
success: function (data) {
$('#periodico').empty();
$.each(data, function () {
$('#periodico').append(
$('<option></option>').val(this.id_medio).html(this.nombre));
});
},
complete: {}
});
jQuery.validator.addMethod("lettersonly", function (value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Por favor, solo letras");
jQuery.validator.addMethod("spanishlettersspace", function (value, element) {
return this.optional(element) || /^[a-zA-ZñÑáéíóúÁÉÍÓÚ\s]*$/i.test(value);
}, "Por favor, solo letras y espacios");
My php code, but I dont think this is the problem:
<?php
header('Access-Control-Allow-Origin: *');
/* Database connection information */
include("mysql.php" );
/*
* AÑADIR MEDIOS!!!!
*/
function fatal_error($sErrorMessage = '') {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
die($sErrorMessage);
}
/*
* MySQL connection
*/
if (!$gaSql['link'] = mysql_pconnect($gaSql['server'], $gaSql['user'], $gaSql['password'])) {
fatal_error('Could not open connection to server');
}
if (!mysql_select_db($gaSql['db'], $gaSql['link'])) {
fatal_error('Could not select database ');
}
mysql_query('SET names utf8');
/*
* SQL queries
* Get data to display
*/
$titulo = $_GET["titulo"];
$email = $_GET["email"];
$keyword = $_GET["keyword"];
$landing = $_GET["landing"];
$keyword2 = $_GET["keyword2"];
$landing2 = $_GET["landing2"];
$medio = $_GET["medio"];
$extra = $_GET["extra"];
$sql1 = "SELECT id_cliente
FROM cliente
where email1='".$email."'";
$res1 = mysql_query($sql1);
while($row1 = mysql_fetch_array($res1, MYSQL_ASSOC))
{
$id_cliente=$row1['id_cliente'];
}
$query1 = "insert into publirreportaje (titulo,fecha_inicio,fecha_modificacion,contenido,id_cliente,id_medio,tipo,palabraClave,url,palabraClave2,url2,infoExtra) values(
'". $titulo . "',
NOW(),
NOW(),
'',
" .$id_cliente . ",
" . $medio .",
'publirreportaje',
'".$keyword."',
'".$landing."',
'".$keyword2."',
'".$landing2."',
'".$extra."');" ;
log($query1);
echo $query1;
$query_res1 = mysql_query($query1);
if($query_res1){
$sql = "SELECT id_contenido
FROM publirreportaje
where titulo='".$titulo."'";
$res = mysql_query($sql);
while($row = mysql_fetch_array($res, MYSQL_ASSOC))
{
$id_nuevo=$row['id_contenido'];
}
}
//echo "idenuevo => $id_nuevo";
for ($i=0;$i<count($editores);$i++)
{
//echo "<br> CLinicaid " . $i . ": " . $clinicas[$i];
$query2 = "insert into publi_editor (id_publirreportaje,id_editor) values(
". $id_nuevo . ",
" . $editores[$i] . ")" ;
//echo"$query1";
$query_res2 = mysql_query($query2);
}
if (!$query_res1||!$res||!$query_res2) {
// $mensaje = 'Error en la consulta de inserts: ' . mysql_error() . "\n";
// $estado = mysql_errno();
if (mysql_errno() == 1062) {
$mensaje = "Imposible añadir el articulo, num articulo ya existe";
$estado = mysql_errno();
} else {
$mensaje = 'Error en la consulta: ' . mysql_error() ;
$estado = mysql_errno();
}
}
else
{
$mensaje = "Insercion correcta";
$estado = 0;
}
$resultado = array();
$resultado[] = array(
'mensaje' => $mensaje,
'estado' => $estado
);
echo json_encode($resultado);
?>
Thanks everybody