FlorianSL
FlorianSL

Reputation: 89

jQuery Combobox and database problems

I'm having a problem with my jquery and combobox. I would like that when I press my button the jQuery uses the selected value of my combobox and finds something in my database. Nothing happens so far.

Here is my code. I tried a lot of solution that I found on Google but nothing works.

HTML :

<select id="choix" name="choix">
    <?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>';
        }
    ?>  
</select>
<a class="btn btn-primary" id="find" name="find" value="find" data-role="button" type="find">Chercher</a>

jQuery :

<script type="text/javascript">
    $(document).ready(function(){
        $("#find").click(function() {

            <?php

                $db = mysql_connect('localhost', 'root', 'root'); 
                mysql_select_db('Projet',$db); 
                $promo = ('#choix option:selected').val();

                $sql = "select Nom from User where Groupe='".$promo."'";

                echo '<div class="row">';
                while ($data = mysql_fetch_array($req)){
                    echo $data['Nom'];
                }
                echo'<div class="row">';

            ?>

        });
    });
</script>

Upvotes: 3

Views: 113

Answers (1)

Asad Ali
Asad Ali

Reputation: 111

use jquery inside javascript - not php and serve php to jquery code

<script type="text/javascript">
$(document).ready(function(){
    $("#find").click(function() {
        $.ajax({
          type:'POST',
          url:'data.php',
          data:'id='+ID,
          success:function(html){
            $('.tutorial_list').append(html);}
        }); 
    });
});

and in your data.php you can use

<?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>';
    }
?>  

Upvotes: 1

Related Questions