Fetch Data to the Form in same page Using Flask, Ajax

  flask, flask-mysql, python, python-3.x

I am having issue with my program. I have two forms, one for searching entry in database, another fetch the data from database and update the form. I am not able to populate search result into the next form on same page.
enter image description here

    {% for row in records %}
                              <div class="form-group row">
                                 <label for="badgeId" class="col-sm-4 col-form-label">Scan Badge ID</label>
                                 <div class="col-sm-5">
                                    <input type="number" class="form-control" id="badgeId" name="badgeId"  value="{{row[0]}}">
                                 </div>
                              <div class="col-sm-3">
                                  <button type="submit" class="btn btn-primary" id="searchButton">Search</button>
                                 </div>
                              </div>
                              <br/>
                                  <div class="form-group row">
                                 <label for="fullName" class="col-sm-4 col-form-label">Full Name</label>
                                 <div class="col-sm-7">
                                    <input type="text" class="form-control" id="fullName"  value="{{row[1]}}" />
                                 </div>
                              </div>
                              <div class="form-group row">
                                 <label for="testDate" class="col-sm-4 col-form-label">Date</label>
                                 <div class="col-sm-7">
 
                                    <input type="text" class="form-control" id="testDate" value="{{row[2]}}" />
                                 </div>
                              </div>
                              <div class="form-group row">
                                 <label for="evidence" class="col-sm-4 col-form-label">Evidence Type</label>
                                 <div class="col-sm-7">
                                    <input type="text" class="form-control" id="evidence" value="{{row[3]}}" />
                                 </div>
                              </div>
                              <div class="form-group row">
                                 <label for="testLab" class="col-sm-4 col-form-label">Test Lab</label>
                                 <div class="col-sm-7">
                                    <input type="text" class="form-control" id="testLab"  value="{{row[4]}}"/>
                                 </div>
                              </div>

                               <div class="form-group row">
                                 <label for="testLab" class="col-sm-4 col-form-label">Test Status</label>
                                 <div class="col-sm-7">
                                    <input type="text" class="form-control" id="testStatus"  />
                                 </div>
                              </div>
                              </form>

  {% endfor %}

And main.py File as;

@app.route('/fetch', methods=['POST', 'GET'])
def fetch():
    Id = request.form['Id']
    cursor = mysql.connection.cursor()

    if request.method == 'POST':
        search_word = request.form['Id']
        print(search_word)
        sql_select_Query = "select * from employee_detail where badge_id = {}".format(badgeId)
        cursor.execute(sql_select_Query)
        records = cursor.fetchall()
        print(records)
    return render_template("display.html",records=records)

Source: Python-3x Questions

LEAVE A COMMENT