[web2py] Re: Passing JavaScript variable to web2py session variable?

2011-07-08 Thread Ross Peoples
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.


[web2py] Re: Passing JavaScript variable to web2py session variable?

2011-07-08 Thread Vasil Petkov
On Jul 8, 3:02 pm, Ross Peoples ross.peop...@gmail.com wrote:
 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.

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,
url: http://127.0.0.1:8000/games/xs-software/vaucher.html;,
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!


[web2py] Re: Passing JavaScript variable to web2py session variable?

2011-07-08 Thread Ross Peoples
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.


[web2py] Re: Passing JavaScript variable to web2py session variable?

2011-07-08 Thread ron_m
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.


[web2py] Re: Passing JavaScript variable to web2py session variable?

2011-07-07 Thread Vasil Petkov
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}
});
$('form:first').submit();
return false;
}


[web2py] Re: Passing JavaScript variable to web2py session variable?

2011-07-07 Thread Vasil Petkov
On Jul 7, 9:47 pm, Vasil Petkov petko...@dir.bg wrote:
 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}});

         $('form:first').submit();
         return false;
 }

, but apperantly nothing happened.

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