如何创建管理页面以将元素添加/删除到下拉列表中? [英] How to create an admin page to add/remove elements to a drop-down list?

查看:62
本文介绍了如何创建管理页面以将元素添加/删除到下拉列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a form in my website that's used to insert documents (has different privileges -Admin/User- ).

此表单包含一个下拉列表,问题是需要由管理员对其进行编辑才能在该下拉列表中添加更多或更少的项目.

This form includes a drop-down list, the problem is that it needs to be edited by the admin to make more or less items in the drop down list.

此编辑不应是代码编辑(绝对容易) 但是允许管理员向下拉列表添加元素的表单 我四处搜寻,但没有找到答案,希望您能帮我解决这个问题!

This edit shouldn't be a code edit (which is absolutely easy) but a form that allows the admin to add elements to drop-down list I searched around and I didn't find an answer , I wish you could help me with this !

<?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>

推荐答案

概述:

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

  1. You need to store the list of desired fields somewhere. You cannot store them in LocalStorage or in cookies - those would be stored locally on the admin's computer (so how would a user see those changes?). You need a central location: the webserver. This gives you two choices: (a) a file on the webserver, or in a MySQL (now called MariaDB) database table. I suggest (b).

为了读取/写入Web服务器上的文件-或从Web服务器上的数据库添加/删除/读取-您需要编写一些服务器端代码. MOST Web服务器将PHP作为后端语言提供,但是Microsoft服务器使用ASP .Net.现在,还可以选择安装/使用node.js(如果要使用javascript作为后端服务器语言).如前所述,PHP十分流行,并且在数不胜数的博客和YouTube教程中都有一些地方展示了如何做到这一点.

In order to read/write to a file on the webserver -- or to add/delete/read from a database on the webserver -- you need to write some server-side code. MOST webservers have PHP available as a back-end language, but Microsoft servers use ASP .Net. There is now also the choice to install/use node.js (if you want to use javascript as your back-end server language). As mentioned, PHP is hugely popular and there are somewhere around a gazillion blog and YouTube tutorials showing how to do this.

以PHP为例,您将index.html重命名为index.php-只需这样做.目前不存在任何影响-但是现在您可以嵌入PHP代码的各个部分,并且服务器将在呈现/显示HTML之前运行该代码. (请注意,重命名文件扩展名不会发生其他变化.只要您在apache网络服务器上-多数情况下-您无需再使用.html扩展名.)

Using PHP as the example, you will rename your index.html to index.php - just do that. Nothing that currently exists will be affected - but now you can embed sections of PHP code and the server will run that code before rendering/displaying the HTML. (Note that nothing else changes when you rename the file extension. Provided you are on an apache web server - and most are - you need never use the .html extension ever again. Try it.)

您的index.php现在将以一段PHP代码开始,该代码段告诉它(a)登录数据库,(b)从表中读取该值,(c)将值存储在变量中.现在,在呈现页面时,很容易将数据隐藏到HTML中.

Your index.php will now begin with a snippet of PHP code that tells it to (a) login to the database, (b) read that value from the table, (c) store the value in a variable. It is now patently easy to schlep that data into the HTML as the page is being rendered.

您将需要一个只有管理员才能访问的页面.同样,使用后端语言将允许您的管理页面(HTML)请求用户名和密码,然后运行一些后端代码来检查存储在Web服务器上的信息(同样,是在文件中还是在数据库中)表),以查看用户名/密码是否正确.

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.

登录后,您的管理页面将:

Your admin page, after logging in, will:

  • 与上面的步骤(4)一样,读取数据库表以获取下拉菜单user级别选项的当前设置,然后在屏幕上显示这些选项.您还需要一种为下拉菜单添加新选项的方法,以及一个按钮,指示更改已完成.按下后,页面会将数据发送回Web服务器,以存储回表中,从而覆盖之前的内容.

  • 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.

有两种方法可以将数据从HTML页面发送到Web服务器:(a)<form></form>和(b)AJAX.无论如何,请使用AJAX-表单更受限制,方式不太优雅,并要求刷新或更改页面. 表格是1999年,AJAX是2019年.

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.

用javascript/jQuery编写的AJAX,可让您(a)检测按钮单击; (b)从输入字段中收集数据; (c)将该数据发送到Web服务器上的PHP文件中; (d)在HTML端收到一条消息(在将数据添加到表中之后,从Web服务器接收消息); (e)顺利更新页面,而不刷新任何内容.使用AJAX,您(作为开发人员)从头到尾始终保持完全的控制权,谨慎.

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.

无数的YouTube和博客教程介绍了如何在PHP和jQuery中完成所有这些操作. 享受!

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

这是一对:

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

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

这篇关于如何创建管理页面以将元素添加/删除到下拉列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆