Good Evening, I'm creating a website for the job and I am having several problems with Ajax. This is a function I'm already using, but in this case, it doesn't work. It gives me an empty value as a result.
This is the script.js
$('#nomeCorso').change(function() {
var tipoCorsi = $('#tipoCorsi').val();
var nomeCorso = $('#nomeCorso').val();
$.ajax({
type: 'post',
//dataType: 'json',
url: '../php/elaborazione_dati.php',
data: {
nomeCorso: nomeCorso,
tipoCorsi: tipoCorsi
},
success: function(data) {
document.getElementById("elencoCorsi").innerHTML = data;
alert(data);
}
});
})
And this elaborazione_dati.php
if ( isset( $_POST[ 'nomeCorso' ] ) && isset( $_POST[ 'tipoCorsi' ] ) ) {
include "connection.php";
$corsi = $_POST[ 'nomeCorso' ];
$tipoCorsi = $_POST[ 'tipoCorsi' ];
$querys = "SELECT * FROM `Corsi` WHERE `Tipo corso` = '" . $corsi . "' AND `Nome del corso`= '" . $tipoCorsi . "'";
$results = mysqli_query( $link, $querys );
// $array = array();
while ( $rows = mysqli_fetch_array( $results ) ) {
echo( "<th scope='row'>" . $rows[ 'Id' ] . "</th>
<td>" . $rows[ 'Tipo corso' ] . "</td>
<td>" . $rows[ 'Nome del corso' ] . "</td>
<td>" . $rows[ 'Descrizione del corso' ] . "</td>
<td>" . $rows[ 'Ore teoria' ] . "</td>
<td>" . $rows[ 'Ore pratica' ] . "l</td>
<td>" . $rows[ 'Ore fad' ] . "</td>
<td>" . $rows[ 'Periodicita' ] . "</td>" );
}
}else {
echo "Error";
}
Where am I doing wrong? I'm going crazy
Thanks for the help
UPDATE 1: The values var tipoCorsi
and var nomeCorso
came from Dynamic dropdown list with PHP, AJAX & MYSQL and if I try alert(tipoCorsi)
or alert(nomeCorso)
the values exist.
I also tried the php function and it correctly reports the while
loop.
Finally I voluntarily tried to give error to the php function and in this case it give me back Error
in alert() Ajax.
UPDATE 2: I changed from $ _POST
to $ _GET
and the script works correctly. How is it possible?