• 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

Using a variable in HTML statement

Q&A Forum › Using a variable in HTML statement
0 Vote Up Vote Down
Paul asked 6 years ago

Hi,
I kind ask for your help. Following the instruction from your article “ESP32 Over-the-air (OTA) Programming – Web Updater Arduino IDE”
https://randomnerdtutorials.com/esp32-over-the-air-ota-programming/
I’m using the Arduino IDE’s OTA WebUpdater example. In the statement:

"<center><font size=4><b>ESP32 Login Page</b></font></center>"

I’d like to replace the “ESP32 Login Page” text with a variable, but I did not success in anyway.
Could you please tell me a way to do that?
Thanks in advance.
Paul



8 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi Paul.
You probably cannot do it because those variables are defined as const char* variables and it is not straightforward to concatenate const char*.
Can you try one of the suggestions in this link and see if it works?
https://stackoverflow.com/questions/1995053/const-char-concatenation
Regards,
Sara

0 Vote Up Vote Down
Paul answered 6 years ago

Hi Sara, thanks for your response.
I’m afraid that my question was not clear enough and I did not make myself understood.
I’d like to replace “ESP32 Login Page” that is plain text with a variable. Being a new one I can define that variable according your instructions in order to succeed replacing the text.
Paul

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

In order to make that work in an easy way, you need to change these two variables to String type:

String loginIndex = 
String serverIndex =

In the setup(), you need to convert them to char with .c_str():

server.on("/", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", loginIndex.c_str());
});

server.on("/serverIndex", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex.c_str());
});

To concatenate/add a variable to your web page, it needs to be String variable and you do it with + +:

String yourVariable = "My custom message";

String
loginIndex =
"<form name='loginForm'>"
"<table width='20%' bgcolor='A09F9F' align='center'>"
"<tr>"
"<td colspan=2>"
"<center><font size=4><b>" + yourVariable + "</b></font></center>"
"<br>"
(....)

Can you try that and let me know if it works for you?

0 Vote Up Vote Down
Paul answered 6 years ago

Hi Sara, I followed your instructions but it did not work.

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi Paul.
Can you be more specific? Why you say it is not working? What error do you get?
We’ve tested the code, and it was working.

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

You can try our example here: https://gist.github.com/RuiSantosdotme/beb5723d11582b0af1701b7745d65e79
The custom variable is declared on line 13. You should change that variable with the value or text you want. But it needs to be a String.

0 Vote Up Vote Down
Paul answered 6 years ago

Thank you very much Sara. I tried your example and it worked seamlessly. Even that I followed your instructions carefully, my code did not work, now I’ll check it again to find where is the problem.
Paul

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Ok. That’s great!
Then, let me know how it went.
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

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

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

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

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.