dongpi3237 2017-03-24 02:22
浏览 25

按模式查看SQL数据

Hello I'm not quite sure where i have gone wrong with this code i am very new to ajax and i have half an idea about php. I have looked at other questions but everyones doing it in different ways then i am.

I am calling data via ajax to modal from SQL

index.php

    <?php
    $connect = mysqli_connect("localhost","root","","testing");
    $query = "SELECT * FROM employee";
    $result = mysqli_query($connect, $query);
    ?>

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>  </title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    </head>
    <body>

    <div class="container" style="width:700px;">
    <h3 align="center">Employee Onboarding</h3>
    <br>
    <div class="table-responsive">

    <table class="table table-bordered">
    <tr> <th width="70%">Employee Name</th> <th width="30%">View</th> </tr>

    <?php
    while($row = mysqli_fetch_array($result))
    { 
    ?>

    <tr> <td> <?php echo $row['Name']; ?> </td> <td> <input type="button" name="view" value="view" id="<?php echo $row['EmployeeID']; ?>" class="btn btn-info btn-xs view_data"> </td> </tr>

    <?php
    }
    ?>



    </table>

    </div>
    </div>

    </body>
    </html>


    <!-- Modal -->
    <div id="dataModal" 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">Employee Details</h4>
    </div>

    <div class="modal-body" id="employee_detail">

    </div>

    <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">close</button>
    </div>

    </div>
    </div>
    </div>
    <!-- Modal -->



    <script>
    $(document).ready(function(){
    $('.view_data').click(function(){
    var employee_id = $(this).attr("EmployeeID");

    $.ajax({
    url:"modal.php",
    method:"POST",
    data:{employee_id:employee_id},
    success:function(data){
    $('#employee_detail').html(data);
    $('#dataModal').modal("show");
    }
    });

    });
    });
    </script>

modal.php

    <?php
    if(isset($_POST['employee_id']))
    {
    $output = '';
    $connect = mysqli_connect("localhost","root","","testing");
    $query = "SELECT * FROM employee WHERE EmployeeID = '".$_POST["employee_id"]."'";
    $result = mysqli_query($connect, $query);

    $output .= '
    <div class="table-responsive">
    <table class="table table-bordered">';
    while($row = mysqli_fetch_array($result))
    {
    $output .= '
    <tr> <td width="30%"> <label> Name </label> </td> <td width="70%"> '.$row['Name'].' </td> </tr>
    <tr> <td width="30%"> <label> Adress </label> </td> <td width="70%"> '.$row['Address'].' </td> </tr>
    <tr> <td width="30%"> <label> Gender </label> </td> <td width="70%"> '.$row['Gender'].' </td> </tr>
    <tr> <td width="30%"> <label> Position </label> </td> <td width="70%"> '.$row['Position'].' </td> </tr>
    <tr> <td width="30%"> <label> Age </label> </td> <td width="70%"> '.$row['Age'].' </td> </tr>
    <tr> <td width="30%"> <label> Added Time </label> </td> <td width="70%"> '.$row['AddedTime'].' </td> </tr>
    ';
    }
    $output .= "</table></div>";
    echo $output;
    }
    ?>

Live/Testing Site

  • 写回答

1条回答 默认 最新

  • douguizhuang8276 2017-03-24 02:42
    关注

    You should also post your ajax! You should append the result of your modal.php into the actual modal.

    modal.php

        $output .= '
    <div class="table-responsive">
    <table class="table table-bordered">';
    while($row = mysqli_fetch_array($result))
    {
    $output .= '
    <tr> <td width="30%"> <label> Name </label> </td> <td width="70%"> '.$row['Name'].' </td> </tr>
    <tr> <td width="30%"> <label> Adress </label> </td> <td width="70%"> '.$row['Address'].' </td> </tr>
    <tr> <td width="30%"> <label> Gender </label> </td> <td width="70%"> '.$row['Gender'].' </td> </tr>
    <tr> <td width="30%"> <label> Position </label> </td> <td width="70%"> '.$row['Position'].' </td> </tr>
    <tr> <td width="30%"> <label> Age </label> </td> <td width="70%"> '.$row['Age'].' </td> </tr>
    <tr> <td width="30%"> <label> Added Time </label> </td> <td width="70%"> '.$row['AddedTime'].' </td> </tr>
    ';
    }
    $output .= "</table></div>";
    echo $output;
    }
    

    In your ajax. Assuming you use jquery:

    $.ajax({
         type: 'POST',
         url : 'modal.php',
         cache: false,
         success: function(data){
                $('#target').html(data);
          }
    });
    

    HTML

    <div class="modal">
         <div id="target"></div>
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?