Clemens
Clemens

Reputation: 33

Web2py: Get value from client (view) to controller

I thought that I found the answer to my question in Web2py: Pass a variable from view to controller

I'm trying to adopt the example there in order to get the size a DIV, which finally depends on the size of the browser window (defined relatively). To do so I have the following code in the view

<script>
    var div_size_Value = document.getElementById("model_graph_div").clientWidth;
    ajax('{{=URL('default', 'my_action', vars=dict(div_size_value=div_size_value))}}', [], ':eval');
</script>

But I always get

NameError: name 'div_size_value' is not defined

The error seems to be okay, since the Javascript var div_size_value is addressed within the HTML Helper URL.

Thus my question is: How can I get the DIV size value from the client (view) to a controller?

Thanks a lot for any support!

Best regards Clemens

Upvotes: 0

Views: 790

Answers (1)

Anthony
Anthony

Reputation: 25536

ajax('{{=URL('default', 'my_action', vars=dict(div_size_value=div_size_value))}}',
     [], ':eval');

You are mixing Javascript with Python. In a web2py template, everything inside the {{...}} template delimiters is Python code that gets executed on the server before sending the HTML to the browser. So, in your code, div_size_value is expected to be a Python variable available in the server-side Python environment. Because the URL query string involves a Javascript variable, you must instead build the query string via Javascript, as below:

ajax('{{=URL('default', 'my_action')}}' + '?div_size_value=' + div_size_value, ...)

Upvotes: 1

Related Questions