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'];
    }
?>  
5
  • 2
    mysql_* functions are deprecated since PHP 5.5 (and removed entirely in PHP 7) and you should stop using them if you can. You should choose another API, like mysqli_* or PDO instead - see choosing an API. Commented Feb 1, 2016 at 13:30
  • Check whether showUser method is getting invoked or not on changing value in selectbox Commented Feb 1, 2016 at 13:31
  • Oh yes I will change that when this things work. I'll use mysqli Commented Feb 1, 2016 at 13:33
  • Does it generate any errors? In PHP, check the error_log (error_reporting(E_ALL);, ini_set('display_errors', 1);) or in JavaScript (see Console in your browser). Commented Feb 1, 2016 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; Commented Feb 1, 2016 at 13:47

1 Answer 1

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]

Sign up to request clarification or add additional context in comments.

12 Comments

It don't works there is the error $('#choix').change(function(){ he can't find the variable $
"[Error] ReferenceError: Can't find variable: $ (fonction anonyme)prof.php:75"
I have updated my answer. Please go through it @FlorianSL
Can you do one thing for me. For a time being in data.php page. Remove all code. and just <?php echo "asd";?> and see what happens. You are close to it. @FlorianSL
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.