0

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 whileloop. 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?

  • Do you have some sample code that is working that you can compare for differences to the code that isn't? If the function hasn't changed but behavior is, then it must be somehow different in the source data. – Rodger Mar 04 '20 at 00:51
  • In order for AJAX to return anything you must `echo` the response from PHP. You're not echoing anything, save for maybe some empty table cells. – Jay Blanchard Mar 04 '20 at 01:00
  • `It gives me an empty value as a result` - what does that mean exactly? Be specific. `it correctly reports the while loop` - also don't understand what this means? Your browser's devtools will show you exactly what happens. Check the network tab, click the POST request - are the right `data` variables sent? Check the response, did you get back some HTML? Side note - you are using jQuery, so the vanilla JS in your `success` callback is out of place. KISS, be consistent - `$('#elencoCorsi').html(data);` is the equivalent in jQuery. – Don't Panic Mar 04 '20 at 09:12
  • `It gives me an empty value as a result` : in Ajax function `alert(data)` return empty,no data from echo in PHP. I try `while loop` with manual querys and print `echo` in elaborazione_dati.php. Finally the data variables are sent right, for example `nomeCorso=FORMAZIONE+DIRIGENTI&tipoCorsi=FAD` . – Giravite 93 Mar 04 '20 at 11:54
  • Glad you solved it, and I can't see why GET/POST would make any difference. However - **your code is wide open to SQL injection**. Please stop everything and fix that now - [here's the reference SO question about it](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php/60496#60496) with examples. – Don't Panic Mar 04 '20 at 22:23

0 Answers0