Python Forum

Full Version: flask not submitting text entry when mixed with checkbox entries
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi,

I am creating a flask form which requires login and after login it goes to the entry form where we have check boxes and text entry.

I am facing specific problem. I am unable to get the value of text boxes but getting the value of checkboxes.

I using flask and using every request method to print my text boxes but not getting the values.

Can anyone please suggest

below is my code for main file:

from flask import Flask, render_template
import os
from flask import redirect, url_for, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"]="sqlite:////OtrsSummary.db"
app.config["SECRET_KEY"]="thisiskey"

db = SQLAlchemy(app)

# @app.route("/ndex")
# def home():
#     names = os.getlogin().split(".")[0].title()
#     return render_template("index.html", name=names)

@app.route("/welcome", methods=['GET', 'POST'])
def welcome():
    if request.method=="POST":
        try:
            phase = request.form.get("phase")
            rphase = phase.replace("on", "1")
            print(rphase)
            # sale = request.form.get("sale")
            # rsale = sale.replace("on", "1")
            # print(rsale)
            # floor = request.form.get("floor")
            # rfloor = floor.replace("on", "1")
            # options = request.form.get("options")
            # roptions = options.replace("on", "1")
            # image = request.form.get("image")
            # rimage = image.replace("on", "1")
            # video = request.form.get("video")
            # rvideo = video.replace("on", "1")
            # possession = request.form.get("possession")
            # rpossession = possession.replace("on", "1")
            # amenities = request.form.get("amenities")
            # ramenities = amenities.replace("on", "1")
            # prdeactivation = request.form.get("prdeactivation")
            # rprdeactivation = prdeactivation.replace("on", "1")
            # np = request.form.get("np")
            # rnp = np.replace("on", "1")
            # newbooking = request.form.get("newbooking")
            # rnewbooking = newbooking.replace("on", "1")
            # bank = request.form.get("bank")
            # rbank = bank.replace("on", "1")
            # lat = request.form.get("lat")
            # rlat = lat.replace("on", "1")
            # usp = request.form.get("usp")
            # rusp = usp.replace("on", "1")
            # fact = request.form.get("fact")
            # rfact = fact.replace("on", "1")
            # prname = request.form.get("prname")
            # rprname = prname.replace("on", "1")
            # prdescription = request.form.get("prdescription")
            # rprdescription = prdescription.replace("on", "1")
            # prspecification = request.form.get("prspecification")
            # rprspecification = prspecification.replace("on", "1")
            # builderdetails = request.form.get("builderdetails")
            # rbuilerdetails = builderdetails.replace("on", "1")
            # tco = request.form.get("tco")
            # rtco = tco.replace("on", "1")
            # npdeactivation = request.form.get("npdeactivation")
            # rnpdeactivation = npdeactivation.replace("on", "1")
            # constuctionimages = request.form.get("constuctionimages")
            # rconstuctionimages = constuctionimages.replace("on", "1")
            # brochure = request.form.get("brochure")
            # rbrochure = brochure.replace("on", "1")
            # rera = request.form.get("rera")
            # rrera = rera.replace("on", "1")
            rticketnumber = request.form["ticketnumber"]#not getting value of these
            rxidnumber = request.form["xidnumber"]##not getting value of these
            rreranumber = request.form["reranumber"]##not getting value of these
            print(rxidnumber)
            print(rticketnumber)
            msg = "Entry Submitted Successfully"

        except AttributeError:
            msg = "Please Do Not Submit Blank Form"
         #con.close()



    return render_template("same.html")

@app.route("/", methods=["GET", "POST"])
def log():
    names = os.getlogin().split(".")[0].title()
    error= None
    if request.method == "POST":
        if request.form["username"]!= os.getlogin() or request.form["password"]!="1234":
            error = "Invalid Credentials.Please Try again."
        else:
            return redirect(url_for("welcome"))
    return render_template("index.html", error=error, name=names)

app.run(debug=True)
I have attached my html file as it is too big
I've simplified your code significantly, so testing is easier.

Python code:
from flask import Flask, render_template, redirect, url_for, request

app = Flask(__name__)
 
@app.route("/", methods=['GET', 'POST'])
def welcome():
    if request.method=="POST":
            phase = request.form.get("phase")
            rphase = phase.replace("on", "1")
            print(f"Checkbox test: {phase} | {rphase}")
            
            spam = request.form["spamtest"]
            print(f"Input test: {spam}")
    return render_template("test.html")
 
app.run(debug=True)
Template (./templates/test.html):
<html>
    <body>
        <form method="post">
            <div>
                <label>
                    Checkbox test:
                    <input type="checkbox" name="phase" />
                </label>
            </div>
            <div>
                <label>
                    Textbox test:
                    <input type="text" name="spamtest" />
                </label>
            </div>

            <input type="submit" value="click me" />
        </form>
    </body>
</html>
Running it:
Output:
E:\Projects\etc>python spam.py * Serving Flask app "spam" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: on * Restarting with stat * Debugger is active! * Debugger PIN: 984-122-235 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [11/Oct/2018 17:54:21] "GET / HTTP/1.1" 200 - Checkbox test: on | 1 Input test: eggs are tasty 127.0.0.1 - - [11/Oct/2018 17:54:31] "POST / HTTP/1.1" 200 -
The input and checkbox were both seen on the server side just fine. Which means your issue is somewhere else. I don't currently have time to dig through the html file to see if something's malformed, but hopefully this helps narrow down where you should be looking.
Name form action so it point to route.
<form action="/welcome" ALIGN="CENTRE" method="POST">
Without a named action, a form submits to the same url. OP's /welcome handles both post and get, so I didn't think that'd matter (the only other function defined is a log).
Looking closer at it,so is checkboxes the problem or how the work bye default.
Only checked checkboxes are sent to server,not unchecked ones.
So if not one checkbox is checked,nothing is sent to server and value in input field is ignored.
Some solution that can fix it in html,not using JavaScript.
Have a hidden field.
<input type="hidden" name="phase" value="0">
Have one value always checked on.
<input type="checkbox" name="phase" checked>
Use <select> with two options on and off.
Probably more fixes if search on this problem.