PHP Ajax MySQL - 无法从Bootstrap模式中插入数据

时间:2018-04-01 09:37:06

标签: php jquery ajax mysqli

我目前正在为管理员面板开发基本的CRUD应用程序。 我正在尝试使用Bootstrap Modal,AJAX和PHP将数据添加到MySQL数据库。 这里的问题是我无法使用模态插入数据,甚至没有显示错误警报。我无法看到我所缺少的东西。谢谢。

以下是查看管理员的代码:

<?php
    if(!isset($_SESSION['admin_email']))
    {
        echo "<script>window.open('login.php','_self')</script>";
    }
    else {
?>

<div class="row">
    <div class="col-lg-12" >
        <!-- FREO ADMINISTRATOR BREADCRUMB AND TITLE - START -->
        <h1 class="page-header"><i class="fa fa-fw fa-trophy" ></i>Administrators</h1>
        <ol class="breadcrumb" >
            <li class="active" >
                <i class="fa fa-dashboard" ></i> Dashboard > Administrators
            </li>
        </ol>
        <!-- FREO ADMINISTRATOR BREADCRUMB AND TITLE - END -->
        <!-- FREO ADMINISTRATOR SEARCH - START -->
        <form class="form-search" action="" method="POST">
            <div class="row">
                <div class="col-md-1">
                    <h4><strong>SEARCH: </strong></h4>
                </div>
                <div class="col-md-7">
                    <input type="text" class="form-control" name="admin_info" placeholder="Enter Administrator Information" required>
                </div>
                <div class="col-md-2">
                    <button class="btn btn-sm btn-primary btn-block" type="submit" name="btnSearch" >
                        Search
                    </button>
                </div>
            </div>               
        </form>
        <!-- FREO ADMINISTRATOR SEARCH - END -->
    </div>
</div>
<br>
<!-- FREO ADMINISTRATOR ADD AND EXPORT - START -->
<div class="row">
    <div class="col-md-12">
        <button type="button" data-toggle="modal" data-target="#add_data_Modal" class="btn btn-success">
            Add Administrator
        </button>
        <button type="button" class="btn btn-danger">
            Export to PDF
        </button>
    </div>
</div>
<!-- FREO ADMINISTRATOR ADD AND EXPORT - END-->
<br>
<div class="row" >
    <div class="col-lg-12" >
        <div class="panel panel-default" >
            <div class="panel-heading" >
                <h3 class="panel-title" >
                     <strong>ADMINISTRATORS</strong>
                </h3>
            </div>
            <div class="panel-body">
                <div class="table-responsive">
                    <div id="employee_table">
                        <table class="table table-bordered table-hover table-striped">
                            <thead>
                                <tr>
                                        <th>Administrator First Name:</th>
                                        <th>Administrator Last Name:</th>
                                        <th>Administrator Address:</th>                                    
                                        <th>Administrator Email:</th>
                                        <th>Administrator Password:</th>
                                        <th>Administrator Contact:</th>
                                        <th>Administrator Gender:</th>
                                        <th>Administrator Role:</th>
                                        <th>Action:</th>
                                </tr>
                            </thead>
                            <tbody>
                                <!-- FREO FETCH ALL ADMINISTRATORS - START -->
                                <?php
                                    $preparedStatement = $dbConnection->prepare("SELECT * 
                                                                                 FROM administrator");    
                                    $preparedStatement->execute();
                                    $preparedStatement->bind_result($adminId,
                                                                    $adminFName,
                                                                    $adminLName,
                                                                    $adminAddress,
                                                                    $adminEmail,
                                                                    $adminPassword,
                                                                    $adminContact,
                                                                    $adminGender,
                                                                    $adminRole);
                                    while($preparedStatement->fetch())
                                    {
                                ?>             
                                <tr>
                                    <td><?php echo $adminFName; ?></td>
                                    <td><?php echo $adminLName; ?></td>
                                    <td><?php echo $adminAddress ?></td>
                                    <td><?php echo $adminEmail; ?></td> 
                                    <td><?php echo $adminPassword; ?></td>  
                                    <td><?php echo $adminContact; ?></td>       
                                    <td><?php echo $adminGender; ?></td>
                                    <td><?php echo $adminRole; ?></td>  
                                    <td>
                                        <a href="index.php?user_delete=<?php echo $admin_id; ?>" >
                                            <i class="fa fa-trash-o" ></i> Remove
                                        </a>
                                    </td>
                                </tr>
                                <?php } ?>
                                <!-- FREO FETCH ALL ADMINISTRATORS - END -->
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- FREO ADD ADMINISTRATOR MODAL - START -->
<div id="add_data_Modal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><strong>Add Freo Administrator</strong></h4>
            </div>
            <div class="modal-body">
                <form method="post" id="insert_form">
                    <label>Administrator First Name: </label>
                    <input type="text" name="firstname" id="name" class="form-control" />
                    <br/>
                    <label>Administrator Last Name: </label>
                    <input type="text" name="lastname" id="name" class="form-control" />
                    <br/>
                    <label>Administrator Address: </label>
                    <textarea name="text" id="address" class="form-control"></textarea>
                    <br/>
                    <label>Administrator Email: </label>
                    <input type="text" name="email" id="email" class="form-control">
                    <br/>
                    <label>Administrator Password: </label>
                    <input type="password" name="password" id="password" class="form-control">
                    <br/>
                    <label>Administrator Contact: </label>
                    <input type="text" name="contact" id="contact" class="form-control">
                    <br/>
                    <label>Gender: </label>
                    <select name="gender" id="gender" class="form-control">
                        <option value="Male">Male</option>  
                        <option value="Female">Female</option>
                    </select>
                    <br>
                    <label>Role: </label>
                    <select name="role" id="role" class="form-control">
                        <option value="Super">Super Administrator</option>  
                        <option value="Payment">Payment Administrator</option>
                    </select>
                    <br/>
                    <center>
                        <input type="submit" name="insert" id="insert" value="Add Administrator" class="btn btn-success"/>              
                    </center>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<!-- FREO ADD ADMINISTRATOR MODAL - END -->
<?php }  ?>

<script>  
    $(document).ready(function(){
        $('#insert_form').on("submit", function(event){  
            event.preventDefault();
            // CHECK IF DETAILS ARE EMPTY - START
            if($('#firstname').val() == "")  
            {  
                alert("Name is required");  
            }  
            else if($('#lastname').val() == "")  
            {  
                alert("Name is required");  
            }  
            else if($('#address').val() == '')  
            {  
                alert("Address is required");  
            }  
            else if($('#email').val() == '')
            {  
                alert("Email address is required");  
            }
            else if($('#password').val() == '')
            {  
                alert("Password is required");  
            }
            else if($('#contact').val() == '')
            {  
                alert("Contact Number is required");  
            }
            else if($('#gender').val() == '')
            {  
                alert("Gender is required");  
            }
            else if($('#role').val() == '')
            {  
                alert("Role is required");  
            }
            // CHECK IF DETAILS ARE EMPTY - END

            else  
            {  
                $.ajax({  
                    url:"AddAdministrator.php",  
                    method:"POST",  
                    data:$('#insert_form').serialize(),  
                    beforeSend:function(){  
                    $('#insert').val("Inserting");  
                    },  
                    success:function(data){  
                    $('#insert_form')[0].reset();  
                    $('#add_data_Modal').modal('hide');  
                    $('#employee_table').html(data);  
                    }  
            });  
        }  
    });
 </script>

以下是添加管理员的代码(AddAdministrator.php):

<?php
    if(!empty($_POST))
    {
            $adminResults = '';

            // DEFINE VARIABLES FOR DATA INSERTION - START
            $adminFName = $_POST['firstname'];
            $adminLName = $_POST['lastname'];
            $adminAddress = $_POST['address'];
            $adminEmail = $_POST['email'];
            $adminPassword = $_POST['password'];
            $adminContact = $_POST['contact'];
            $adminGender = $_POST['gender'];
            $adminRole = $_POST['role'];
            // DEFINE VARIABLES FOR DATA INSERTION - END
            // BIND ALL VARIABLES FOR INSERTION - START
            $preparedStatement = $dbConnection->prepare("INSERT INTO administrator 
                                                         VALUES (?, ?, ?, ?, ? ,? ,? ,?)");      
            $preparedStatement->bind_param('ssssssss', $adminFName,
                                                       $adminLName,
                                                       $adminAddress,
                                                       $adminEmail,
                                                       $adminPassword,
                                                       $adminContact,
                                                       $adminGender,
                                                       $adminRole); 
            // BIND ALL VARIABLES FOR INSERTION - END

            $preparedStatement->execute();
            $countAdmin = $preparedStatement->affected_rows;

            if($countAdmin == 1)
            {
                $adminResult.= '<label class="text-success">Freo Administrator Added</label>';
                $preparedStatement = $dbConnection->prepare("SELECT * 
                                                             FROM administrator");    
                $preparedStatement->execute();
                $preparedStatement->bind_result($dsplyAdminId,
                                                $dsplyAdminFName,
                                                $dsplyAdminLName,
                                                $dsplyAdminAddress,
                                                $dsplyAdminEmail,
                                                $dsplyAdminPassword,
                                                $dsplyAdminContact,
                                                $dsplyAdminGender,
                                                $dsplyAdminRole);

                $adminResult .= '<table class="table table-bordered table-hover table-striped">
                                    <thead>
                                        <tr>
                                                <th>Administrator First Name:</th>
                                                <th>Administrator Last Name:</th>
                                                <th>Administrator Address:</th>                                    
                                                <th>Administrator Email:</th>
                                                <th>Administrator Password:</th>
                                                <th>Administrator Contact:</th>
                                                <th>Administrator Gender:</th>
                                                <th>Administrator Role:</th>
                                                <th>Action:</th>
                                        </tr>
                                    </thead>';
                while($preparedStatement->fetch()))
                {
                $adminResult .= ' <tbody>   
                <tr>
                    <td><?$dsplyAdminFName</td>
                    <td>$dsplyAdminLName</td>
                    <td><$dsplyAdminAddres</td>
                    <td><$dsplyAdminEmail</td> 
                    <td>$dsplyAdminPassword</td>  
                    <td>$dsplyAdminContact</td>       
                    <td>$dsplyAdminGender</td>
                    <td>$dsplyAdminRole</td>  
                    <td>
                        <a href="index.php?user_delete=<?php echo $admin_id; ?>" >
                            <i class="fa fa-trash-o" ></i> Remove
                        </a>
                    </td>
                </tr>
                <?php } ?>
            </tbody>';
                }
                $adminResult .= '</table>';
            }
        echo $output;
    }?>

0 个答案:

没有答案