Home > OS >  How can I can a checkbox to pass through a string into MySQL?
How can I can a checkbox to pass through a string into MySQL?

Time:06-04

I am working on a project right now and am having trouble figuring out something. I have created a table in MySQL with the main input from the user being a "message" and a "creator" What I am trying to do is have it so on the "Create message form", if a user wishes to not input their name, they can simply get a checkbox and "anonymous" would be passed through as "creator". I havent been able to figure out how to get the "anonymous" input to get pass through. If i type I leave the "creator" field empty and have the checkbox checked, my validations get called and says field cannot be empty, and if i remove the validations then is passes through but in the DB, the creator field is set as empty. My next approach was to have the creator have a default of "anonymous" in MySQL, i ran this "ALTER TABLE messages ALTER creator SET DEFAULT 'Anonymous';" and again it would call validations, and with no validations, it still enters the data as empty. My next attempt is to maybe perhaps write an if statement in my validations, but not entirely sure how to write it out. Hopefully this question makes sense...

UPDATE: I have since created an If statement that handles the "anonymous" checkbox. When no name is entered into the text input field, and the checkbox is checked, then "Anonymous" gets pushed into MySQL DB. NOW the issue is if I enter a name and the checkbox is unchecked, I receive a key error

KeyError: 'creator2'

I believe it has something to do with my If statement and have had no success in fixing it...

this is my form on the user side

<form action="/messages/create" method="post">
    <div>
        <label for="message">Enter Message</label>
        <textarea name="message" id="message" cols="30" rows="10"></textarea>
    </div>
    <div>
        <label for="creator">From</label>
        <!-- <small ></small> -->
        <input type="text" name="creator1">
        <input type="checkbox" id="Anonymous" name="creator2" >Anonymous</input>
    </div>
    <input type="submit" value="Create">
</form>

this is my controller to handle the form with the bugged if statement.

@app.route("/messages/create", methods = ["POST"])
def create_message():
data = request.form.to_dict()
if data["creator2"]:
    data["creator"] = "Anonymous"
else:
    data["creator"] = data["creator1"]
if Message.message_validator(data):
    query_data = {
        "message": data["message"],
        "creator": data["creator"]
    }
    message_id = Message.create_message(query_data)
    return redirect("/")
return redirect("/messages/new")

CodePudding user response:

First thing, as far as I know in HTML it's better to have the same value for the fields 'id' and 'name'. It can be error prone. Next thing I would link the anonymous checkbox with the input as it should be disabled when user declares as anonymous. In that way, you can just use onChanged methods to substitue the input value and only watch its content.

CodePudding user response:

In the form I'd suggest you use the same name for the name field and the id field just to avoid confusion

<input type="checkbox" id="Anonymous" name="Anonymous" >Anonymous</input>

Then I'd add a value field inside the checkbox so that when the checkbox is checked, the value gets sent to the backend

<input type="checkbox" id="Anonymous" name="Anonymous" value="Anonymous">Anonymous</input>

Then in the backend we check if the value of our checkbox is equal to the value we expect, and if it is that means the checkbox has been checked

if data["creator2"] == "Anonymous":
    data["creator"] = "Anonymous"

I didn't test the code, so let me know if it works :)

  • Related