MySQL query with Ajax PHP

0

I need to query my database from a button, and I've had problems with that.

I have a "button" or an "a" or an "input type" and I want to change a record with the "onclick", that is, if I press the button, the "state" field of my table will change. "available" to "busy".

I've been seeing what can be done with ajax which takes up your PHP function but does not work

<input type="submit" onclick="accion();">

<script>
 function accion()
{

$.ajax({
        type:'POST', 
        url: 'boton.php',
        success:function (){

            alert('Bien');
       },
       error:function (){

        alert("mal");
       }
     });

}
  </script>

and this I have in "boton.php"

<?php
  $con=mysqli_connect("localhost","root","","sm");
  mysqli_query($con,"UPDATE Lunes set EstadoL="Ocupado";");

  ?>

I hope you can help me, if I deploy the alert but until then

    
asked by Eduardo Cervantes 10.12.2017 в 08:20
source

2 answers

0

Try changing:

mysqli_query($con,"UPDATE Lunes set EstadoL="Ocupado";");

with:

mysqli_query($con,"UPDATE Lunes set EstadoL='Ocupado';");
    
answered by 10.12.2017 в 08:30
0

print what the request returns, for that you will have to return a message from your file boton.php

this would be your function

   $.ajax({
                  type: 'POST',
                  url: 'boton.php',
                  cache: false,
                  success: function(response, textStatus, XMLHttpRequest){
                    alert(response);
                  },
                  error: function(xhr, type, exception) {
                  alert("error ajax")
                 }
             });

and in your file boton.php returns a message to know if there is an error

$result="";

$con=mysqli_connect("localhost","root","","sm");
// Check connection
if (!$con)
{
   $result="Failed to connect to MySQL ";
}
else{
  // 
  if (mysqli_query($con," UPDATE Lunes set EstadoL='Ocupado';")) {
     $result="Actualizado correctamente";
  } else {
     $result="Error updating record: " . mysqli_error($con);
  }
  mysqli_close($con);
}

echo $result;

Do not forget to include jquery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
answered by 16.11.2018 в 01:23