Hello, good day, it would help me a lot if you could help me with the following:
Practically the code has a select where the value you select goes to the script but so far it hasn't worked for me
<script>
$('#id_tracto').on('change', function() {
$.ajax({
url : 'datos_reparacion.php',
data : { valor : valor },
type : 'POST',
dataType : 'json',
success : function(json) {
$("#patente_tracto").val(json.patente_tracto);
$("#marca_tracto").val(json.marca_tracto);
$("#propietario_tracto").val(json.propietario_tracto);
},
error : function(xhr, status) {
alert('Disculpe, existió un problema');
}
});
});
</script>
<div class="col-md-4 position-relative">
<label class="form-label">SELECCIONAR CAMION:</label>
<select name="id_tracto" id="id_tracto" class="form-control">
<option value="">Seleccione...</option>
<?php
// Realizamos la consulta para extraer los datos
$consulta="SELECT * FROM tracto";
$result=mysqli_query($mysqli,$consulta) or die (mysqli_error());
while ($row=mysqli_fetch_array($result)){
// En esta sección estamos llenando el select con datos extraidos de una base de datos.
echo '<option value="'.$row['id_tracto'].'">'.$row['patente_tracto'].'</option>';
}
?>
</select>
</div>
<div class="col-md-4 position-relative">
<label class="form-label">MARCA CAMION:</label>
<input type="text" class="form-control" name="marca_tracto" id="marca_tracto">
</div>
*** I have this in data_repair.php***
<?php
require("../../Conexion/Conectar_Bbdd_Oroverde.php");
$valor=$_POST['valor'];
$jsondata = array();
$query="SELECT id_tracto,patente_tracto,marca_tracto,propietario_tracto from tracto where id_tracto=$valor";
$r=mysqli_query($mysqli,$query);
$resultados= mysqli_fetch_array($r);
$id_tracto=$resultados['id_tracto'];
$patente_tracto=$resultados['patente_tracto'];
$marca_tracto=$resultados['marca_tracto'];
$propietario_tracto=$resultados['propietario_tracto'];
$jsondata['id_tracto'] = $id_tracto;
$jsondata['patente_tracto'] = $patente_tracto;
$jsondata['marca_tracto'] = $marca_tracto;
$jsondata['propietario_tracto'] = $propietario_tracto;
header('Content-type: application/json; charset=utf-8');
echo json_encode($jsondata);
mysqli_close($mysqli);
?>
**I have this in the code, where the script sent the data value to data_repair.php and there it does the query and returns the data I need and sends it to the input that I have below
In your Javascript code you are not getting the value of the
select
, without which the server will not receive any data to process.Then the PHP code can be simplified as well. Mysqli has a function
mysqli_fetch_assoc
that creates an associative array with column names => values, so you don't have to put everything by hand.This is how it should work. Although you still need to refine your code a bit, establishing error handling. And give it security, applying prepared queries, since your current query is highly vulnerable to SQL injection attacks , which is a very serious security hole .