AJAX
Publicado por Gustavo (14 intervenciones) el 15/09/2020 14:15:37
Buen día me ha surgido un problema y no hallo como solucionarlo,he intentado con un for pero no funciona.
tengo el siguiente html, hasta aqui todo bien
tengo una funciton RCFve() que como parametro recibe la url a donde se envian las variables.
al recibir la url las variables son enviadas al siguiente php
una vez hecho esto, paso como parametros las variables recibidas para enviarlas a mi modelo
Mi problema esta en que mi consulta devuelve 11111 y por los momento cumple su función, pero si se fija en mi peticion ajax solo hay r==true || r==11111, si se agrega otro campo en el html ya no seria 11111 sino 111111.
Lo que quiero lograr es que en mi AJAX recoja ese true sin importar cuantos 1 devuelva mi consulta
Gracias de antemano
tengo el siguiente html, hasta aqui todo bien
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
include_once("../../CONTROLLERS/agregar/select_centros.php");
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- VISTA DE MOVIL -->
<meta name="viewport" content="width=device-width, initial-scale=.1, shrink-to-fit=no">
<!----------------------------->
<meta name="description" content="">
<meta name="author" content="">
<title>Observatorio Nacional de Ciencia y tecnología e innovación</title>
<!-- Bootstrap core CSS -->
<link href="../../CSS/CSS-BOOTSTRAP/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="../../CSS/CSS-BOOTSTRAP/style.css" rel="stylesheet">
<!-- ESTILOS PERSONALIZADOS POR MI --------->
<link rel="stylesheet" href="../../CSS/style1.css">
<link rel="shortcut icon" href="../../IMG/icon.png" />
<link rel="stylesheet" href="../../CSS/style2.css">
<link rel="shortcut icon" href="../../IMG/icon.png" />
<link rel="stylesheet" href="../../CSS/datepicker.css">
<!------------------------------->
</head>
<body>
<div class="loader"><h1 id="mensaje" style="display: none">Esta tardando mucho...</h1></div>
<div class="images">
<img class="cintillo" src="../../IMG/Cintillo.png" alt="">
</div>
<div class="d-flex" id="wrapper">
<!-- Sidebar -->
<div class="bg-light border-right" id="sidebar-wrapper" >
<?php include_once("../../menu.php") ?>
</div>
</div>
</div>
<!-- /#sidebar-wrapper -->
<!-- Page Content -->
<div id="page-content-wrapper">
<?php include_once("../../menux2.php"); ?>
<!------------- COMIENZO DEL CONTENIDO DE LA VISTA PRINCIPAL DENTRO DE SECTION-------------->
<div class="container-fluid" >
<form id="formulario" method="post" action="../../CONTROLLERS/agregar/personas-contagiadas.php" name="formulario">
<table border="1">
<?php while ($c = $centros->fetch()) {
?>
<tr>
<td><?php echo $c['descentro'] ?><input type="hidden" name="id[]" value="<?php echo $c['codcentro'] ?>" placeholder=""></td><td><input type="number" class="input val" placeholder="Campo numérico" name="personas[]" value="" ></td>
</tr>
<?php
} ?>
<tr>
<td colspan="5" rowspan="" headers="" ><input type="text" id="fecha" required="" name="fecha" value="" placeholder="dd/mm/yyy" style="width: 100%"></td>
</tr>
<input class=" btn btn-success btn-enviar" type="submit" id="btn-enviar" value="Enviar"></input>
</table>
</form>
<div class="success" id="success">
<div class="mensaje" style="position:relative;top: 20px;font-weight: bold"></div>
</div>
</div>
<!--------------FIN DEL CONTENIDO DE LA VISTA PRINCIPAL--------->
<!-- /#page-content-wrapper -->
</div>
<!-- Bootstrap core JavaScript -->
<script src="../../JS/JQUERY/jquery.min.js"></script>
<script src="../../JS/JS-BOOTSTRAP/bootstrap.bundle.min.js"></script>
<script src="../../JS/jquery-3.2.1.min.js"></script>
<script src="../../JS/funciones.js"></script>
<script src="../../JS/datepicker.js"></script>
<script>
menu();
var url='../../CONTROLLERS/agregar/personas-contagiadas.php';
RCFve(url)</script>
<!--SCRIPTS PERSONALIZADOS-->
<script>
$(function(){
$("#fecha").datepicker({
dateFormat: "dd-mm-yy",
minDate:0,
monthNames:['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Nomviembre','Diciembre',],
dayNamesMin:['Do','Lu','Ma','Mie','Jue','Vie','Sab'],
});
});
</script>
</body>
</html>
tengo una funciton RCFve() que como parametro recibe la url a donde se envian las variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function RCFve(url){
$(document).ready(function(e){
$("#btn-enviar").click((e)=>{
e.preventDefault();
x =$('.val');
for(i=0;i<x.length;i++){
if (x[i].value.length===0) {
e.preventDefault();
x[i].style.borderBottom="1px solid red"
x[i].placeholder='Campo numérico vacio'
}if($("#fecha").val()===''){
$("#fecha").css({border:'1px solid red'})
}else{
for(i=0;i<x.length;i++){
if (x[i].value.length>0) {
e.preventDefault();
x[i].style.borderBottom="1px solid rgba(100,100,100,.2)"
}}
$("#fecha").css({border:'1px solid rgba(100,100,100,.2)'})
var datos = $("#formulario").serialize();
$.ajax({
type:"POST",
url:url,
data:datos,
success:function(r){
if(r==true || r==11111){
$("#success").css({
transform:'translateX(-170%)'
})
$(".mensaje").html("Se ha registrado exitosamente!")
setTimeout(()=>{$("#success").css({
transform:'translateX(120%)',
opacity:'0.5'
})},2500)
document.getElementById("formulario").reset();
}else{
$("#success").css({
transform:'translateX(-170%)',
background:'red',
opacity:'0.5'
})
$(".mensaje").html("Lo sentimos, no se pudo resgistrar :(")
setTimeout(()=>{$("#success").css({
transform:'translateX(120%)',
background:'rgba(40,167,69,.5)'
})},2500)
}
}
});
return false;
}
}
});
})
}
al recibir la url las variables son enviadas al siguiente php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
include_once("../../CONFIG/datosBD.php");
include_once("../../autocarga.php");
use MODELS\indicadores\comportamientoVnzl as personas_contagiadas;
$obj = new conexion;
$conex = $obj->conectar();
if ( isset($_POST)) {
$obj= new personas_contagiadas\personasContagiadas;
$personas = $_POST['personas'];
$id = $_POST['id'];
$fecha = $_POST['fecha'];
$verif=$obj->verificar($conex,$fecha);
if ($verif->fetchColumn()>0) {
echo 'no';
return 400;
}else{
for ($i=0; $i <sizeof($id) ; $i++) {
$obj->agregar($id[$i],$personas[$i],$fecha,$conex);
}
}
}else{
?>
<script>history.go(-1)</script>
<?php
}
?>
una vez hecho esto, paso como parametros las variables recibidas para enviarlas a mi modelo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public function agregar($id,$personas,$fecha,$conex){
try{
$string="INSERT INTO indicador_centros (codtipo,codpai,codcentro,contagiados,fecha) VALUES (:personas_contagiadas, :codpai, :codcentro, :contagiados, :fecha)";
$prepare = $conex->prepare($string);
echo $execute = $prepare->execute(array(':personas_contagiadas'=>codpersonas,':codpai'=>codpai,':codcentro'=>$id,':contagiados'=>$personas,':fecha'=>$fecha));
}catch(PDOException $e){
echo 'ERROR '.$e->getMenssage();
}
}
Mi problema esta en que mi consulta devuelve 11111 y por los momento cumple su función, pero si se fija en mi peticion ajax solo hay r==true || r==11111, si se agrega otro campo en el html ya no seria 11111 sino 111111.
Lo que quiero lograr es que en mi AJAX recoja ese true sin importar cuantos 1 devuelva mi consulta
Gracias de antemano
Valora esta pregunta


1