0

There are some answers related to 2 forms - 1 submit button with Javascript but there is not how to collect the submitted in routes.py. My script is only able to collected one form, the second one not. I tried to set <input> out forms too. First form is empty, have a hint, please check it:

HTML

<div class="row m-2">
    <div class="col-sm-5 mt-4">
        <h4>Form 1 - Choose one</h4>
        <form method="post" id="form1">
            <div class="custom-control custom-radio">
              <input type="radio" id="selected_A" name="check1" class="custom-control-input" value="selected_A">
              <label class="custom-control-label" for="selected_A">Selected A</label>
            </div>
            <div class="custom-control custom-radio">
              <input type="radio" id="selected_B" name="check1" class="custom-control-input" value="selected_B">
              <label class="custom-control-label" for="selected_B">Selected B</label>
            </div>
        </form>
    </div>
    <div class="col-sm-5 mt-4">
        <h4>Form 2 - Choose one</h4>
        <form method="POST" id="form2">
            <div class="custom-control custom-radio">
              <input type="radio" id="selected_2A" name="check2" class="custom-control-input" value="selected_2A">
              <label class="custom-control-label" for="selected_2A">selected_2A</label>
            </div>
            <div class="custom-control custom-radio">
              <input type="radio" id="selected_2B" name="check2" class="custom-control-input" value="selected_2B">
              <label class="custom-control-label" for="selected_2B">selected_2B</label>
            </div>
            <div class="custom-control custom-radio">
              <input type="radio" id="selected_2C" name="check2" class="custom-control-input" value="selected_2C">
              <label class="custom-control-label" for="selected_2C">selected_2C</label>
            </div>
        </form>
        <input type="button" value="Click" onclick="submit()" />
    </div>
</div>

Javascript

function submit(){
   document.getElementById("form1").submit();
   document.getElementById("form2").submit();
}

routes.py

@envio.route('/checkboxes', methods=['GET', 'POST'])
@login_required
def checkboxes():
    if current_user.is_admin:
        if request.method == 'POST':
            option1 = request.form.getlist('check1')
            option2 = request.form.getlist('check2')
            print(option1, option2)
        return render_template('checkboxes.html')

printout (Selected one item from form1 and another from form2)

[] ['selected_2B']
127.0.0.1 - - [04/Sep/2020 20:58:34] "POST /envios/checkboxes HTTP/1.1" 200 -

Thanks DH

dannisis
  • 423
  • 7
  • 17

1 Answers1

0

I decided to do with WTForm and RadiosFields in back-end instead of Javascript on front-end. It still is at open question.

DH

dannisis
  • 423
  • 7
  • 17