Estoy creando una página de objetivo de temperatura donde el formulario leerá el objetivo actual, permitirá al usuario +/- por 0.5 usando los botones de Javascript y luego 'establecerá' el objetivo que toma el nuevo valor y lo guarda en la base de datos.
Tengo una página de trabajo que puede leer el valor DB y configurarlo, pero luego vuelve automáticamente a '0.0' como objetivo. Si la página se actualiza, mostrará el objetivo, pero a veces lo guarda en 0.0 en la base de datos.
Estoy muy confundido en cuanto a por qué, ya que he pasado demasiado tiempo en esto, ¡pero me está molestando! Cualquier ayuda es muy apreciada, gracias.
Aquí está mi código:
<?php $conn = connect DB stuff here... $queryTarget = "SELECT * FROM target;"; $result2 = $conn->query($queryTarget); $conn->close(); if ($result2->num_rows > 0) { while($row = $result2->fetch_assoc()) { $target = $row['target']; } } ?> <form id="input" method="post" action=""> Temp <input type="text" value="<?php echo $target; ?>" name="temp" id="temp" > <input type="button" id="Up" value="up" / > <input type="button" id="Down" value="down"/ > <input type="submit" id="submit" value="submit " name="submit"> </form> <?php $conn = connect DB stuff here... $temp = $_POST['temp']; $updateTarget = "UPDATE target SET target = '"; $updateTarget = $updateTarget . $temp . "' WHERE id = 1;"; $result = $conn->query($updateTarget); $conn->close(); ?> <script> var min = 15, max = 25; $("#Up").click(function(){ if($("#temp").val() < 25 && $("#temp").val() >= 15) $("#temp").val(Number($("#temp").val()) + 0.5); }); $("#Down").click(function(){ if($("#temp").val() <= 25 && $("#temp").val() > 15) $("#temp").val(Number($("#temp").val()) - 0.5); }); </script> <script> $(document).ready(function(){ $('#submit').click(function(){ var srt = $("#input").serialize(); // alert is working perfect alert(srt); $.ajax({ type: 'POST', url: 'form.php', data: srt, success: function(d) { $("#input").html(d); } return false; }); }); }); </script>solo haz eco de este valor temporal
<?php //$conn = connect DB stuff here... $temp = $_POST['temp']; or print_r($_POST); echo $temp; die(); $updateTarget = "UPDATE target SET target = '"; $updateTarget = $updateTarget . $temp . "' WHERE id = 1;";luego verifique la alerta si la alerta muestra los datos correctos y luego use una consulta única como esta
$updateTarget = "UPDATE target SET target = '$temp' WHERE id = 1"; //i think it will help you to find issue $result = $conn->query($updateTarget); $conn->close(); ?>y su función javascript necesita actualizarse
<script> $(document).ready(function(){ $('#submit').click(function(){ var srt = $("#input").serialize(); // alert is working perfect alert(srt); $.ajax({ type: 'POST', url: 'form.php', data: srt, success: function(d) { alert(d); $("#input").html(d); } }); return false; }); }); </script> si encuentra los datos correctos, verifique la estructura de su base de datos, es decir, permite flotar, etc. Creo que funcionará
también puedes comprobar esto
Cómo buscar el valor de la entrada de mysqli en la base de datos