Search code examples
javascripthtmlflaskgetweb-frontend

How do I send search parameters to Flask server?


This is my first question! I am trying to enter a search term into a search box, and when I click Submit, I want the search term inserted into this URL:

http://www.hadrians-search.tk/search?search_param=mario?&items_per_page=2&page_number=2

mario would be the search term. This is the search page I am testing with:

http://cs.oswego.edu/~jmcquaid/CSC-380/search3.html

When I enter a search term such as ball into the search box and click Submit, this is the URL I get:

http://hadrians-search.tk/search?search%3Fsearch_param%3D=ball

As you can see, this is clearly not the URL I am intending to get.

Bear in mind that search3.html is just a file I am testing with on the front-end:

<!DOCTYPE html>
<html>
<body>

<form id="myForm" action="http://hadrians-search.tk/search?search_param=">
Search: <input type="text" name="search?search_param="><br><br>
<input type="button" onclick="myFunction()" value="Submit form">
</form>

<script>
    function myFunction() {
    document.getElementById("myForm").submit().action="http://hadrians-search.tk/search?search_param=";
    }
</script>

</body>
</html>

So basically I am trying to send the search term entered in the search box to the Flask server, but it isn't getting sent properly. Should I be trying an HTTP GET Request? I originally tried this, but I couldn't figure out how to integrate an HTTP GET Request with a search box and button. Because of this, I instead tried to use action, as you can see in the file. Any advice that you can provide will be greatly appreciated, and I thank you for your time.


Solution

  • You can pass the parameters via GET. If you have three parameters, you could add the three elements in the form. You can do something like this.

    <!DOCTYPE html>
    <html>
    
    <body>
    <form id="myForm" method="get" 
    action="http://hadrians-search.tk/search">
    search_param: <input type="text" name="search_param"><br><br>
    items_per_page: <input type="text" name="items_per_page"><br><br>
    page_number: <input type="text" name="page_number"><br><br>
    <input value="Submit form" type="submit">
    </form>
    </body>
    </html>