-1

I have a form like so:

<form method="post">
    <div id="some-div">
        <input id="some-input" type="text" name="input" placeholder="some input">
        <button id="update-button" type="submit" name="submit_update_entry">Save changes</button>
        <button id="delete-button" type="submit" name="submit_delete_entry">Delete entry</button>
    </div>

In PHP I can use isset($_POST['submit_update_entry']) or isset($_POST['submit_update_entry']).

I've already searched the internet and can only find solutions for Flask or Django. Also searched in class BaseHTTPRequestHandler, but with no luck.

EDIT: So I finally figured out how to do it. Apparently the button name is in field_data = self.rfile.read(length), but when I parsed field_data to get the fields using fields = parse.parse_qs(str(field_data,"UTF-8")), it was getting deleted. I had to change the line to fields = parse.parse_qs(str(field_data,"UTF-8"), keep_blank_values=True) to see submit_update_entry or submit_delete_entry.

  • What's wrong with using Flask or Django? – Rojo Apr 09 '23 at 13:55
  • I'm trying to implement my own web server to learn about how they work. – ijustwantedtosayhi Apr 09 '23 at 14:00
  • Then I suggest you research on what exactly POST requests look like in terms of the content of the body and then use strings and lists to interpret the body. – Rojo Apr 09 '23 at 14:02
  • I know what POST requests look like (e.g. "127.0.0.1 - - [09/Apr/2023 17:03:49] "POST /path.html HTTP/1.1" 200 -"). The issue is that the the button name field is never passed. One alternative would be to modify the path to something like `/path/option1` or `path/option2` etc, but I don't want that. – ijustwantedtosayhi Apr 09 '23 at 14:05

1 Answers1

0

So I finally figured out how to do it. Apparently the button name is in

field_data = self.rfile.read(length)

but when I parsed field_data to get the fields using

fields = parse.parse_qs(str(field_data,"UTF-8"))

it was getting deleted. I had to change the line to

fields = parse.parse_qs(str(field_data,"UTF-8"), keep_blank_values=True)

to see submit_update_entry or submit_delete_entry.