dimanche 19 novembre 2017

How to choose one or more checkbox values and pass that data from HTML to MY-SQL using PYTHON?

As i'm doing one project in that i want to send checkbox values from HTML to PYTHON and i have completed passing checkbox values from HTML to PYTHON but my problem is if i choose anyone of checkbox value it will not be send values to my database but if i choose given all values it will be sent values to my database. but i want to choose one or more values from checkbox and pass choosen values to my database but i don't know how do i do it...Please anyone help me..Thanks in advance

here is my.html:

<form action='/s' method="post">
    <label>UserName:<input type="text" name="well"></label><br/>
        <br/><label>Password:<input type="password" name="dull"></label><br/>
    panchayath<input type="checkbox" name="hello[]" value="yes" >
ward-name<input type="checkbox" name="hi[]" value="yes" >
result<input type="checkbox" name="well[]" value="yes" >
    <input type="submit">
    </form>

here is my.py:

import os
from flask import Flask, request, render_template
from flaskext.mysql import MySQL

mysql = MySQL()

app = Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'votelist'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))

@app.route('/',methods=['GET','POST'])
def main():
    return render_template("ram.html")

@app.route('/s', methods=['GET', 'POST'])
def get_data():
    print("hlo")
    if request.method == 'POST':
        print("cat")
        sp = request.form['well']
        print(sp)
        t = request.form['dull']
        print(t)
        panchayath = request.form.getlist('hello[]')
        print (panchayath)
        ward = request.form.getlist('hi[]')
        print (ward)
        result = request.form.getlist('well[]')
        print (result)
        conn = mysql.connect()
        cursor = conn.cursor()
        # cursor.execute()
        print ("yes")
        # cursor.execute("insert into try_chk3  values (%s,%s,%s)", (some_var))
        # query = "CREATE TABLE saracheck( panchayath  VARCHAR(40), ward VARCHAR(40),Results VARCHAR (40))"
        # cursor.execute(query)
        query = "INSERT INTO demo(username,password,panchayath,wardname,results) VALUES(%s,%s,%s, %s,%s)"
        a=cursor.execute(query, (sp,t,panchayath,ward,result))
        print (a)
        print ("execuet")
        # cursor.execute(query,(first_name,last_name,email_id,can_pic,logo_pic))
        conn.commit()

    return "over"


if __name__ == '__main__':
    app.run()




Aucun commentaire:

Enregistrer un commentaire