-->
Page 1 of 1

Web Server - get data from textbox

PostPosted: Wed Oct 11, 2017 12:58 pm
by exadra
Hello all
Im writting a web setup file on my esp8266 using the NodeMCU.
I can send easily data to the web page but Im having problems getting data from the page.
Check the above below code:

"
...
buf = buf.."MQTT..User..:<input type=\"text\" name=\"Gmqttuser\"><br><br>"
buf = buf.."MQTT..Pass..:<input type=\"password\" name=\"Gmqttpass\"><br><br>"
buf = buf.."<a href=\"?pin=GO1\"><button>Save&Restart</button></a>&nbsp&nbsp&nbsp;<a href=\"?pin=GO2\"><button>Leave without SavingOFF</button></a></p>"
"
I´m able to get the data from the buttons from the ?pin but how can I get the value inside the text boxes???

Thanks
.Pedro

Re: Web Server - get data from textbox

PostPosted: Thu Oct 12, 2017 3:21 pm
by martinayotte
Pure HTML stuff ... :ugeek:
You need to place the text fields inside a FORM with action URL and method POST along with Submit button.

Re: Web Server - get data from textbox [SOLVED]

PostPosted: Fri Oct 13, 2017 3:15 am
by exadra
Thanks for the reply.
It works perfectly.
I leave Below the code of the TCP socket that gets values from the web browser and saves it in a file

"
local function HTTP_start()
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
request= string.gsub(request,"%%21", "!")
request= string.gsub(request,"%%26", "&")

local t={} ; i=1
for str in string.gmatch(request, "([^"..":".."]+)") do
t[i] = str
i = i + 1
end

local y={} ; i=1
for str in string.gmatch(t[1], "([^".."=".."]+)") do
y[i] = str
i = i + 1
end

if (y[10] ~= nil ) then
y[2]= string.gsub(y[2],"%&Gssidpass", "")
y[3]= string.gsub(y[3],"%&Gip", "")
y[4]= string.gsub(y[4],"%&Gmask", "")
y[5]= string.gsub(y[5],"%&Ggateway", "")
y[6]= string.gsub(y[6],"%&Gmqttip", "")
y[7]= string.gsub(y[7],"%&Gmqttuser", "")
y[8]= string.gsub(y[8],"%&Gmqttpass", "")
y[9]= string.gsub(y[9],"%&Button1", "")
y[10]= string.gsub(y[10],"% HTTP/1.1", "")

if string.match(y[10], "Save&Restart") then
file.remove("config.txt")
file.open("config.txt", "w")
file.writeline(y[2])
file.writeline(y[3])
file.writeline("true")
file.writeline(y[4])
file.writeline(y[5])
file.writeline(y[6])
file.writeline(y[7])
file.writeline(y[8])
file.writeline(y[9])
file.close()
tmr.delay(1500)
node.restart()
end
end

local buf = "";
buf = buf.."HTTP/1.1 200 OK\n\n"
buf = buf.."<!DOCTYPE HTML>\n"
buf = buf.."<html>\n"
buf = buf.."<head><meta content=\"text/html; charset=utf-8\">\n"
buf = buf.."<form action=\"Configs\">"
buf = buf.."<p>Network SSID:<input type=\"text\" name=\"Gssdi\" size=15 autofocus></p>"
buf = buf.."<p>Network PASS:<input type=\"password\" name=\"Gssidpass\" size=15 autofocus></p>"
buf = buf.."<p>Device IP:<input type=\"text\" name=\"Gip\" size=15 autofocus></p>"
buf = buf.."<p>Device Mask:<input type=\"text\" name=\"Gmask\" size=15 autofocus></p>"
buf = buf.."<p>Device Gateway:<input type=\"text\" name=\"Ggateway\" size=15 autofocus></p>"
buf = buf.."<p>MQTT IP:<input type=\"text\" name=\"Gmqttip\" size=15 autofocus></p>"
buf = buf.."<p>MQTT User:<input type=\"text\" name=\"Gmqttuser\" size=15 autofocus></p>"
buf = buf.."<p>MQTT Pass:<input type=\"text\" name=\"Gmqttpass\" size=15 autofocus></p>"
buf = buf.."<p><input type=\"submit\" name=\"Button1\" value=\"Save&Restart\">&nbsp;&nbsp;<input type=\"submit\" name=\"Button2\" value=\"EXIT\"></p>"
buf = buf.."</form>"

client:send(buf);
client:close();
collectgarbage();
end)
end)
end
"

.Pedro