1

I'm having a problem with Ajax. Nothing happen when I change my select value.

I have a div with the id textHint in order to print the result.

Here is my select :

<form>              
    <select id="choix" name="choix" onchange="showUser(this.value)">
        <div class="tutorial_list">
            <?php 
                $db = mysql_connect('localhost', 'root', 'root'); 
                mysql_select_db('Projet',$db); 

                $sql = 'select NomPromo, NumPromo from Promo';
                $req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());

                while ($data = mysql_fetch_array($req)){
                     echo'<option value="'.$data['NumPromo'].'">'.$data['NomPromo'].'</option>';
                }
            ?>
        </div>
    </select>
</form>

Here's my script :

<script>
    function showUser(str) {
        if (str == "") {
           document.getElementById("txtHint").innerHTML = "";
           return;
           } else { 
                if (window.XMLHttpRequest) {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                } else {
                    // code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                    }
                };
                xmlhttp.open("GET","data.php?q="+str,true);
                xmlhttp.send();
        }
    }
</script>

And here's my data.php :

<?php 

    $q = intval($_GET['q']);

    $db = mysql_connect('localhost', 'root', 'root'); 
    mysql_select_db('Projet',$db);

    $sql = "select Nom, Prenom from User where Groupe ='".$q."'";
    $req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());

    while ($data = mysql_fetch_array($req)){
         echo $data['Nom'].' '.$data['Prenom'];
    }
?>  
FlorianSL
  • 89
  • 1
  • 9
  • 2
    `mysql_*` functions are deprecated since PHP 5.5 (and removed entirely in PHP 7) and you should [stop using them](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) if you can. You should choose another API, like `mysqli_*` or PDO instead - see [choosing an API](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Qirel Feb 01 '16 at 13:30
  • Check whether `showUser` method is getting invoked or not on changing value in selectbox – Haridarshan Feb 01 '16 at 13:31
  • Oh yes I will change that when this things work. I'll use mysqli – FlorianSL Feb 01 '16 at 13:33
  • Does it generate any errors? In PHP, check the error_log ([`error_reporting(E_ALL);`](http://php.net/manual/en/function.error-reporting.php), [`ini_set('display_errors', 1);`](http://php.net/manual/en/function.ini-set.php)) or in JavaScript (see Console in your browser). – Qirel Feb 01 '16 at 13:41
  • There is an error : `[Error] TypeError: null is not an object (evaluating 'document.getElementById("txtHint").innerHTML = xmlhttp.responseText') onreadystatechange` on the line `document.getElementById("txtHint").innerHTML = xmlhttp.responseText;` – FlorianSL Feb 01 '16 at 13:47

1 Answers1

2

I don't understand what <div class='tutorial_list'></div> doing inside <select></select>

This error

[Error] ReferenceError: Can't find variable: $ (fonction anonyme)prof.php:75

may be because of few reasons. a) jquery library is not loaded correctly b) path could be not correct. Check this link

I've done little changes, you can try this out.

<form>              
  <select id="choix" name="choix">
    <div class="tutorial_list">
      <?php 
      $db = mysql_connect('localhost', 'root', 'root'); 
      mysql_select_db('Projet',$db); 

      $sql = 'select NomPromo, NumPromo from Promo';
      $req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());

      while ($data = mysql_fetch_array($req)){
           echo'<option value="'.$data['NumPromo'].'">'.$data['NomPromo'].'</option>';
      }
      ?>
    </div>
  </select>
</form>

JS

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<script>
  $(document).ready(function(){
    $('#choix').change(function(){
      var q= $('#choix').val();
      $.ajax({url:"data.php?q="+q,cache:false,success:function(result){
        $('#txtHint').html(result);
      }});
    });
  });
</script>

[NOTE: mysql_* functions are deprecated since PHP 5.5. Use mysqli_* or PDO]

Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • It don't works there is the error `$('#choix').change(function(){` he can't find the variable $ – FlorianSL Feb 01 '16 at 13:52
  • "[Error] ReferenceError: Can't find variable: $ (fonction anonyme)prof.php:75" – FlorianSL Feb 01 '16 at 13:54
  • On the following line `$('#choix').change(function(){` – FlorianSL Feb 01 '16 at 13:55
  • 1
    I have updated my answer. Please go through it @FlorianSL – Nana Partykar Feb 01 '16 at 14:04
  • I've put this at the top and the document ready function but nothing happen when I change the value – FlorianSL Feb 01 '16 at 14:08
  • and the div tutorial_list is useless i've deleted this. And the path to data.php is correct – FlorianSL Feb 01 '16 at 14:09
  • I have used all your code. Nothing happen, and no errors... When I change the value it load data.php but print nothing – FlorianSL Feb 01 '16 at 14:12
  • 1
    Can you do one thing for me. For a time being in **data.php** page. Remove all code. and just `` and see what happens. You are close to it. @FlorianSL – Nana Partykar Feb 01 '16 at 14:14
  • Nothing happen.. Thank for your help. I really don't know why it don't work. When i use the developper tool on safari I can see that when i change the value it load data.php – FlorianSL Feb 01 '16 at 14:18
  • My data.php need to return value? Or he just can print the result? @Nana – FlorianSL Feb 01 '16 at 14:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102255/discussion-between-floriansl-and-nana-partykar). – FlorianSL Feb 01 '16 at 14:34
  • 1
    When i go in the development tool and I press on data.php, it tells me that there is the value that I want to print. So my data.php works, there is an error for print this – FlorianSL Feb 01 '16 at 14:40