Setting a variable value from Form Input

I have a form with six inputs.

Each Input has a corresponding function with a variable I want to populate from what the user inputs

I created inputs, example here:

self.text_box_age

I have a function for age - and then a total of 6 for this form.

@anvil.server.callable
def ageQuery():
  currentage = ()
  conn = connect()
  with conn.cursor() as cur:
    cur.execute("SELECT Age, AgePercent FROM age WHERE Age ={currentage}".format(currentage=currentage))
  return cur.fetchall()

Then the function is called on a button click event

    def button_1_click(self, **event_args):
      self.repeating_panel_2.items = anvil.server.call('ageQuery')
      self.repeating_panel_3.items = anvil.server.call('daysQuery')
      self.repeating_panel_4.items = anvil.server.call('baggageQuery')
      self.repeating_panel_5.items = anvil.server.call('singlearticleQuery')
      self.repeating_panel_6.items = anvil.server.call('cancellationQuery')
      pass

How do I get the result of what the customer has input into

self.text_box_age

into the variable “currentage” for the function. Thanks in advance

assuming self.text_box_age is a TextBox, then accessing the text property would work.

currentage = self.text_box_age.text

Then pass it to the server function

self.repeating_panel_2.items = anvil.server.call('ageQuery', currentage)

Just make sure you add the argument in the ageQuery function:

@anvil.server.callable
def ageQuery (currentage):
  conn = connect()
  with conn.cursor() as cur:
    cur.execute("SELECT Age, AgePercent FROM age WHERE Age ={currentage}".format(currentage=currentage))
  return cur.fetchall()

Just as an aside, you would probably want to have all those server calls bundled up in to one to avoid multiple round trips to the server slowing your app down.

Thanks I think I must be doing something else wrong, its saying Warning: baseprice.button_1_click does not exist. Trying to set the ‘click’ event of self.button_1 from baseprice.

Baseprice is the form.

On the server calls thanks for that, we are newbies :slight_smile: I’ll go and add the argument now

We are all newbies at one point or another :slight_smile:

Have a look at this tutorial, it is a really good starting point

The docs have plenty of fantastic tutorials like this which step you through different features.

The docs page is also excellent for information.

The Warning about the click event is probably because there is no funciton in the baseprice form that is called button_1_click but the event handler has been registered. In the design view, if you double click the button_1 it will take you to the event function or create one if it doesn’t exist.

1 Like

Thank you I will take a look - my head hurts anyway so probably time to walk away for a bit. Considering 2 days ago I couldnt even get anything to show on a page from the database, its not too bad

1 Like

So, I watched the tutorial which is good, but since its using Anvils database and mine is external, I’m just not making the mental connections I need to. I am now finding this error if I have the code laid out as per the bottom of this post:-

Warning: baseprice.button_1_click does not exist. Trying to set the ‘click’ event of self.button_1 from baseprice.

However, if I indent it back one, I dont get the button click warning but get this instead

NameError: name ‘self’ is not defined `

*** at ServerModule1, line 28**
*** ` called from baseprice, line 26**

I think I am going backwards as well, I had the same not defined error on all the variables so declared them at the top, but they are already declared inside each function, but not as global variables.

My functions are all like this with variations on the detail, but structured the same


@anvil.server.callable
def ageQuery (currentage):
  currentage = self.text_box_age.text
  conn = connect()
  with conn.cursor() as cur:
    cur.execute("SELECT Age, AgePercent FROM age WHERE Age ={currentage}".format(currentage=currentage))
  return cur.fetchall()

This is my code



class baseprice(basepriceTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
   self.init_components(**properties)
    
    # Any code you write here will run when the form opens.

   def button_1_click(self, **event_args):
      global currentage
      currentage =()  
      global currentdays
      currentdays=()
      global currentbaggage
      currentbaggage=()
      global currentsinglearticle
      currentsinglearticle=()
      global currentcancellation
      currentcancellation=()
      self.repeating_panel_2.items = anvil.server.call('ageQuery', currentage)
      self.repeating_panel_3.items = anvil.server.call('daysQuery', currentdays)
      self.repeating_panel_4.items = anvil.server.call('baggageQuery', currentbaggage)
      self.repeating_panel_5.items = anvil.server.call('singlearticleQuery', currentsinglearticle)
      self.repeating_panel_6.items = anvil.server.call('cancellationQuery', currentcancellation)
      pass

So basically here is the result I want to achieve

I want what is input on the form by the user, to act as the variable for the function when they press submit, which will then output the matching record in my database, on the repeating panel.

Many thanks in advance

Can you provide a clone link?

You cannot access form components from a server function. That’s definitely a step backwards. If you’re passing currentage into the server function, you do not need the line that tries to set it.

Your code that calls the server function is the one that needs to get the text from the text box:

currentage = self.text_box_age.text
self.repeating_panel_2.items = anvil.server.call('ageQuery', currentage)

Or, more likely, just pass the text box’s value into the server function and avoid the currentage variable entirely on the client:

self.repeating_panel_2.items = anvil.server.call('ageQuery', self.text_box_age.text)
1 Like

I did try but it doesnt hide all the server credentials, anyway I think jshaffstall may have sorted it… its almost working :slight_smile:

thank you, I am almost there, I think a couple of my functions have some issues, but the rest seem to be working now.
EDIT all fixed, thank you both of you!

2 Likes