Send data by AJAX and return it by JSON


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">
	<meta charset="UTF-8">
	<!-- metas -->
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<!-- library jq -->
	<script src=''></script>
	<!-- styles -->
	<link href="" rel="stylesheet">
	<link rel="stylesheet" href="estilos.css">
		<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">
  <script type="text/javascript">

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

    			var save = $.ajax({
			          type: "POST",
			          url: "validar-certlaboral.php",
			          data: 'codigo=' + codigo,
			          async: false,
			          success: function(){
			      	    var url="validar-certlaboral.php";
						$("#tabla tbody").html("");
							$.each(clientes, function(i,cliente){
							var newRow =
							$(newRow).appendTo("#tabla tbody");
   <table class="tabla" >
				<th>Fecha expedición</th>

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


$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);

	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

0 answers