0

I am trying to get the data that I have stored inside of a script tag in my html file to my python file so that I can use it in a python function, does anyone have any idea how I could achieve this? Here's my html code:

<textarea id="rendezook" class="rendezook"></textarea>
<button onclick="get_text();">Valider</button>
<script>
    function get_text() {
        var x = document.getElementById("rendezook")
        location.href = "/validate_comment?rendezook=" + encodeURIComponent(x.value);
        console.log(x.value)
    }
</script>

And then this is my python file:

@app.route('/validate_comment')
def validate_comment(item_id=None):
    print("test")
    print(request.args.get("rendezook"))
    print("test2")
    return redirect(url_for('msr_edit', item_id=72))

test and test2 are not getting printed so it is not entering the function.

1 Answer 1

1

The easiest way is to just use a form that's submitted via a GET request:

<form action="/validate_comment">
  <textarea name="rendezook" class="rendezook"></textarea>
  <button type="submit">Valider</button>
</form>

This will result in an URL like /validate_comment?rendezook=some_value_here, so you can access it via request.args (as it looks like you're using Flask):

@app.route('/validate_comment')
def validate_comment():
    print("yeet", request.args.get("rendezook"))
    return redirect(url_for('msr_edit', item_id=72))

Based on comments, if you're unable to use a form for some reason, then:

<textarea id="rendezook" class="rendezook"></textarea>
<button onclick="test()">Valider</button>
<script>
function test() {
    var x = document.getElementById("rendezook");
    location.href = "/validate_comment?rendezook=" + encodeURIComponent(x.value);
}
</script>

EDIT 2

Here's a minimal example that provably works :)

from flask import request, Flask

app = Flask(__name__)


@app.route("/validate_comment")
def validate_comment(item_id=None):
    x = request.args.get("rendezook")
    return f"you said {x}"


@app.route("/")
def index():
    return """
<textarea id="rendezook" class="rendezook"></textarea>
<button onclick="get_text();">Valider</button>
<script>
    function get_text() {
        var x = document.getElementById("rendezook")
        location.href = "/validate_comment?rendezook=" + encodeURIComponent(x.value);
        console.log(x.value)
    }
</script>
"""


if __name__ == "__main__":
    app.run()
Sign up to request clarification or add additional context in comments.

11 Comments

the problem with this is that i am already inside of a form tag so I can't make a new one, do you think there is a way to do this but without the form tags?
Well, in that case just location.href = '/validate_comment?rendezook=' + encodeURIComponent(x.value) ...
I have changed the href in the <a> tag with this <a location.href = '/validate_comment?rendezook=' + encodeURIComponent(x.value)>Valider</a> but it does not seem to call my python function anymore, did i do something wrong? (sorry if I did I am fairly new to html)
Oh sorry I did not see that! I have added the location.href exactly like you sent it but it still isn't going into my python function, is it maybe because the python function expects the href to be just "/validate_comment"?
I have edited my question with the way my code looks like now
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.