【问题标题】:find records in sql with same id as html button's id and generate a modal在 sql 中查找与 html 按钮的 id 相同 id 的记录并生成模式
【发布时间】:2017-07-21 07:00:11
【问题描述】:

问题是我有一个 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>

【问题讨论】:

  • 我没有看到输出按钮的代码。可以展示一下吗?
  • “我试图进行所需的查询并将其设置为 php 中的某个变量,然后在 javascript 中访问它” - 你在这里是什么意思?您为什么尝试在 javascript 中访问 php 变量?您的任务可以通过打开一个新页面(简单的方法)或通过 AJAX 查询来解决。
  • @user4035 你能解释一下如何打开一个新页面或写一个代码示例吗?我试图在 javascript 中访问它,因为我不明白如何在 JS 中运行查询以及如何在 php 中执行 onClick 函数。
  • 您想使用 Ajax 还是打开一个新页面?第二种变体更可取。
  • 你表中的buttonid字段是什么?

标签: php jquery mysql ajax


【解决方案1】:

您创建一个函数来处理对您网站的调用,如下所示:

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>";

我建议您使用&lt;a&gt; 标签而不是&lt;button&gt;,因为您在这里有一个链接,而不是一个表单。只需stylize it with CSS。在表格的“Полная информация”列中创建一个链接:

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

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

然后添加装饰,如制作窗口模式、更改窗口大小等。

【讨论】:

  • 当我试图打开 /employee.php?id=12 时,它会向我抛出这个警告警告:为 foreach() 提供的参数无效
  • @DamirBadilov 你连接到数据库了吗?试试var_dump($_GET['id']);,会打印什么?
  • string(2) "12" 和那个警告
  • @DamirBadilov 该警告来自数据库错误-您没有获取数据。尝试自己调试。
  • 你能解释一下吗(“SELECT * FROM employees WHERE id=:id”); id - 来自 sql 的 id? :id - 按钮 ID?
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多