Get a normal HTML variable value in Anvil server side code

Good day,

Please assist me. In a standard HTML file, I have a line that contains a named variable “First Name”. So I want to get the dynamic value of the First Name in the Anvil server-side code.

Here is my standard HTML file with the variable First Name :

<form>
      <div class="center-box">
        <div class="textbox-container">
          <label class="textbox-label">First Name</label>

Here is the anvil server-side code and how to get the First Name dynamic value in the code:

class Form1(Form1Template):


  def __init__(self, **properties):

    # Set Form properties and Data Bindings.


    self.init_components(**properties)```

I don’t see any variable. You have a label that looks like it’s supposed to be for a text box, but there is no text box.

Are you trying to get the value from a text box that exists in the custom HTML?

I don’t see any server-side code. What you’ve shown all runs on the browser.

You are showing some HTML, which is rarely used in Anvil, and doesn’t have any variables.

The Anvil way to show a text on a form doesn’t involve HTML. You do it adding a label and setting its text property to the text you want to show.

If you have specific reasons for using HTML, please elaborate, otherwise it’s difficult to guess what you are trying to do.

Yes, I’m trying to get a value from a custom HTML to Anvil Python code.

What does this mean?

Hi, I truly appreciate your response.

The reason I’m using the Custom HTML code is that I want the App to behave in such a way that certain text boxes can only accept numeric values (0-9) with a specific length of values and while other text boxes take only alphabets values.

The first App indicates what I’m trying to achieve on the second app in terms of text box value types and length.Cloned App1
Cloned 2 App

So it sounds like you’re trying to validate the value the user types in, and prevent them from proceeding unless a proper value is present. That does not require custom HTML, just validation code.

In the simplest version, you just check the value present before you do something with it, and prevent the action if the value isn’t correct. For example, for a field that must have something in it, you could use:

    if not self.inputfield.text:
        alert("You must type something in")
        return

    # take the action

It obviously gets more complicated than that, but you could do all your validation in the same way, with a series of if statements.

There are also libraries that will do validation for you, if you specify the rules. Here’s one: Yet another validator and formatter

I did not clone your apps, but after reading your messages, it sounds like you are talking about validation, and @jshaffstall already gave the correct answer about that.

Anvil is designed to do as much as you can in Python. If you see yourself using HTML and Javascript because of your previous experience with other tools, your first question should be “how do I do this in Anvil, without using HTML, only in Python”. In some cases, very few cases, you will need to use HTML and Javascript, but in most cases you can write web apps with nothing but Python :slight_smile:

1 Like

Thank you again for the advice, I will go ahead and try.

Thank you so much, I will attempt it.