how to pass value from view to controller in web2p

2020-05-04 00:49发布

问题:

I want to insert the value of lang to mysql table. How to pass the value of lang to controller

view
<script type="text/javascript">
    function test(it)
    {
        var lang=it.value;
    }
</script>
<form>
<p>language
<select id="select"  onchange="test(this)">
  <option value ="0">c</option>
  <option value ="1">cc</option>
  <option value="2">pas</option>
  <option value="3">java</option>
  <option value ="4">rb</option>
</select>
</p>
</form>

conteoller
def problem(lang):
{
}

Thanks!

回答1:

web2py doesn't work quite like that. Before proceeding, I suggest you read some of the introductory documenation as well as the documentation on forms.

If you submit a form to a web2py URL, the function that handles that URL can access the form variables in request.vars (also, request.post_vars if submitted via POST and request.get_vars if submitted via GET). In the case of your code, you would need to add a "name" attribute with the value "lang" to the <select> element, in which case, the receiving action could access the value via request.vars.lang. You controller action itself should not take any arguments.

Instead, though, you might find it easier to use web2py's built in forms functionality as noted above.