在检索数据时将空格应用于输入框

时间:2018-03-04 15:44:12

标签: javascript php jquery css

我正在用PHP设计一个应用程序并使用javascript来执行添加/编辑/删除。数据正确插入但是当我点击编辑按钮时,显示的数据在输入框的中心位于中心。我尝试使用CSS但没有任何反应。我也无法输入任何一个来纠正错误.Below是代码 的 PHP:

 <!DOCTYPE html>
    <!--[if IE 8]> <html lang="en" class="ie8 no-js"> <![endif]-->
    <!--[if IE 9]> <html lang="en" class="ie9 no-js"> <![endif]-->
    <!--[if !IE]><!-->
    <html lang="en">
    <!--<![endif]-->
    <!-- BEGIN HEAD -->
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />

    <head>
        <?php include_once("header.php"); ?>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" integrity="sha256-p6xU9YulB7E2Ic62/PX+h59ayb3PBJ0WFTEQxq0EjHw=" crossorigin="anonymous" />

    </head>
    <!-- END HEAD -->

    <body class="page-header-fixed page-sidebar-closed-hide-logo page-container-bg-solid page-content-white">

        <div class="page-wrapper">
            <?php include_once("navbar.php"); ?>
            <!-- BEGIN HEADER & CONTENT DIVIDER -->
            <div class="clearfix">
            </div>
            <!-- END HEADER & CONTENT DIVIDER -->
            <!-- BEGIN CONTAINER -->
            <div class="page-container">
                <?php include_once("side_bar.php"); ?>
                <!-- BEGIN CONTENT -->
                <div class="page-content-wrapper">
                    <!-- BEGIN CONTENT BODY -->
                    <div class="page-content">
                        <button class="btn btn-success mt-sweetalert hide" data-title="Sweet Alerts with Icons" data-message="Success Icon" data-type="success" data-allow-outside-click="true" data-confirm-button-class="btn-success" id="msgbox" >Icon Success Alert</button>




                            <button class="btn btn-warning mt-sweetalert hide" data-title="Sweet Alerts with Icons" data-message="Warning Icon" data-type="warning" data-allow-outside-click="true" data-confirm-button-class="btn-warning" id="wmsg">Icon Warning Alert</button>



                         <a class="btn green btn-outline sbold" data-toggle="modal" href="#draggable"> + Add Assessment Type </a>

                        <div class="table-responsive">
                            <table id="users" class="table table-striped table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th style="display:none;">ID</th>
                                        <th onclick="sortTable(1)">Assessment type code</th>
                                        <th onclick="sortTable(2)">Assessment Name</th>
                                        <th colspan="2">Actions</th>
                                    </tr>
                                </thead>
                                <tbody id="table_body">
                                    <tr>
                                        <th style="display:none;">ID</th>
                                        <th>
                                            <input type="text" class="form-control" id="codes" onkeyup="myFunction(this.id, 1)" placeholder="Search for codes.." title="Type in a name">
                                        </th>
                                        <th>
                                            <input type="text" class="form-control" id="names" onkeyup="myFunction(this.id, 2)" placeholder="Search for names.." title="Type in a name">
                                        </th>
                                        <th colspan="2"></th>
                                    </tr>
                                    <?php
                                    include_once PHP_PATH .'/config.php';
                                    $sql = "select * from list_assessment_types";
                                    $result = $conn->query($sql);

                                    while ($row = $result->fetch_assoc()) {
                                        $aid = $row["pk_assess_type_id"];
                                        $code = $row["assess_type_code"];
                                        $name = $row["assess_type_name"];
                                        ?>
                                        <tr id="<?php echo $aid ?>">
                                            <td style="display:none;"></td>
                                            <td>
                                                <?php echo $code ?>
                                            </td>
                                            <td>
                                                <?php echo $name ?>
                                            </td>
                                            <td><a name="edit" class="btn blue btn-outline sbold" data-toggle="modal" href="#draggable1"><i class="fa fa-pencil-square-o fa-1x" aria-hidden="true"></i> Edit</a>
                                            </td>
                                            <td><button name="del" class="btn red btn-outline sbold"><i class="fa fa-trash fa-1x" aria-hidden="true"></i> Delete</button>

                                            </td>
                                        </tr>
                                        <?php
                                    }
                                    db_close();
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <!-- Modal to add assessment type-->
                        <div class="modal fade draggable-modal" id="draggable" tabindex="-1" role="basic" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                                        <h4 class="modal-title">Add new Assessment Type</h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class="form-group">
                                            <label for="code">Assessment type code</label>
                                            <input type="text" name="code" maxlength="15" id="code" value="" class="form-control" />
                                        </div>
                                        <div class="form-group">
                                            <label for="name">Assessment name</label>
                                            <input type="text" name="name" id="name" maxlength="20" value="" class="form-control" />
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button id="new_save" type="button" class="btn green" data-dismiss="modal">Save changes</button>
                                        <button type="button" class="btn dark btn-outline" data-dismiss="modal">Close</button>

                                    </div>
                                </div>
                                <!-- /.modal-content -->
                            </div>
                            <!-- /.modal-dialog -->
                        </div>
                        <!-- Modal finishes here-->

                        <!-- Modal to edit assessment type-->
                        <div class="modal fade draggable-modal" id="draggable1" tabindex="-1" role="basic" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                                        <h4 class="modal-title">Edit Assessment Type</h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class="form-group">
                                            <label for="e_code">Assessment type code</label>
                                            <input type="text" name="e_code" maxlength="15" id="e_code" value="" placeholder="test" class="form-control" />
                                        </div>
                                        <div class="form-group">
                                            <label for="e_name">Assessment name</label>
                                            <input type="text" name="e_name" id="e_name" maxlength="20" value="" placeholder="test" class="form-control" />
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button id="e_save" type="button" class="btn green" data-dismiss="modal">Save changes</button>
                                        <button type="button" class="btn dark btn-outline" data-dismiss="modal">Close</button>

                                    </div>


                                </div>
                                <!-- /.modal-content -->
                            </div>
                            <!-- /.modal-dialog -->
                        </div>
                        <!-- Modal finishes here-->




                    </div>

                    <!-- END QUICK SIDEBAR -->
                </div>


            </div>
            <!-- END CONTAINER -->
            <?php include_once("footer_text.php"); ?>
        </div>

        <div class="quick-nav-overlay"></div>
        <?php include_once("footer.php"); ?>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js" integrity="sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30=" crossorigin="anonymous"></script>

        <script type="text/javascript" src="<?= ASSET_PATH ?>assets/staff_js/assessment_type.js"></script>
        <script>
            function myFunction(tab, s) {
                var input, filter, table, tr, td, i;
                input = document.getElementById(tab);
                filter = input.value.toUpperCase();
                table = document.getElementById("users");
                tr = table.getElementsByTagName("tr");
                for (i = 0; i < tr.length; i++) {
                    td = tr[i].getElementsByTagName("td")[s];
                    if (td) {
                        if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
                            tr[i].style.display = "";
                        } else {
                            tr[i].style.display = "none";
                        }
                    }
                }
            }

        </script>
        <script>
            $("#draggable").draggable({
                handle: ".modal-header"
            });

        </script>
    </body>


    </html>

JS

var x, okay_content, idx;
$(function () {
    var parentId, tr;
    $('#new_save').click(function () {
        var code = $('#code').val();
        var name = $('#name').val();

        $.ajax({
            url: 'class_codes.php',
            type: 'post',
            dataType: 'json',
            data: {
                from: 'insert',
                code: code,
                name: name
            },
            success: function (response) {
                // Check if data is redundant. 
                if (response[0] === "REDUNDANT") {
                    alert("Data already exist.");
                } else if (response[0] === "EMPTY") {
                    alert("Field can't be left empty");

                } else if (response[0] == "1")
                {
                    // Insert or Edit
                    $("#users tbody").append("<tr id=" + response[1] + "><td style='display:none;'><td>" + code + "</td>" + "<td>" + name + "</td>" + "<td><a name= 'edit' href='' class='btn blue btn-outline sbold'><i class='fa fa-pencil-square-o fa-1x' aria-hidden='true'></i> Edit</a></td><td><button name='del' class='btn red btn-outline sbold'><i class='fa fa-trash fa-1x' aria-hidden='true'></i> Delete</button></td></tr>");
                    //sortTable(1);
                    alert("Class Code created successfully.");
                    return false;
                } else {
                    alert("Error: " + response[0]);

                }
            },
            error: function (error) {
                okay_content = error[0];
                $("#okay").dialog("open");
            }
        });

    });

    $(document).on('click', 'a[name="edit"]', function () {
        tr = $(this).parents('tr');

       $('#e_code').val(tr.find("td:eq(1)").text());
        $('#e_name').val(tr.find("td:eq(2)").text());

        parentId = tr.attr('id');
    });

    $(document).on('click', '#e_save', function () {
        var code = $('#e_code').val();
        var name = $('#e_name').val();
        alert("CODE: " + code + " NAME: " + name + " ID: " + parentId);
        $.ajax({
            url: 'assessment_type.php',
            type: 'post',
            dataType: 'json',
            data: {
                from: 'edit',
                id: parentId,
                code: code,
                name: name
            },
            success: function (response) {
                // Check if data is redundant. 
                if (response[0] === "REDUNDANT") {
                    alert("Data already exist.");
                } else if (response[0] === "EMPTY") {
                    alert("Field can't be left empty");
                    // Check if field is empty
//                        okay_content = "Field can't be left empty ";
                } else if (response[0] == "1")
                {
                    // Insert or Edit
                    tr.remove();
                    $("#users tbody").append("<tr>" + "<td style='display:none;'>" + response[1] + "<td>" + code + "</td>" + "<td>" + name + "</td>" + "<td><a href='' class='btn blue btn-outline sbold'><i class='fa fa-pencil-square-o fa-1x' aria-hidden='true'></i> Edit</a></td>" + "<td><button name='del' class='btn red btn-outline sbold'><i class='fa fa-trash fa-1x' aria-hidden='true'></i> Delete</button></td></tr>");
                    //sortTable(1);
                    alert("Class Code edited successfully.");
                    return false;

                } else {
                    alert("Error: " + response[0]);
                }
            },
            error: function (error) {
                okay_content = error[0];
                $("#okay").dialog("open");
            }
        });


    });

    $(document).on('click', 'button[name="del"]', function () {
        var row = $(this).parents('tr');
        var id = row.attr('id');

        $.ajax({
            url: 'class_codes.php',
            type: 'post',
            dataType: 'json',
            data: {
                id: id,
                from: 'delete'
            },
            success: function (suc) {
                if (suc[0] == "1") {
                    alert("Class Code deleted successfully.");
                    $(this).addClass('btn-success ')
                    row.fadeOut(1000);
                }
            },
            error: function (error) {
                alert(error);
            }
        });
    });
});

我不确定我哪里出错了。看起来这个问题在JS中,因为我已经尝试过关闭CSS,但问题仍然存在。请帮助!!!

1 个答案:

答案 0 :(得分:0)

php eho语句应该在1行