Home > database >  How to take a variable from inside a html form that wasn't inputted by the user and output it t
How to take a variable from inside a html form that wasn't inputted by the user and output it t

Time:02-21

<form action="{{ url_for('searchResult') }}" method="POST">
                          <ul style="margin-bottom: 10px;" >
                            <li ><input type="hidden" value="location"/> {{row[0]}}</li>
                            <li >Capacity :{{row[1]}}</li>
                            <li >Prices per night(in GBP):</li>
                            <li >Off-Peak - {{row[2]}}</li>
                            <li >Peak (Apr-Sept) - {{row[3]}}</li>
                            <li ><input type="submit" value="More Info"/><input type="submit"  value="Book"/></li>
                          </ul>
                        </form>

    @app.route('/Locations', methods=['GET', 'POST'])
def searchResult():
    if request.method == 'POST':
        locationInput = request.form['location']
        searchedLocation = locationInput.capitalize()
        try:       
            conn = connection.connection()
            if conn != None:    #Checking if connection is None    
                print('MySQL Connection is established')                          
                cursor = conn.cursor()    #Creating cursor object
                cursor.execute('USE {};'.format(DB_NAME)) #use database                
                sqlStatement = "SELECT * FROM locations;"
                cursor.execute(sqlStatement)
                rows = cursor.fetchall()
                cursor.close() 
                match = False
                for row in rows:
                    if row[0] == searchedLocation:
                        match = True
                        return render_template('locations/'   searchedLocation   '.html')
                if match == False:
                    return render_template('searchError.html', searchedLocation = searchedLocation)                                    
        except:
            conn.rollback()

When I hit one of the submit buttons I want to take the value of row[0] out into a flask app. The row values have come from an 3d array from the flask app, this block of html is looped over the columns. I want to receive the value of the location for that specific block that I clicked the submit button on so that I can move to a new page specifically about that location, and I don't want to user to see an input box. I'm confused and any help would be much appreciated, thanks.

CodePudding user response:

There are several things wrong with this. First, a "hidden" item is not going to be visible, so it should not have a list item (unless you want the user to see it). Second, if you want the hidden item to be called "location", then it's NAME needs to be location. The value is what you want sent back. So:

 <input type="hidden" name"location" value="{{row[0]}}">
  • Related