I have a parameter that I send to a javascript function, when I make an alert of that parameter it shows me its value, there everything is correct. The problem is that I want to send this parameter via Ajax to a php file, but I think it is not doing well because with that value I make an sql query and load a select but it does not do anything.
The value of the select was sent to the function as follows:
<select id="tipo" onchange="cargarCategoria(this.value)">
AJAX Code
function cargarCategoria(dato)
{
alert(dato)//Me muestra su valor correctamente
$.ajax({
data: dato, //Esto es lo que envio a php, que es un elemento de un select
type: "POST",
url: '../consultas/consultarCategoria.php',
success: function(resp){
$('#categoria').html(resp);
}
});
}
PHP Code
$valor = $_POST["dato"];
echo '<script>alert ("DATO: '.$valor.'");</script>';
$tipo = mysqli_real_escape_string($con,$_POST['dato']);
$sql = "SELECT DISTINCT categoria FROM formacion WHERE tipo = ".$tipo;
$result = mysqli_query($con, $query);
while($fila = mysqli_fetch_array($result))
{
echo '<option value="'.$fila["categoria"].'">'.$fila["categoria"].'</option>';
}
How can I know if I'm sending the variable by ajax and how can I handle it in PHP?