使用$ .ajax发送数据的问题

时间:2014-12-21 12:00:42

标签: javascript php jquery html ajax

我在PHP代码中使用$.ajax()...遇到了这个恼人的问题。我无法检索ajax函数发送的数据。

<?php


if ($_POST){
    include 'Bdd_connexion.php';

    $filiere = $_POST['filiere'];
    /*$annee = $_POST['annee'];
    $groupe = $_POST['groupe'];*/

    $sql = "";
    $query = $Conn->prepare("SELECT * FROM `Etudiant` WHERE `filiere` = '".$filiere."'");
    $result = $query->execute();
        if ($result)
            echo "success";

    $results = $query->fetchAll( PDO::FETCH_ASSOC );
        foreach($results as $rows)
            echo $rows['Nom'];  

}else{

    echo "No post";
}?>

<head>
    <title> La liste des presences </title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link rel="stylesheet" href="Liste_des_presences_css.css"/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/> 

    <script language="javascript" type="text/javascript">
    var filiere; var annee; var groupe;
    fil = false;
    ann = false;
    grp = false; 


        function getEventTarget(e) {
                  e = e || window.event;
                    return e.target || e.srcElement; 
                }

        function clickFil(event) {
            var target = getEventTarget(event);
            if(target.innerHTML != "Selectionner un choix"){
                filiere = target.innerHTML;
                fil = true;
                return true;
            }else{
            fil = false;
            return false;
                }
            };

            function clickAn(event) {
            var target = getEventTarget(event);
            if(target.innerHTML != "Selectionner un choix"){
                annee = target.innerHTML;
                ann = true;
                return true;
            }else{
            ann = false;
            return false;
                }
            };

            function clickGrp(event) {
            var target = getEventTarget(event);
            if(target.innerHTML != "Selectionner un choix"){
                groupe = target.innerHTML;
                grp = true;
                return true;
            }else{
            grp = false;
            return false;
                }
            };

            function TestEntries(){
                if (!fil || !ann || !grp)
                    alert("Selectionner tout les champs !");
            }

            function ajax(event) {
            if ('preventDefault' in event) event.preventDefault();
                event.returnValue = false;

            $.ajax({

            url: 'http://127.0.0.1:8080/ProjetWeb/Liste_des_presences.php',
            type: 'POST',
            data: { filiere : filiere 
                , groupe : groupe
                , annee : annee}, 
            contentType: "application/x-www-form-urlencoded",
            cache : false,

                });         

            }

    </script>
</head>

    <body>

<div class="container size" style="margin-top:40px">

          <div class="panel-body">

            <form action="Liste_des_presences.php" class="form-group" method="POST">

                <div class="panel panel-default">
                    <div class="panel-heading">
                    </div>
                        <div class="panel-body">
                            <div class="btn-group division11">
                            <button type="button" class="btn btn-default btn-lg dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                            Filiere <span class="caret"></span>
                            </button>
                                 <ul class="dropdown-menu" onclick="clickFil(event);">
                                   <li><a value="select">Selectionner un choix</a></li>
                                   <li><a value="IL">IL</a></li>
                                   <li><a value="RSD">RSD</a></li>
                                   <li><a value="SSI">SSI</a></li>
                                   <li class="divider"></li>
                                   <li><a value="SII">SII</a></li>
                                   <li><a value="APCI">APCI</a></li>
                                   <li><a value="MIND">MIND</a></li>
                                 </ul>
                            </div>


                        <div class="input-group input-group-lg division3">
                            <button type="button" class="btn btn-default btn-lg dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                            Annee <span class="caret"></span>
                            </button>
                                 <ul class="dropdown-menu" onclick="clickAn(event);">
                                   <li><a>Selectionner un choix</a></li>
                                   <li><a>1</a></li>
                                   <li><a>2</a></li>
                                 </ul>
                        </div>
                        </div>

                        <div class="input-group input-group-lg division4">
                            <button type="button" class="btn btn-default btn-lg dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                            Groupe <span class="caret"></span>
                            </button>
                                 <ul class="dropdown-menu" onclick="clickGrp(event);">
                                   <li><a>Selectionner un choix</a></li>
                                   <li><a>1</a></li>
                                   <li><a>2</a></li>
                                   <li><a>3</a></li>
                                   <li><a>4</a></li>
                                   <li><a>5</a></li>
                                   <li><a>6</a></li>
                                   <li><a>7</a></li>
                                 </ul>
                        </div>          
                        <center>
                            <div class="input-group input-group-lg padd">
                            <input type="submit" class="btn btn-lg btn-primary btn-block" value="Confirmer" onclick="TestEntries();" onfocus="ajax(event);"/>
                            </div>
                        </center>   

                        </div>

                        <form  action="" class="form-group" method="POST">
                            <div><hr/></div>
                                <table class="table table-bordered">
                                    <thead>
                                        <tr> 
                                            <th>Nom</th>
                                            <th>Prenom</th>
                                            <th>Presence</th>

                                        </tr>
                                    </thead>
                                    <tbody>
                                    <?php 
                                        if ($_POST) foreach($results as $rows){
                                            echo '<tr>';
                                                echo '<td>'. $rows['Nom'].'</td>';
                                                echo '<td>'. $rows['Prenom'].'</td>';
                                                echo '<td></td>';
                                            echo '</tr>';
                                        }

                                    ?>
                                    </tbody>                                    
                                </table>



                          </form>

                    </form> 





                        </div>

                    </div>  


        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type=""></script>
        <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js" type=""></script>

    </body>

</html>

请...我会感激任何帮助......谢谢。

1 个答案:

答案 0 :(得分:-1)

 $.ajax({
                        type: "POST",
                        url: "count.php",
                        data: dataString,
                        cache: false,
                        success: function(html){
                            $(".deletefasl").html(html);
                        }
                    });

您的数据字符串应为

 var dataString = 'varibale='+ id;

或用户$ .post

$.post("filename.php", { var in php : var in jquery or javascript},
                         function(result){
}
});