在sql中找到与html按钮的id相同id的记录,并生成一个模态

时间:2017-07-21 07:00:12

标签: php jquery mysql ajax

问题是我有一个html表,其中包含有关从MySQL数据库获取的员工的简短信息。我正在尝试创建一个带有一些ID的按钮,该按钮将打开一个模态窗口,其中包含有关员工的完整信息,该信息在其名为“ButtonID”的字段中具有相同的值。我尝试进行所需的查询并将其设置为php中的某个变量,然后在javascript中访问它,但它并没有很好地结束。

 <!--
 in <input id = "1" type="button" value="Full Info" input id equals to sql 
'ButtonID' fields value
 -->
 Edit:
<form action="">
<input id = "1" type="button" value="Full Info" 
 onclick="fullInfo()">
 </form>

<!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Список сотрудников</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
      </head>
      <body>

    <div class = "header">
        <img src = "img/header.png"></img>
    </div>

    <div class = "container">
    <?php

    echo "<table id='table_id' class = 'table table-striped'>";
    echo "<thead>
            <tr>
                <th>№</th>
                <th>ФИО</th>
                <th>Имя транслитом</th>
                <th>Дата рождения</th>
                <th>Должность</th>
                <th>Дата приёма</th>
                <th>№ удостоверения</th>
                <th>Полная информация</th>
            </tr>
        </thead>
        ";

    class TableRows extends RecursiveIteratorIterator { 
        function __construct($it) { 
            parent::__construct($it, self::LEAVES_ONLY); 
        }

        function current() {
            return "<td>" . parent::current(). "</td>";
        }

        function beginChildren() { 
            echo "<tr>"; 
        } 

        function endChildren() { 
            echo "</tr>" . "\n";
        } 
    } 

    $servername = "localhost";
    $username = "user";
    $password = "password";
    $dbname = "test";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("SELECT number, fullname, engname,birthdaydate, position, recruitmentDate,id,buttonid FROM employees"); 
        $ids = $conn->prepare("SELECT * FROM employees"); 
        $stmt->execute();

        // set the resulting array to associative
        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
        foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
            echo $v;
        }
    }
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }

    $conn = null;
    echo "</table>";
    ?>
    </div>
    <div class = "footer">
        <img src = "img/footer.jpg"></img>
    </div>

        <!--<link rel="stylesheet" type="text/css" href="css/styles.css">-->
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="css/normalize.css">
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css">
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
        <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
        <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.15/css/jquery.dataTables.css">
        <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.15/js/jquery.dataTables.js"></script>
        <script>

    $(document).ready(function() {
        $('#table_id').DataTable( {
            "language": {
                "url": "//cdn.datatables.net/plug-ins/1.10.13/i18n/Russian.json"
            }
        } );



    } );
        </script>
        <!--
        <script>
        function fullInfo(){
            var ids = <?php echo json_encode($ids); ?>;
            for (int i = 0;i<ids.length;i++){
                if (ids['buttonID'] == this.id){
                    alert ("good");
                }
            }
        };
        </script>
        -->

      </body>
    </html>

2 个答案:

答案 0 :(得分:0)

您创建了一个处理对您网站的调用的功能,如下所示:

http://my-site.com/employee.php?id=12

employee.php(想法)

$stmt = $conn->prepare("SELECT * FROM employees WHERE id=:id"); 
$stmt->execute(array(':id' => $_GET['id']));
$employee = $stmt->fetch(PDO::FETCH_ASSOC);
print "<ul>";

//list the employee data
foreach($employee as $key => $value)
{
    $key = htmlspecialchars($key, ENT_QUOTES, 'UTF-8');
    $value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8')
    print "<li>". $key .': ' . $value . '</li>';
}
print "</ul>";

我建议您使用<a>代码而不是<button>,因为此处有链接,而不是表单。只需stylize it with CSS。在您的表格的“Полнаяинформация”栏中创建一个链接:

<a target="_blank" href=\"/employee.php?id=$row['id']\">Полная информация</a>

这将打开一个新页面。使用PHP处理请求,我给了你示例代码。

然后添加装饰品,如制作窗户模态,改变它的大小等。

答案 1 :(得分:0)

对于每一行,回显一个id为员工ID的按钮。 在按钮上的id之前添加一个字母,以确保它不会重复。

echo "<button id='emp*$employeeid*'>view</button>