Search code examples
pythoncherrypy

Getting value from a dropBox using Cherrypy in Python


I am new to Cherrypy, Kindly help me how can I get the selected value from dropdown using cherrypy in python.


Solution

  • I assume the dropbox is a element with id="dropDownId"; i assume the server call is done using jquery.

    the value of the currently selected item is

    $('#dropDownId').val();
    

    the currently selected text:

    $('#dropDownId :selected').text();
    

    You can post it to the server with an ajax request:

        queryparams = $.param({     
                            "dropDownVal":$('#dropDownId').val(),
                            "email":$("#email").val(), 
                            "password":$("#password").val(), 
                        });
    
        $.ajax  ({
            url: '/login',
            type: "POST",
            data: queryparams,
        })
    

    On the server side we have a cherrypy funcion, exposed to receive requests for the "login" page:

    class mainPage():
    
    @cherrypy.expose
    def login(self,password=None,email=None,dropDownVal=None):
    
        self.password = password
        self.email = email.strip().lower()
        self.dropDownVal = dropDownVal
    
        cherrypy.log(self.dropDownVal)
    
        return "login ok"
    

    It expects 3 optional parameters (email,password,dropDown), logs the "dropDownVal" and returns the string "ok" to the browser.