[OpenWRT系列]一、LUCI开发之luci.http.formvalue获取表单数据
1. 前言
开发项目过程中需要获取页面的按钮点击动作并且通过传入的值进行相应的操作,例如:实现网页显示运行日志,并且可以通过点击清除按钮清空运行日志。
2. luci.http.formvalue
函数原型为:
luci.http.formvalue (name, noparse)
Get a certain HTTP input value or a table of all input values.
Parameters
name: Name of the GET or POST variable to fetch
noparse: Don’t parse POST data before getting the value
Return value:
HTTP input value or table of all input value
3. 实现思路
点击 清除 按钮网页将 POST 值Clear:1,程序通过formvalue捕获到Clear值后通过相关的文件操作函数清空日志文件。
4. 程序代码
添加显示调试日志菜单
entry({"admin","concentrator", "productmanagement"}, post_on({ exec = "1"},"action_product"), _("Product Management"), 2)
POST调用函数
function action_debug()
local dlog
local fs = require "nixio.fs"
local submit = (luci.http.formvalue("exec")=="1")
if submit then
local clear = (luci.http.formvalue("clear")=="1")
if clear then
if nixio.fs.access("/tmp/debuglog") then
file = io.open("/tmp/debuglog", "w+")
io.close(file)
end
end
end
if nixio.fs.access("/tmp/debuglog") then
file = io.open("/tmp/debuglog", "r")
dlog = file:read("*a")
io.close(file)
else
dlog = "NONE!\n"
end
luci.template.render("admin_user/debug",{dlog=dlog})
end
当点击清除按钮后将提交 exec:1 和 clear:1 ,action_debug 中获取到键值的时候清空文件,并读取该文件将文件内容传递到 admin_user/debug html模板。