• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Input Data on HTML Form ESP8266 Web Server using Arduino IDE

Q&A Forum › Category: ESP8266 › Input Data on HTML Form ESP8266 Web Server using Arduino IDE
0 Vote Up Vote Down
Johan Romuald asked 11 months ago

Hi,
I would like to do validity check on submission on user input value before write to the file. If validity fails I would like alter the user and not to write to the file.
I am wondering where do I do that? Is it some where here?
webserver.on(“/get”, HTTP_GET, [] (AsyncWebServerRequest * request)
{
String inputMessage;

if (request->hasParam(PARAM_SSID))
{
inputMessage = request->getParam(PARAM_SSID)->value();
writeFile(SPIFFS, “/input_ssid.txt”, inputMessage.c_str());
}
else if (request->hasParam(PARAM_PSK))
……………………..
Thanks
Johan
 
 
 

Question Tags: Asyncwebserver
7 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 11 months ago

Hi.

The user input is saved on the inputMessage variable:

inputMessage = request->getParam(PARAM_SSID)->value();

This is the line that writes that value into SPIFFS:

writeFile(SPIFFS, “/input_ssid.txt”, inputMessage.c_str());

So, you need to add your conditions before calling the previous line. Only if the input is valid, you’ll call the writeFile() function.

To send a message to the client, for example:

request->send(200, "text/plain", "invalid input");

Let me know if you need further help.
Regards,
Sara

0 Vote Up Vote Down
Johan Romuald answered 11 months ago

Thanks Sara, I will give go.

0 Vote Up Vote Down
Sara Santos Staff answered 11 months ago

Great.
Then, tell me if you need anything else.
Regards,
Sara

0 Vote Up Vote Down
Johan Romuald answered 11 months ago

Hi Sara,
I got it working but only I could send the message if the entry is invalid.
where do I write this statement?

request->send(200, "text/plain", "invalid input");

Thanks
Johan

 

0 Vote Up Vote Down
Sara Santos Staff answered 11 months ago

Hi again.
You need to add a condition that if the input is not valid, it will call that line.
As you did to write in SPIFFS, but in case the input is invalid.
If you need further help, you can share the snippet of code that handles that.
Regards,
Sara

0 Vote Up Vote Down
Johan Romuald answered 11 months ago

Hi Sara,
Here is the code;
webserver.on(“/get”, HTTP_GET, [] (AsyncWebServerRequest * request)
{
String inputMessage;

if (request->hasParam(PARAM_SSID))
{
inputMessage = request->getParam(PARAM_SSID)->value();
writeFile(SPIFFS, “/input_ssid.txt”, inputMessage.c_str());
}
else if (request->hasParam(PARAM_PSK))
{
inputMessage = request->getParam(PARAM_PSK)->value();
writeFile(SPIFFS, “/input_psk.txt”, inputMessage.c_str());
}
else if (request->hasParam(PARAM_IP))
{
inputMessage = request->getParam(PARAM_IP)->value();
int n = inputMessage.length();
char inMsg[n+1];
strcpy(inMsg, inputMessage.c_str());

if(validate_ip(inMsg) == 1)
{
writeFile(SPIFFS, “/input_ip.txt”, inputMessage.c_str());
}
else{
request->send(200, “text/plain”, “invalid input”);
}
}
else if (request->hasParam(PARAM_MASK))
…………………………………………………………………..
…………………………………………………………………
}
else
{
inputMessage = “No message sent”;
}
request->send(200, “text/text”, inputMessage);
});
Thank you.
Johan

0 Vote Up Vote Down
Sara Santos Staff answered 11 months ago

Hi.

It looks good to me. That part seems to do exactly what you want.

 

if(validate_ip(inMsg) == 1)
{
  writeFile(SPIFFS, “/input_ip.txt”, inputMessage.c_str());
}
else{
  request->send(200, “text/plain”, “invalid input”);
}

Isn’t it working? Or is there something missing?
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Firebase Web App with ESP32 and ESP8266 (version 1.2) April 25, 2022
  • [eBook Updated] MicroPython Programming with ESP32 and ESP8266 (version 1.3) February 7, 2022

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2022 · RandomNerdTutorials.com · All Rights Reserved