按模式查看SQL数据

时间:2017-03-24 02:22:40

标签: php jquery ajax

您好我不太确定我在这个代码出错的地方我对ajax很新,我对php有一半的想法。 我已经看过其他问题,但是每个人都以不同的方式做到这一点。

我通过ajax从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 个答案:

答案 0 :(得分:0)

你也应该发布你的ajax!您应该将modal.php的结果附加到实际模态中。

<强> modal.php

count % 2 ? str += '*' : str = '*' + str;

在你的ajax中。假设你使用jquery:

from dateutil.parser import parse
# parse the date str, and return day/month/year specified in the string.
# the value is None if the string does not have information
def parse_date(date_str):
    # choose two different dates and see if two parsed results
    default_date1 = datetime.datetime(1900, 1, 1, 0, 0)
    default_date2 = datetime.datetime(1901, 12, 12, 0, 0)
    year = None
    month = None
    day  = None
    try:
        parsed_result1 = parse(date_str, default=default_date1)
        parsed_result2 = parse(date_str, default=default_date2)
        if parsed_result1.year == parsed_result2.year: year = parsed_result2.year
        if parsed_result1.month == parsed_result2.month: month = parsed_result2.month
        if parsed_result1.day == parsed_result2.day: day = parsed_result2.day
        return year, month, day
    except ValueError:
        return None, None, None

<强> HTML

    $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;
}