So, i am pretty new to ajax and i was working with a small project in which i need to make a query from a HTML form, but for some reason i can't send any data. I've tried to send a FormData through the xmlhttprequest.send() method, as well as an array containing values, but the php to which i'm sending the request gets absolutely nothing. Here's the html document
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ajax</title>
<script type="text/javascript">
function pasarXMLATabla(xml){
var empleados = xml.getElementsByTagName('empleado');
var tabla = "<table> <tr> <th>ID</th> <th>Nombres</th> <th>Apellidos</th> <th>Salario</th> </tr>";
for (var i = 0; i < empleados.length; i++) {
tabla += "<tr> <td>"+empleados[i].getElementsByTagName("id")[0].innerHTML+"</td>";
tabla += "<td>"+empleados[i].getElementsByTagName("nombre")[0].innerHTML+"</td>";
tabla += "<td>"+empleados[i].getElementsByTagName("apellidos")[0].innerHTML+"</td>";
tabla += "<td>"+empleados[i].getElementsByTagName("salario")[0].innerHTML+"</td></tr>";
}
tabla += "</table>"
document.getElementById('lista-empleados').innerHTML=tabla;
}
function manejadorDeFormulario(){
var datosDelFormulario = new FormData(document.getElementById('formulario'));
const xhttp = new XMLHttpRequest();
xhttp.onload = function () {
var respuesta = xhttp.responseXML;
pasarXMLATabla(respuesta);
console.log(respuesta.getElementsByTagName('mensaje'));
}
xhttp.open("GET","empleados.php");
xhttp.send(datosDelFormulario);
}
</script>
</head>
<body style="background-color:#9EADAD;">
<form onsubmit="manejadorDeFormulario();return false;" id='formulario' method='get'>
<select id='tipo-de-busqueda' name='tipo-de-busqueda'>
<option value=''>--Seleccione el tipo de búsqueda a realizar--</option>
<option value='1'>Salarios menores</option>
<option value='2'>Salarios iguales</option>
<option value='3'>Salarios mayores</option>
</select>
<input type="text" id='cantidad-de-salario'><br>
<button type="submit">Realizar búsqueda</button>
</form>
<div id="lista-empleados"></div>
</body>
</html>
Here's the php
<?php
require_once("db/query_fns.php");
$empleadosEncontrados = buscarEmpleadosPorSalario();
require_once("views/empleadosxml.php");
function buscarEmpleadosPorSalario(){
$tipoDeBusqueda = $_GET['tipo-de-busqueda'];
$cantidadDeSalario = $_GET['cantidad-de-salario'];
$result = "";
switch ($tipoDeBusqueda) {
case 1:
$sql = "SELECT * FROM empleados WHERE salario < " . $cantidadDeSalario;
$result = getElements($sql);
return $result;
break;
case 2:
$sql = "SELECT * FROM empleados WHERE salario = " . $cantidadDeSalario;
$result = getElements($sql);
return $result;
break;
case 3:
$sql = "SELECT * FROM empleados WHERE salario > " . $cantidadDeSalario;
$result = getElements($sql);
return $result;
break;
}
}
?>
The /views/empleadosxml.php document is where i build an XML from the results of the query, i've tested it giving specific values to $tipoDeBusqueda and $cantidadDeSalario and there's no errors there UNLESS i try to use $_GET. And the /db/query_fns document it's where i make the queries, and since i can generate the xml answer without a problem, then i can safely assume it's working. I have tried to store the $_GET values and then echo print_r them inside a new XML tag to see what am i getting in the $_GET, and it's always an empty array, regardless of if i'm using wether POST or GET. All my testing leads me to the $_GET being empty, since neither the query, XML generation or parsing, connection to server and database has problems, there's only problems when i don't give fixed values to $cantidadDeSalario and $tipoDeBusqueda, and the error i get is "extra content at the end of document". I have been trying all of this in OperaGX and Microsoft Edge with the same results. I can't use jquery for this.