Problems in passing json values

0

I have the following code:

$.post("php/llenar.php",{opcion: op}, function(data){
		$("#valor").html(data);
	});
	$("#valor").change(function(){
		$("#valor option:selected").each(function(){
			op = "valor";
			valor= $(this).val();
			$.post("php/llenar.php",{opcion: op, valor: valor}, function(data){
				$("#valor1").val(data.valor);
				//$("#cedula").val(data.cedula);
			}, "json");
		});
	});
$valor = mysqli_real_escape_string($con,$_POST['valor']);
		if(empty($valor)){
			$sql="SELECT codigovar, descripcion FROM valores";
			$result=mysqli_query($con, $sql);
			$html.="<option valur=''>Escoga una opción</option>";
			while($row = mysqli_fetch_array($result))
			{
				$html.="<option value ='".$row['codigovar']."'>".$row['descripcion']."</option>";
			}
		}else{
			$sql  = "SELECT valor FROM valores WHERE codigovar = '$valor'";
			$result=mysqli_query($con, $sql);
			$row =  mysqli_fetch_array($result);
			echo json_encode(array("valor"=>$row['valor']));
		}
<div id="contenedor">
	<h1 id="titulo-form"> Asignacion turno de trabajo</h2>
	<div id="form">
		<form>
			<select class="input-100" type = "text" id="tpveh" name="tpveh">
				<option value="0">Escoga un tipo de vehiculo</option>
				<option value="camioneta">Camioneta</option>
				<option value="carry">Carry</option>
				<option value="furgon">furgon</option>
				<option value="moto">Moto</option>
			</select>			
			<select class="input-100" type = "text" id="placa" name="placa">
			</select>	
			<input class="input-50" type="text" id="mod" name="cc" readonly="readonly">
			<input class="input-50" type="text" id="cap" name="nomape" readonly="readonly"> 
			<select class="input-50" type = "text" id="valor" name="valor">
			</select>	
			<input class="input-50" type="text" id="valor1" name="valor1"> 
			<input type = "button" value = " Registrar" class = "btn-enviar" id = "btn-enviar"/>
		</form>
	</div>
</div>	

When checking if the value of the base is pulling me, I realize that the query is doing its job as you can see in the following image.

But you're not assigning the value to the input, I do not know if I'm omitting something.

I would appreciate your help.

    
asked by Familia Valencia Hdz 18.10.2018 в 21:08
source

1 answer

0

Thanks for the help you gave me, but I found the error, it turns out that the code that I uploaded from PHP is only part of it, that it happened when I closed the connection to the BD and sent the variable $ html to print this variable in each part of the code and I am solved my problem

<?php
include("conex.php");
$opcion = $_POST['opcion'];
//echo $opcion;
switch ($opcion){
    case "dpto":
        $dpto = mysqli_real_escape_string($con,$_POST['dpto']);
        if (empty($dpto)){
            $sql  = "SELECT departamento.cod_depto, nombre FROM departamento ORDER BY nombre ASC";
            $result = mysqli_query($con, $sql);
            $html.="<option value = ''>Escoga un departamento</option>";
            while($row = mysqli_fetch_array($result))
            {
                $html.="<option value ='".$row['cod_depto']."'>".$row['nombre']."</option>";
            }
        }else{
            $sql  = "SELECT cod_municipo, nombre FROM municipio WHERE cod_dpto = '$dpto' ORDER BY nombre ASC";
            $result = mysqli_query($con, $sql);
            $html.="<option value = ''>Escoga un municipio</option>";
            while($row = mysqli_fetch_array($result))
            {
                $html.="<option value ='".$row['cod_municipio']."'>".$row['nombre']."</option>";
            }   
        }
        echo $html;
        break;
    case "placas":
        $tipo = mysqli_real_escape_string($con,$_POST['tipovh']);
        $sql  = "SELECT placa FROM vehiculo WHERE tipovehiculo = '$tipo' ORDER BY placa ASC";
        $result = mysqli_query($con, $sql);
        $html.="<option value = ''>Escoga una placa</option>";
        while($row = mysqli_fetch_array($result))
        {
            $html.="<option value ='".$row['placa']."'>".$row['placa']."</option>";
        }
        echo $html;
        break;
    case "marca":
        $marca = mysqli_real_escape_string($con,$_POST['marca']);
        if (empty($marca)){
            $sql  = "SELECT codigomarca, nombre FROM marcas ORDER BY nombre ASC";
            $result = mysqli_query($con, $sql);
            $html.="<option value = ''>Escoga una marca</option>";
            while($row = mysqli_fetch_array($result))
            {
                $html.="<option value ='".$row['codigomarca']."'>".$row['nombre']."</option>";
            }
        }else{
            $sql  = "SELECT codigolinea, nombre FROM lineas WHERE codigomarca = '$marca' ORDER BY nombre ASC";
            $result = mysqli_query($con, $sql);
            $html.="<option value = ''>Escoga una linea</option>";
            while($row = mysqli_fetch_array($result))
            {
                $html.="<option value ='".$row['codigolinea']."'>".$row['nombre']."</option>";
            }   
        }
        echo $html;
        break;
    case "valor":
    $codigo = mysqli_real_escape_string($con,$_POST['cod']);
    //echo $codigo;
    if(empty($codigo)){
        $sql="SELECT codigovar, descripcion FROM valores";
        $result=mysqli_query($con, $sql);
        $html.="<option value=''>Escoga una opción</option>";
        while($row = mysqli_fetch_array($result))
        {
            $html.="<option value ='".$row['codigovar']."'>".$row['descripcion']."</option>";
        }
        echo $html;
    }else{
        $sql  = "SELECT valor FROM valores WHERE codigovar = '$codigo'";
        $result = mysqli_query($con, $sql);
        $row = mysqli_fetch_array($result);
        echo json_encode(array("precio"=>$row['valor']));
    }
    break;
}
mysqli_free_result($result);
mysqli_close($con);?>
    
answered by 19.10.2018 / 19:41
source