为什么按钮仅适用于第一行?

时间:2015-04-28 19:03:18

标签: php jquery html

HTML:

<?php
            $username = "trainerapp";
            $password = "password";
            $hostname = "localhost";
            $link = @mysql_connect($hostname, $username, $password);

            if(@mysql_select_db("trainer_registration"))
            { 
                $check_query = @mysql_query("select id,program_name, company, date_prog from program_details");
                while($row = @mysql_fetch_assoc($check_query)){ 

                        # code...
                    ?>
                <div class = "container content-rows">
                    <div class = "col-md-1" id = "snocontent"><?php echo $row['id'] ?></div>
                    <div class = "col-md-3" id = "pgnamecontent"><?php echo $row['program_name'] ?></div>
                    <div class = "col-md-3" id = "cmpcontent"><?php echo $row['company'] ?></div>
                    <div class = "col-md-2" id = "datecontent"><?php echo $row['date_prog'] ?></div>
                    <button onclick="add_trainers()" ><span class = "glyphicon glyphicon-king" class = "addtrainersbutton" id ="addtrainersbutton" title = "Add Trainers" ></span></button>
                    <span class = "glyphicon glyphicon-pencil" id = "editprogrambutton" title = "Edit Program"></span>
                    <span class = "glyphicon glyphicon-user" id = "assigntrainersbutton" title = "Assign Trainers for the Program"></span>
                    <span class = "glyphicon glyphicon-remove" id = "deleteprogrambutton" title = "Delete the Program"></span>



                </div>
            <?php       }}
             else {

            }


        ?>
        <div class = "addtrainers" id = "addtrainersblock" hidden = "true">
                        <?php
                                $email_query = @mysql_query("select email_id from facilitator_details");
                                $check_query = @mysql_query("select firstname,lastname from facilitator_details"); 
                                while($row = @mysql_fetch_assoc($check_query)) { ?>
                                <input type = "checkbox" name = "trainernames"id = "checkboxtrain"  value ="<?php echo $row["firstname"]." ".$row["lastname"] ?>" name = "trainerbox">
                                <?php echo $row["firstname"]." ".$row["lastname"] ?> </input></br>
                                <?php   }  ?>
                                <button class="btn btn-default" id = "sendemail">Send Email</button>
                    </div>
                    <div class = "editprogramdetails" id = "editprogramblock" hidden = "true">
                        <form class="form" action="#" id="update" method ="POST">
                        <?php

                            $programid_query = @mysql_query("select id,program_name,company,date_prog from program_details");
                            $row = @mysql_fetch_assoc($programid_query);
                            ?>
                            ID: <input type = "text" id = "programnum" value = "<?php echo $row["id"] ?>" readonly/><br>
                            Program Name: <input type = "text" id = "prognameedit" placeholder = "<?php echo $row["program_name"] ?>"/><br>
                            Company Name: <input type = "text" id = "compnameedit" placeholder = "<?php echo $row["company"] ?>"/><br>
                            Date: <input type = "date" id = "dateedit" placeholder = "<?php echo $row["date_prog"] ?>"/><br>

                            <input type="button" class = "btn btn-default" id = "updatebutton" value ="Update"></input>
                        </form>
                    </div>

脚本文件位于同一个php文件中: 脚本文件:

<script>
        $(document).ready(function() {

                $("#addtrainersbutton").click(function(){
                $("#addtrainersblock").toggle();
                $("#editprogramblock").hide();
                });

                $("#editprogrambutton").click(function(){
                $("#editprogramblock").toggle();
                $("#addtrainersblock").hide();
                });


    $("#updatebutton").click(function(){
        var programidphp = $("#programnum").val();
        var programnamephp = $("#prognameedit").val();
        var companynamephp = $("#compnameedit").val();
        var datephp = $("#dateedit").val();

        var updaterequest = {
            upprogid : programidphp,
            upprognam : programnamephp,
            upcompnam : companynamephp,
            uppdate : datephp,
        };
        $.post("/TrainerApp/update_program.php", updaterequest).done(function(data){
            alert(data);
        }).fail(function(){
            alert("Failed");
        });

    });

    $("#sendemail").click(function(){
    $('input[name="trainernames"]:checked').each(function() {
        var f_name=this.value.split(" ")[0];
        var l_name=this.value.split(" ")[1];
        console.log("fname:",f_name);
        console.log("lname:",l_name);

        var trainee_list = [{
            first_name : f_name,
            last_name: l_name
        }];
        console.log(trainee_list);
    });
    });
});
function add_trainers() {
                console.log("in add trainers function");
                $("#addtrainersblock").toggle();
                $("#editprogramblock").hide();
            }
        </script>

问题:

按钮&#34; addtrainersbutton&#34;,&#34; editprogrambutton&#34;仅显示mysql_fetch_assoc返回的第一行值所需的已获取内容。对于其他行,按钮不起作用。请查看代码,让我知道这里做了什么错误。

1 个答案:

答案 0 :(得分:4)

您有重复的ID,但这不起作用。将它们更改为类以使它们工作。例如改变

$("#addtrainersbutton").click(function(){

$(".addtrainersbutton").click(function(){

完成后,您必须相应地修改HTML。

相关问题