首页 > 解决方案 > 如何创建管理页面以向下拉列表添加/删除元素?

问题描述

我的网站中有一个用于插入文档的表单(具有不同的权限 -Admin/User- )。

此表单包含一个下拉列表,问题是它需要由管理员编辑才能在下拉列表中制作更多或更少的项目。

此编辑不应该是代码编辑(这绝对容易),而是一种允许管理员向下拉列表添加元素的表单!

<?php
require_once("identification.php");
require_once('connexionDB.php');
$nom            = isset($_POST['nom']) ? $_POST['nom'] : "";
$pole           = isset($_POST['pole']) ? $_POST['pole'] : "";
$valideur       = isset($_POST['valideur']) ? $_POST['valideur'] : "";
$perimetre      = isset($_POST['perimetre']) ? $_POST['perimetre'] : "";
$direction      = isset($_POST['direction']) ? $_POST['direction'] : "";
$activite       = isset($_POST['activite']) ? $_POST['activite'] : "";
$version        = isset($_POST['version']) ? $_POST['version'] : "";
$type_doc       = isset($_POST['type_doc']) ? $_POST['type_doc'] : "";
$description    = isset($_POST['description']) ? $_POST['description'] : "";
$zone           = isset($_POST['zone']) ? $_POST['zone'] : "";
$langue         = isset($_POST['langue']) ? $_POST['langue'] : "";
$date           = isset($_POST['date']) ? $_POST['date'] : "";
$comm_sur_modif = isset($_POST['comm_sur_modif']) ? $_POST['comm_sur_modif'] : "";
$commentaire    = isset($_POST['commentaire']) ? $_POST['commentaire'] : "";
$auteur         = $_SESSION["fati"];

if (isset($_FILES['document']) and !empty($_FILES['document']['name'])) {
    $taillemax        = 4221225472;
    $extensionvalides = ['pdf', 'docx'];
    if ($_FILES['document']['size'] <= $taillemax) {
        $extensionUpload = strtolower(substr(strrchr($_FILES['document']['name'], '.'), 1));
        if (in_array($extensionUpload, $extensionvalides)) {
            $chemain = "doc/" . $nom . "." . $extensionUpload;

            $resultat = move_uploaded_file($_FILES['document']['tmp_name'], $chemain);

            if ($resultat) {
                $requete  = "insert into document(nom,direction,pole,activite,version,type_doc,description,zone,perimetre,langue,chemin,auteur,date,comm_sur_modif,commentaire) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                $params   = [
                    $nom,
                    $direction,
                    $pole,
                    $activite,
                    $version,
                    $type_doc,
                    $description,
                    $zone,
                    $perimetre,
                    $langue,
                    "doc/" . $nom . "." . $extensionUpload,
                    $auteur,
                    $date,
                    $comm_sur_modif,
                    $commentaire,
                ];
                $resultat = $pdo->prepare($requete);
                $resultat->execute($params);
                header("location:documents.php");
            }
        }
    }
}

<div class="form-group">
    <label for="type_doc">type de document  </label>
    </br>
    <select name="type_doc" id="type_doc">
        <option value="NA">N/A</option>
        <option  value="guide_de_conception">guide de conception</option>
        <option  value="standard_rt">standard et RT</option>
        <option  value="methodologies">methodologies</option>
        <option  value="processus">processus</option>
        <option  value="retex_capitalisation">retex et  capitalisation</option>
        <option  value="normes_reglementations">normes reglementations</option>
        <option  value="cdc">CDC</option>
        <option  value="essais_plans_validation">essais et plans de validation</option>
    </select>
</div>

标签: phphtmlformslogin

解决方案


概述:

  1. 您需要将所需字段的列表存储在某处。您不能将它们存储在 LocalStorage 或 cookie 中——它们将本地存储在管理员的计算机上(那么用户将如何看到这些更改?)。您需要一个中心位置:网络服务器。这为您提供了两种选择:(a) 网络服务器上的文件,或 MySQL(现在称为 MariaDB)数据库表中的文件。我建议(b)。

  2. 为了读取/写入网络服务器上的文件——或从网络服务器上的数据库添加/删除/读取——您需要编写一些服务器端代码。大多数网络服务器都使用 PHP 作为后端语言,但 Microsoft 服务器使用 ASP .Net。现在还可以选择安装/使用 node.js(如果您想使用 javascript 作为后端服务器语言)。如前所述,PHP 非常流行,并且有大量的博客和 YouTube 教程展示了如何做到这一点。

  3. 以 PHP 为例,您将重命名index.htmlindex.php- 只需这样做。当前存在的任何内容都不会受到影响 - 但现在您可以嵌入部分 PHP 代码,服务器将在呈现/显示 HTML 之前运行该代码。(请注意,当您重命名文件扩展名时,其他任何内容都不会改变。如果您在 apache Web 服务器上 - 而且大多数是 - 您永远不需要再使用.html扩展名。试试看。)

  4. index.php现在将从一段 PHP 代码开始,它告诉它 (a) 登录到数据库,(b) 从表中读取该值,(c) 将值存储在一个变量中。现在,在呈现页面时,显然很容易将这些数据放入 HTML 中。

  5. You will need a page that only the admin can access. Again, using a back-end language will allow your admin page (HTML) to request a username and password, and then run some back-end code to check the info stored on the webserver (again, either in a file or in a database table), to see if the username/password is correct.

  6. Your admin page, after logging in, will:

    • As in step (4) above, read the database table to get the current settings for user-level choices for the drop-down, then display those choices on the screen. You will also need a method of adding new options for the drop down, and a button to signify that the changes are done. Once pressed, the page will send that data back to the webserver to store back in the table, over-writing what was there before.

    • There are two ways to send data from an HTML page to the webserver: (a) <form></form> and (b) AJAX. By all means, use AJAX - forms are more restricted, way less elegant, and require that the page is refreshed or changed. Forms are 1999, AJAX is 2019.

    • AJAX, written in javascript/jQuery, lets you (a) detect a button click; (b) collect the data from the input fields; (c) send that data to a PHP file on the webserver; (d) receive a message (from the webserver, after it has finished adding the data to the table) back on the HTML side; (e) update the page smoothly and without refreshing anything. With AJAX, you (as the developer) retain complete control from beginning to end, comme il faut.

There are gazillions of YouTube and blog tutorials on how to do all of this in PHP and jQuery. Enjoy!

Here's are a couple:

https://www.youtube.com/watch?v=aujNp92p0Uc

https://www.youtube.com/watch?v=gvGb5Z0yMFY


推荐阅读