Fill input text according to the value of other input text

0

I have a product table which has a code, name, etc. ..
then when entering the product code in the form, automatically place the name of that product ...

<form  accept-charset="utf-8">  
    <div class="form-group" id='mcodigo'>
        <label>Código</label> 
        <input type="text" class="form-control" required placeholder="Código" id='codigo' name='codigo' autofocus>
    </div>

    <div class="form-group">
        <label>Nombre</label>
        <input type="text" disabled class="form-control" id='nombre' name='nombre'>
    </div>
 </form>
    
asked by Cheke Sanchez 30.03.2017 в 20:47
source

2 answers

1

Well, there is a function called change () in jquery, in which you capture the value in the following way.

$(document).ready(function(){ // Se ejecuta una vez que el DOM de la pagina este listo
        $( "#codigo" ).change(function() { //Captura cada vez que vea un cambio en el input con id "codigo"
              //Ejecuta la accion a realizar
        });
});

Here the question is: Do you have a controller, which when passing a parameter (the code), returns the parameters you need back? and do you have a connection to the database, and the query you need for it?

These questions are key, because the idea is that every time you change data in this input, an ajax will be executed which will make an asynchronous call to look up your data in real time, to the controller, where it will happen the parameter "code" and this when executing a query to the database, will return the information that is needed, and through the same ajax, we will leave the answer in their respective inputs. Greetings!

    
answered by 30.03.2017 в 21:44
0
<?php

include('Conexion.php');

$codigo = $_POST["codigo"];

if (!$con) {
    echo 'No pudo conectarse a mysql';
    exit;
}

if (!mysql_select_db('BASE DE DATOS', $con)) {
    echo 'No pudo seleccionar la base de datos';
    exit;
}

$sql       = 'SELECT nombre FROM TABLADEDATOS WHERE codigo = '.$codigo; //solo un ejemplo de la consulta
$resultado = mysql_query($sql, $con);

if (!$resultado) {
    echo "Error de BD, no se pudo consultar la base de datos\n";
    echo "Error MySQL: ' . mysql_error();
    exit;
}


$pila = array();
while ($fila = mysql_fetch_assoc($resultado)) {
      array_push($pila, $fila['nombre']);
}

echo json_encode($pila);

mysql_free_result($resultado);


?>

Consider $ with as the connection variable to connect to the class itself. You pass the include of the connection, then you capture by the "POST" the code that you have to pass from the web page to the controller. Then you do some consultations to see if you can connect with the bbdd and with the table you need. After that you make a query, you mount it in a variable and you do the query to the bbdd with mysql_query where it will make the call to the DB. If it has problems, it will return an error, and if not put the result in an array as a new element. Once you have reviewed all the results, it will return a json, where in jquery you will capture it through ajax.

Let me know when you have these steps ready, and sorry for the delay.

    
answered by 31.03.2017 в 18:44