Passing JavaScript variable to web2py session variable?

2,179 views
Skip to first unread message

Vasil Petkov

unread,
Jul 7, 2011, 1:47:52 PM7/7/11
to web2py-users
Hello!

I am having that JavaScript function inside a web2py view:

function submit_form(input_number, input_goal) {
$('input#user_input_number').val(input_number);
$('input#user_input_goal').val(input_goal);
$('form:first').submit();
return false;
}

I would like to assign the value of input_number to the web2py
session.my_var-variable.

As much as i know, to pass input_number to the session.my_var
variable, first an AJAX call must be made. After that, the value of
input_number could be called from web2py request.vars["input_number"],
for example.
The end result should look like:

function submit_form(input_number, input_goal) {
$('input#user_input_number').val(input_number);
$('input#user_input_goal').val(input_goal);
THE AJAX CALL
{{session.my_var = request.vars["input_number"]}}
$('form:first').submit();
return false;
}

The problem is that i do not know how to make that "THE AJAX CALL"
from inside the web2py view.

Could you please give me examles of such a JavaScript to Python
variable passing?

Vasil Petkov

unread,
Jul 7, 2011, 2:47:31 PM7/7/11
to web2py-users
I tried the following:

function submit_form(input_number, input_egn) {
$('input#user_input_number').val(input_number);
$('input#user_input_egn').val(input_egn);
$.ajax({
url: "http://127.0.0.1:8000/games/xs-software/vaucher.html",
method: "POST",
data: {input_n: input_number}

Vasil Petkov

unread,
Jul 7, 2011, 2:50:46 PM7/7/11
to web2py-users
, but apperantly nothing happened.

PS: sorry for the double post. Was a glitch from my side.

Ross Peoples

unread,
Jul 8, 2011, 8:02:00 AM7/8/11
to web...@googlegroups.com
You're doing it right, but there are a couple of things to keep in mind. Using $.post(url, {input_n: input_number}); is easier than using $.ajax, but that's merely preference. The other thing you should know is that if a web2py ticket is generated during an AJAX call, you will not know about it. The call will appear to do nothing, which sounds like what you described.

Whenever I make an AJAX call, this is how I usually do it:

jQuery.post(url, {myvar: myvalue}, function(data) {
    // do something with the returned data from server (only gets called if call was successful).
}).error(function(data) {
    // error is usually HTML, so convert it to plain text for the alert() function.
    alert(data.replace(/(<.*?>)/ig,""));
});

This way, you will know if AJAX was successful or failed.

Vasil Petkov

unread,
Jul 8, 2011, 9:28:45 AM7/8/11
to web2py-users
Hello!

I would like to save the JavaScript variable to a web2py session
variable.

I have tried the following in my view (that has no controller
associated with it):

function submit_form(input_number, input_egn) {
$('input#user_input_number').val(input_number);
$('input#user_input_egn').val(input_egn);
$.ajax({
type: "POST",
data: "{game_id : $
('input#user_input_number').val(input_number)}"
success: function(msg){
alert( "Data Saved: " + msg );
}
});
{{session.game_id = 4}}
{{if request.vars['game_id']:
session.game_id = request.vars['game_id']}}
$('form:first').submit();
return false;
}

, but i receice the following error traceback:

1 Traceback (most recent call last):
2 File "/home/vpetkov/Documents/cashterminal_new/terminal/gluon/
rocket.py", line 1064, in run
3 self.run_app(conn)
4 File "/home/vpetkov/Documents/cashterminal_new/terminal/gluon/
rocket.py", line 1531, in run_app
5 self.environ = environ = self.build_environ(sock_file, conn)
6 File "/home/vpetkov/Documents/cashterminal_new/terminal/gluon/
rocket.py", line 1363, in build_environ
7 request = self.read_request_line(sock_file)
8 File "/home/vpetkov/Documents/cashterminal_new/terminal/gluon/
rocket.py", line 1138, in read_request_line
9 raise SocketTimeout("Socket timed out before request.")
10 SocketTimeout: Socket timed out before request.

I mean, does web2py has a way to RECEIVE JavaScript variables FROM
Python/web2py templates? Where are they stored? And how to call these
JavaScript variables inside {{ }} in a web2py view/template?

Thank you in advance for your answers!

Ross Peoples

unread,
Jul 8, 2011, 9:55:17 AM7/8/11
to web...@googlegroups.com
Going from web2py -> JavaScript is easy:

{{=var}}

However, going from JavaScript -> web2py requires an AJAX call. Since JavaScript is run on the user's machine and not the web server, the two machines have to communicate somehow, which is where AJAX comes in. You should also know that things inside {{ }} will NOT run on the user's machine. So if you are expecting something in these blocks to happen after the page loads, it won't work and you have to use AJAX.

ron_m

unread,
Jul 8, 2011, 11:35:14 AM7/8/11
to web...@googlegroups.com
You need to have the AJAX URL formed in a way that invokes a controller.

eg /application_name/controller_name/function_name/arg0/arg1 .....

and include the variable(s) you want to send back. The variables can be either addition items after the function as above where they show up as args or if they are part of a form POST then the variables would be in the vars dict.

The controller will then validate the variables if need be and store what it needs to in the session. You don't need a view for this.
Reply all
Reply to author
Forward
0 new messages