简体   繁体   中英

How to get data to bootstrap 3 modal form using jquery

Im currently working on a project using codeigniter framework and bootstrap 3 sb admin 2 template, I have no experience about JQuery or Ajax before, now I want create an edit form to load and saving data in bootstrap modal just like this http://formvalidation.io/examples/loading-saving-data-modal/ but I keep getting this error on my console view_user:323 Uncaught ReferenceError: $ is not defined and the edit form didn't load at all. Here's and my code :

The CSS I include:

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Home">
<meta name="author" content="MGA">

<title>App | POS</title>

<!--Page Logo-->    
<link rel="icon" type="images/ico" href="<?php echo base_url();?>/assets/images/logo.png">    

<!-- Bootstrap Core CSS -->
<link href="<?php echo base_url();?>/assets/bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom -->
<link href="<?php echo base_url();?>/assets/bower_components/bootstrap/dist/css/mytheme.css" rel="stylesheet">

<!-- MetisMenu CSS -->
<link href="<?php echo base_url();?>/assets/bower_components/metisMenu/dist/metisMenu.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="<?php echo base_url();?>/assets/dist/css/sb-admin-2.css" rel="stylesheet">

<!-- Custom Fonts -->
<link href="<?php echo base_url();?>/assets/fontawesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<!-- DataTables CSS -->
<link href="<?php echo base_url();?>/assets/bower_components/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css" rel="stylesheet">

<!-- DataTables Responsive CSS -->
<link href="<?php echo base_url();?>/assets/bower_components/datatables-responsive/css/dataTables.responsive.css" rel="stylesheet">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

The Code Itself :

    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">Daftar User</h1>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <ul class="nav nav-pills">
                        <li class="active"><a data-toggle="pill" href="#view">Lihat User</a></li>
                        <li><a data-toggle="pill" href="#add">Tambah Baru</a></li>
                    </ul>
                </div>
                <div class="panel-body">
                    <div class="tab-content">
                        <div id="view" class="tab-pane fade in active">
                            <div class="row">
                                <div class="col-lg-12">
                                    <!-- /.panel-heading -->
                                    <div class="dataTable_wrapper">
                                        <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                            <thead>
                                                <tr>
                                                <th>No.</th>
                                                <th>ID</th>
                                                <th>Jabatan</th>
                                                <th>Status</th>
                                                <th>Menu</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $no = 1;
                                                foreach ($session as $row1) {
                                                    if($row1->id == "ADMIN" && $row1->jabatan_id == 1){
                                                        foreach ($user as $row) {
                                                            echo "<tr>
                                                            <td><div style='text-align:right;'>".$no.".</div></td>
                                                            <td >".$row->id."</td>
                                                            <td><div style='text-align:center;'>".$row->namajabatan."</div></td>
                                                            <td>".$row->status."</td>
                                                            <td>
                                                                <div style='text-align:center;'><a class='btn btn-primary btn-circle'><i class='fa fa-info fa-fw'></i></a> <a data-toggle='modal' data-id=".$row->id." class='edit btn btn-warning btn-circle'><i class='fa fa-pencil fa-fw'></i></a></div>
                                                            </td>
                                                            </tr>";
                                                            $no++;
                                                        }
                                                    }else{
                                                        foreach ($user as $row) {
                                                            echo "<tr>
                                                            <td><div style='text-align:right;'>".$no.".</div></td>
                                                            <td>".$row->id."</td>
                                                            <td><div style='text-align:center;'>".$row->namajabatan."</div></td>
                                                            <td>".$row->status."</td>
                                                            <td><div style='text-align:center;'><a class='btn btn-primary btn-circle'><i class='fa fa-info fa-fw'></i></a></div>
                                                            </tr>";
                                                            $no++;
                                                        }
                                                    }
                                                }
                                                ?>

                                            </tbody>
                                        </table>
                                        <!-- /.table-responsive -->
                                    </div>
                                </div>
                            </div>
                        <!-- /.panel-body -->
                        </div>
                        <div id="add" class="tab-pane fade">
                            <?php include 'addnew.php' ?>
                        </div>
                        <?php include 'formedit.php' ?>
                    </div>
                </div>
            </div>
            <!-- /.panel -->
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
</div>
<!-- /#page-wrapper -->

The formedit.php :

<script type="text/javascript">
$(document).on("click", ".open-edit", function () {
    var userID = $(this).data('id');
    $(".modal-body #userID").val( userID );
});
</script>

<div class="modal hide" id="edit_user">
<div class="modal-header">
    <button class="close" data-dismiss="modal">×</button>
    <h3>Modal header</h3>
</div>
<div class="modal-body">
    <p>some content</p>
    <input type="text" name="userID" id="userID" value=""/>
</div>

The Javascripts And JQueries :

    <!-- jQuery -->
<script src="<?php echo base_url();?>/assets/bower_components/jquery/dist/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="<?php echo base_url();?>/assets/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>

<!-- Metis Menu Plugin JavaScript -->
<script src="<?php echo base_url();?>/assets/bower_components/metisMenu/dist/metisMenu.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="<?php echo base_url();?>/assets/dist/js/sb-admin-2.js"></script>

<!-- DataTables JavaScript -->
<script src="<?php echo base_url();?>/assets/bower_components/datatables/media/js/jquery.dataTables.min.js">
</script>
<script src="<?php echo base_url();?>/assets/bower_components/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.min.js"></script>

<!-- Page-Level Demo Scripts - Tables - Use for reference -->
<script type="text/javascript">
    $(document).ready(function() {
        $('#dataTables-example').DataTable({
                responsive: true
        });
    });
</script>

Sorry for my bad english btw :)

IDs should be unique to the page, so it would probably be cleaner to do $(#userID).val(userID) . But that aside...

In terms of error view_user:323 Uncaught ReferenceError: $ is not defined likely your jQuery is not defined. Ensure that your page is loading the jQuery properly (that the file is both there, and has the code in it).

Chrome: Right click -> Inspect -> Sources

Firefox: Right Click -> Inspect Element -> Debugger -> Sources

Internet Explorer: Click -> Inspect Element -> Debugger

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM