title: Flask: Dropdown values based on prior selection
body: <p>I'm trying to limit selections in a dropdown based on a prior selection the user has made. This is how my flask looks:</p>
  
  <p><strong>init.py</strong></p>
  
  <pre><code>@app.route('/create/', methods=['GET','POST'])
  def create():
      mySQL2 = SelectCustomer(session['ID']) #displayed invoicereceiver
      global sessioncur
      try:
          form = CreateinvoiceForm(request.form)
          if request.method == 'POST' and form.validate():
              #HEADER
              #This fetches from HTML
              customer = request.form.get('customer')
              goodsrec = request.form.get('goodsrec')
      return render_template("createinvoice.html", form=form,  mySQL2 = mySQL2)
  </code></pre>
  
  <p><em>customer</em> is populated from an html form using mySQL2 as possible variables to select from:</p>
  
  <p><strong>html select form</strong></p>
  
  <pre><code>&lt;select required name="customer" class="selectpicker form-control" , 
  placeholder="Select"&gt;
  &lt;option selected="selected"&gt;&lt;/option&gt;
  {% for o in mySQL2 %}      
  &lt;option value="{{ o[2] }}"&gt;{{ o[2] }}&lt;/option&gt;  
  {% endfor %}
  &lt;/select&gt;
  </code></pre>
  
  <p>The selection for <em>goodsrec</em> has to dependend on which <em>customer</em> was selected. 
  My idea was to obtain the customer ID doing as follows:</p>
  
  <pre><code>c, conn = connection()
  customerID = c.execute("SELECT Cm_Id FROM customer WHERE Cm_name ='" +
  str(customer) +"' limit 1")
  customerID = c.fetchone()[0]
  </code></pre>
  
  <p>This value I could then use in a function I have to obtain the <em>goodsreceivers</em> with that ID:</p>
  
  <pre><code>def SelectGoodsrecSEE(customerID):
      c,conn = connection()
      c.execute("SELECT * FROM goodsrec WHERE Gr_Cm_id=" +str(id))
      mySQL8 = c.fetchall()
      c.close()
      conn.close()
      gc.collect()
      return mySQL8
  </code></pre>
  
  <p>So far I am quite sure that would work. What I don't know is how to structure the flask to make it load the first selection and take it into account for the second one. Similiar to the html I would have to loop through <em>mySQL8</em>. But how does the strucutre look in flask to get that done?
  At the moment what I have looks like </p>
  
  <pre><code>@app.route('/create/', methods=['GET','POST'])
  def create():
      mySQL2 = SelectCustomer(session['ID']) #displayed invoicereceiver
      global sessioncur
      try:
      form = CreateinvoiceForm(request.form)
      if request.method == 'POST' and form.validate():
          #HEADER
          #This fetches from HTML
          customer = request.form.get('customer')
          c, conn = connection()
          customerID = c.execute("SELECT Cm_Id FROM customer WHERE Cm_name ='" +
          str(customer) +"' limit 1")
          customerID = c.fetchone()[0]
          mySQL8 = SelectGoodsrecSEE(customerID)
          goodsrec = request.form.get('goodsrec')
      return render_template("create.html", form=form,  mySQL2 = mySQL2)
  </code></pre>
  
  <p>I need to be able to pass <em>mySQL8</em> to the <em>create.html</em>, so that I can make the selection from it in html. Any ideas? Hope its more or less clear what I'm looking for..</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><code>SELECT * FROM goodsrec WHERE Gr_Cm_id=18;</code>
  <a href="https://i.stack.imgur.com/lGMq6.png" rel="nofollow noreferrer">mySQL8</a></p>