Ich bin Neuling mit AJAX, und ich fand die Code-Lösung für mein Problem auf dieser Seite, aber ich kann es nicht funktionieren.Zuverlässige Auswahlbox funktioniert nicht
Ich brauche, dass der Eingang #precio
ändert, wenn ich eine Option in wählen #producto
auswählen.
Ich habe diesen Code:
<form action="actualizarprod_pr.php" method="post">
<input type="text" name="producto" value="" placeholder="Producto" list="productolist" />
<datalist id="productolist">
<select id="productos" style="display: none;">
<?php
$query = "SELECT * FROM lista_precios";
$query_ej = mysqli_query($conexion, $query);
while($registro = mysqli_fetch_array($query_ej)){
echo "<option value='" . $registro['producto'] .
"'</option>";
}
?>
</select>
</datalist>
<input type="text" id="precio" name="precio" placeholder="Precio">
<input type="submit" name="Actualizar" value="Actualizar">
<input type="submit" name="Borrar" value ="Borrar">
</form>
Script:
<script type="text/javascript">
$(document).ready(function(){
$("#productos").change(function(){
var producto=$(this).val();
$.ajax({
type:"post",
url:"cargaselect.php",
data:"producto="+producto,
success:function(data){
$("#precio").val(data);
}
});
});
});
</script>
cargaselect.php:
<?php
session_start();
include("conexion.php");
if(isset($_SESSION["id_usuario"])){
$producto=$_POST["producto"];
$result = mysqli_query($conexion,
"select * FROM lista_precios WHERE producto =". $producto ." ");
echo $result[precio];
} else {
header("location: login.php");
}
?>
verwenden, wenn '$ producto' eine Zeichenfolge ist, wird dies fehlschlagen' WHERE producto = ". $ Producto. "' - Überprüfen Sie auf Fehler und schauen Sie sich Ihre Konsole an. Stellen Sie außerdem sicher, dass '$ _SESSION [" id_usuario "]' einen Wert hat. –
Warum wird dieser Code fehlschlagen? Empfehlen Sie LIKE anstelle von =? Ich entferne $ _SESSION ["id_usuario"], aber es funktioniert immer noch nicht. –
können Sie sagen, welche Antwort Sie in Ihrer Konsole erhalten? – Qazi