Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
PHP para leer el valor de la base de datos, JS para incrementarlo, AJAX para guardar en la base de datos, pero ¿por qué el valor predeterminado es 0.0 antes de actualizar la página?

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>
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!