Send data by AJAX and return it by JSON

0

I'm trying to make a routine where the user enters a validation code and the moment he gives in sending; send what the user wrote, make a query to the database and there make a tour of the information to return it.

This is something I've tried to do. I do not have much idea, because it is the first time that I use JSON.

<!DOCTYPE html>
<html lang="es">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<!-- metas -->
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<!-- library jq -->
	<script src='https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>
	<!-- styles -->
	<link href="https://fonts.googleapis.com/css?family=Signika+Negative" rel="stylesheet">
	<link rel="stylesheet" href="estilos.css">
</head>
<body>
	<section>
		<div class="caja-uno">
			<div class="contenedor">
				<h2>Validación de código de autenticidad</h2>
				
				<div class="elementos">
					<h3>Digite el código a validar</h3>

					<input type="text" id="cod" name="cod">
					<input type="submit" id="submit" value="Validar">
				</div>
			</div>
		</div>
	</section>
  <script type="text/javascript">
		$(document).ready(function(){
			
			$('#submit').click(function(){

				var codigo = $("#cod").val();
				alert(codigo);

    			var save = $.ajax({
			          type: "POST",
			          url: "validar-certlaboral.php",
			          data: 'codigo=' + codigo,
			          dataType:"html",
			          async: false,
			          success: function(){
			      	    var url="validar-certlaboral.php";
						$("#tabla tbody").html("");
						$.getJSON(url,function(clientes){
							$.each(clientes, function(i,cliente){
							var newRow =
								"<tr>"
								+"<td>"+cliente.nombres+"</td>"
								+"<td>"+cliente.identificacion+"</td>"
								+"<td>"+cliente.cargo+"</td>"
								+"<td>"+cliente.contrato+"</td>"
								+"<td>"+cliente.estado+"</td>"
								+"<td>"+cliente.salario+"</td>"
								+"<td>"+cliente.fecha+"</td>"
								+"</tr>";
							$(newRow).appendTo("#tabla tbody");
							});
						});
			          }
      			}).responseText;
           console.log(save);
			});
		});
    </script>
   <table class="tabla" >
			<thead>
				<th>Nombre</th>
				<th>Identificación</th>
				<th>Cargo</th>
				<th>Contrato</th>
				<th>Estado</th>
				<th>Salario</th>
				<th>Fecha expedición</th>
			</thead>
			<tbody>
			</tbody>
		</table>
</body>
</html>

This is the form that receives the validation code and where the query is made.

<?php

$codigo = $_POST['codigo'];

print $codigo."\n";

require 'conn.php';

$consulta ="SELECT NOMBRES, U_SOLICITA, COD_VALIDACION, FECHA_SOLICITA, CARGO, TIPO_CONTRATO,DISPOSICION, CONCAT('$', SUM( SAL_BASICO+AUX_MOV+AUX_ROD+AUX_MAN)) AS SALARIO FROM 'certificados_laborales' INNER JOIN tbl_personal ON certificados_laborales.U_SOLICITA=tbl_personal.EXTERNAL_ID INNER JOIN cc_nom ON certificados_laborales.U_SOLICITA=cc_nom.CC_NOM INNER JOIN datos_corporativos ON certificados_laborales.U_SOLICITA=datos_corporativos.EXTERNAL_ID WHERE COD_VALIDACION = '".$codigo."'";

//print $consulta.".\n";


$doquery = mysqli_query($conn, $consulta);
$numeros = mysqli_num_rows($doquery);
//var_dump($numeros);

if(!$numeros){
	print "Algo malo ha ocurrido con la consulta.";
}
if ($numeros == 0) {
	print "El campo a buscar no se encuentra en la base de datos.";
}

if ($numeros) {
	$clientes = array();
	while ($data = mysqli_fetch_assoc($doquery)) {
		$nombres = $data['NOMBRES'];
		$cc = $data['U_SOLICITA'];
		$cargo = $data['CARGO'];
		$contrato = $data['TIPO_CONTRATO'];
		$estado = $data['DISPOSICION'];
		$salario = $data['SALARIO'];
		$fecha = $data['FECHA_SOLICITA'];
		
		$clientes[]=array('nombres' => $nombres, 'identificacion' => $cc, 'cargo' => $contrato, 'contrato' => $contrato, 'estado' => $estado, 'salario' => $salario, 'fecha' => $fecha);
	}
}
$json_string = json_encode($clientes);
echo $json_string;

?>	

When I log in the console it returns an array in JSON format but I can not show it in the table I have. That's what I try ... I hope I made myself understood about this little problem I have.

    
asked by devsant13 04.12.2018 в 22:40
source

0 answers