HTML:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Form to MySQL</title> </head> <body> <form action="/submit" method="POST"> <label for="name">Name:</label> <input type="text" id="name" name="name" required><br><br> <label for="surname">Surname:</label> <input type="text" id="surname" name="surname" required><br><br> <label for="email">Email:</label> <input type="email" id="email" name="email" required><br><br> <input type="submit" value="Submit"> </form> </body> </html>
Python Flask:
from flask import Flask, render_template, request import mysql.connector app = Flask(__name__) # MySQL configuration db = mysql.connector.connect( host='localhost', user='your_username', # Replace with your MySQL username password='your_password', # Replace with your MySQL password database='your_database' # Replace with your MySQL database name ) cursor = db.cursor() @app.route('/') def index(): return render_template('index.html') @app.route('/submit', methods=['POST']) def submit(): if request.method == 'POST': name = request.form['name'] surname = request.form['surname'] email = request.form['email'] # Insert data into MySQL database sql = "INSERT INTO users (name, surname, email) VALUES (%s, %s, %s)" val = (name, surname, email) cursor.execute(sql, val) db.commit() return "Data stored successfully" if __name__ == '__main__': app.run(debug=True)
Try to answer about this code:
Which attribute is used to set the form submission URL in the HTML form?
formmethod
action
formtarget
method
What type of input field is used for capturing an email address in the form?
text
email
mail
mail
Which tag is used to define the label for the "Surname" input field in the form?
<surname>
<label>
<input>
<text>
What HTML attribute is used to ensure that the "Name" input field must be filled out in the form?
mandatory
required
necessity
validate