Re: [web2py] Re: Pass multiple arguments to URL through javascript
Try this it'll give you a form, intercept the form submission and instead do an ajax call to the validate page which checks the email and password and if that's correct will return True and the page will get forwarded via JS to the next page with your email password in the URL as args. Why do the redirect via javascript rather than just having the controller do the redirect though? Controller (default.py) - def index(): example action using the internationalization operator T and flash rendered by views/default/index.html or views/generic.html response.title = Passing Args to URL through JS response.subtitle = T('Home') form = SQLFORM.factory( Field('email', 'string', requires=IS_NOT_EMPTY(IS_EMAIL())), Field('password', 'string', requires=IS_NOT_EMPTY()), _name='foo_form', _id='foo_form') return dict(message=T('Demo'), form = form) def validate(): import gluon.contrib.simplejson as sj print request.vars print request.args try: if request.vars.email == 'm...@email.com' and request.vars.password == 'mypassword': #success return sj.dumps(dict(success=True, message='Success!')) else: #fail return sj.dumps(dict(error=True, message='Oops wrong login info!')) except: #fail return sj.dumps(dict(error=True, message='Login Failed, Please Try Again!')) def receiving_page(): response.title = Passing Args to URL through JS response.subtitle = T('Results') #get passed args email = request.args[0] password = request.args[1] return dict(email = email, password = password) View (default/index.html) {{left_sidebar_enabled=right_sidebar_enabled=False}} {{extend 'layout.html'}} h1{{=message}}/h1 {{=form}} div id=statusPlease Login/div script type=text/javascript $(function() { $('#foo_form').bind('submit',function(event){ var email = $('#no_table_email').val(); var password = $('#no_table_password').val(); alert(Submitting +email+ +password); $.post('{{=URL(r=request, f=validate)}}', $(this).serialize(), function(json) { if(json.error) { //alert(json.message); $('#status').html(json.message); } else { //alert(json.message); $('#status').html(json.message); destination ='{{=URL(r=request, f=receiving_page)}}/'+email +'/'+password; //alert(destination); parent.window.location = destination; } }, 'json'); return false; }); }); /script {{block left_sidebar}}New Left Sidebar Content{{end}} {{block right_sidebar}}New Right Sidebar Content{{end}} - Good Luck! ~Brian On Friday, September 21, 2012 11:20:44 AM UTC-5, jjcurats wrote: good day great folks..i've been encountering same problem here..i have 2 variables in my function to pass in my controller...i just cant do it right trying lot of options already..here's my code: $(function() { $('#client-login').bind('submit',function(event){ var email = $('#email').val(); var password = $('#password').val(); $.post('{{ constant('BB_URL_API') }}guest/client/login', $(this).serialize(), function(json) { if(json.error) { alert(json.error.message); } else { parent.window.location =' http://localhost/course_booking/index.php/display/client_info/'+email +password;//this doesnt work...what is the right approach in this,,pls help } }, 'json'); return false; }); }); and how should my controller look...?thanks people On Monday, May 21, 2012 10:49:24 AM UTC+8, Bruce Wade wrote: If param1 and param2 are from javascript: 1) You can not write the code in headers as you have current written you will have to use something like the following: h3a id='url_with_params'/a/h3 // javascript function: function generateURL(param1, param2) { url = '/action/' + param1 + / + param2; $(url_with_params).attr('href', url); } On Sun, May 20, 2012 at 7:41 PM, Ashraf Mansour ad...@aqar-riyadh.comwrote: both h3a href={{=URL('action')}}/**param1/param2/a/h3 and h3a href={{=URL('action')}}/**param1/param2/a/h3 are not working ( they are not showing the values of param1 and param2 ) -- -- Regards, Bruce Wade http://ca.linkedin.com/in/brucelwade http://www.wadecybertech.com http://www.fittraineronline.com - Fitness Personal Trainers Online http://www.warplydesigned.com --
Re: [web2py] Re: Pass multiple arguments to URL through javascript
good day great folks..i've been encountering same problem here..i have 2 variables in my function to pass in my controller...i just cant do it right trying lot of options already..here's my code: $(function() { $('#client-login').bind('submit',function(event){ var email = $('#email').val(); var password = $('#password').val(); $.post('{{ constant('BB_URL_API') }}guest/client/login', $(this).serialize(), function(json) { if(json.error) { alert(json.error.message); } else { parent.window.location ='http://localhost/course_booking/index.php/display/client_info/'+email +password;//this doesnt work...what is the right approach in this,,pls help } }, 'json'); return false; }); }); and how should my controller look...?thanks people On Monday, May 21, 2012 10:49:24 AM UTC+8, Bruce Wade wrote: If param1 and param2 are from javascript: 1) You can not write the code in headers as you have current written you will have to use something like the following: h3a id='url_with_params'/a/h3 // javascript function: function generateURL(param1, param2) { url = '/action/' + param1 + / + param2; $(url_with_params).attr('href', url); } On Sun, May 20, 2012 at 7:41 PM, Ashraf Mansour ad...@aqar-riyadh.comjavascript: wrote: both h3a href={{=URL('action')}}/**param1/param2/a/h3 and h3a href={{=URL('action')}}/**param1/param2/a/h3 are not working ( they are not showing the values of param1 and param2 ) -- -- Regards, Bruce Wade http://ca.linkedin.com/in/brucelwade http://www.wadecybertech.com http://www.fittraineronline.com - Fitness Personal Trainers Online http://www.warplydesigned.com --
Re: [web2py] Re: Pass multiple arguments to URL through javascript
Thank you, Bruce. h3a id='url_with_params'/a/h3 document.getElementById('url_with_params').href = {{=URL('action')}} + / + param1 ; is working fine.
Re: [web2py] Re: Pass multiple arguments to URL through javascript
Thank you, Anthony. h3a id='url_with_params'/a/h3 document.getElementById('url_with_params').href = {{=URL('action')}} + / + param1 ; is working fine.
Re: [web2py] Re: Pass multiple arguments to URL through javascript
{{=URL('controller','action', args=[arg1, arg2])}} On May 20, 2012 10:18 AM, Ashraf Mansour ad...@aqar-riyadh.com wrote: I tried this h3 {{=A(' . ',_href=URL('action'))}}/param1/param2 /h3 and it did not work.:) what is the right way of doing it? On Saturday, December 3, 2011 7:37:58 PM UTC+3, Anthony wrote: script var param1 = 'value1' ; var param2 = 'value2' {{URL(r=request,c='static',f=**'action')}}/+param1param2 You need to use = to write the output of URL. {{=URL(...)}} + '/' + param1 + '/' + param2 Anthony
Re: [web2py] Re: Pass multiple arguments to URL through javascript
thanks for the immediate reply. param1 and param2 are determined on the client side. h3 {{=A(' . ',_href=URL('action'))}}/param1/param2 /h3 how can it be generated by javascript?
Re: [web2py] Re: Pass multiple arguments to URL through javascript
Why would you do the code like that?? [?] Updated: script jQuery(function() { var param1 = 'hello' var param2 = 'world' jQuery('a#mylink').attr('href', '{{=URL(action, args=[' + param1 + ',' + param2 + '])}}'); }); /script h3{{=A('...', _href='', _id='mylink')}}/h3 On Sun, May 20, 2012 at 4:36 PM, Anthony abasta...@gmail.com wrote: On Sunday, May 20, 2012 3:43:38 PM UTC-4, Ashraf Mansour wrote: thanks for the immediate reply. param1 and param2 are determined on the client side. how can it be generated by javascript? It depends. Here's one example: script jQuery(function() { var param1 = 'hello' var param2 = 'world' jQuery('a#mylink').attr('href', '{{=URL(action)}}' + '/' + param1 + '/' + param2); }); /script h3{{=A('...', _href='', _id='mylink')}}/h3 When the page is loaded, the href of the mylink anchor tag will be replaced with the action URL along with the values of param1 and param2 appended. Of course, you'll need some way to set the values of param1 and param2, and there may be some other event that should trigger the replacement -- it depends what you're trying to do. Anthony -- -- Regards, Bruce Wade http://ca.linkedin.com/in/brucelwade http://www.wadecybertech.com http://www.fittraineronline.com - Fitness Personal Trainers Online http://www.warplydesigned.com 35E.gif
Re: [web2py] Re: Pass multiple arguments to URL through javascript
He said param1 and param2 are determined on the client side -- which means they cannot be set on the server side by web2py. Of course, I don't know what he's actually doing on the client side, so this exact code may or may not be relevant. Do you have an alternative suggestion? Anthony On Sunday, May 20, 2012 7:57:10 PM UTC-4, Bruce Wade wrote: Why would you do the code like that?? Updated: script jQuery(function() { var param1 = 'hello' var param2 = 'world' jQuery('a#mylink').attr('href', '{{=URL(action, args=[' + param1 + ',' + param2 + '])}}'); }); /script h3{{=A('...', _href='', _id='mylink')}}/h3 On Sun, May 20, 2012 at 4:36 PM, Anthony abasta...@gmail.com wrote: On Sunday, May 20, 2012 3:43:38 PM UTC-4, Ashraf Mansour wrote: thanks for the immediate reply. param1 and param2 are determined on the client side. how can it be generated by javascript? It depends. Here's one example: script jQuery(function() { var param1 = 'hello' var param2 = 'world' jQuery('a#mylink').attr('href', '{{=URL(action)}}' + '/' + param1 + '/' + param2); }); /script h3{{=A('...', _href='', _id='mylink')}}/h3 When the page is loaded, the href of the mylink anchor tag will be replaced with the action URL along with the values of param1 and param2 appended. Of course, you'll need some way to set the values of param1 and param2, and there may be some other event that should trigger the replacement -- it depends what you're trying to do. Anthony -- -- Regards, Bruce Wade http://ca.linkedin.com/in/brucelwade http://www.wadecybertech.com http://www.fittraineronline.com - Fitness Personal Trainers Online http://www.warplydesigned.com
Re: [web2py] Re: Pass multiple arguments to URL through javascript
Well honestly if it was a dynamic link I wouldn't even use URL as good style of JS programming is putting the code in a .js if this is the case then the web2py parser cannot even access the URL to parse it. (At least I have never been successful with that process.) If they are getting the params at a later time and in a js file there is no need in using the URL just use url = '/path/' + param + '/' + param; The only problem with this is if he changes the path, however if he changes the path he would be required to update URL() anyways. I have fell into the trap on trying to rely on the helpers to much myself in the past. Some places in your code you shouldn't use them. Trying to use them for everything kind of forces you to change your logic to bend around web2py instead of having web2py work with your logic. Also whenever you use URL instead of the direct url='/path' the framework is forced to do more function calls, in some situations like this one it isn't really worth having the extra function call and it is actually more typing :D On Sun, May 20, 2012 at 5:19 PM, Anthony abasta...@gmail.com wrote: He said param1 and param2 are determined on the client side -- which means they cannot be set on the server side by web2py. Of course, I don't know what he's actually doing on the client side, so this exact code may or may not be relevant. Do you have an alternative suggestion? Anthony On Sunday, May 20, 2012 7:57:10 PM UTC-4, Bruce Wade wrote: Why would you do the code like that?? Updated: script jQuery(function() { var param1 = 'hello' var param2 = 'world' jQuery('a#mylink').attr('href'**, '{{=URL(action, args=[' + param1 + ',' + param2 + '])}}'); }); /script h3{{=A('...', _href='', _id='mylink')}}/h3 On Sun, May 20, 2012 at 4:36 PM, Anthony abasta...@gmail.com wrote: On Sunday, May 20, 2012 3:43:38 PM UTC-4, Ashraf Mansour wrote: thanks for the immediate reply. param1 and param2 are determined on the client side. how can it be generated by javascript? It depends. Here's one example: script jQuery(function() { var param1 = 'hello' var param2 = 'world' jQuery('a#mylink').attr('href'**, '{{=URL(action)}}' + '/' + param1 + '/' + param2); }); /script h3{{=A('...', _href='', _id='mylink')}}/h3 When the page is loaded, the href of the mylink anchor tag will be replaced with the action URL along with the values of param1 and param2 appended. Of course, you'll need some way to set the values of param1 and param2, and there may be some other event that should trigger the replacement -- it depends what you're trying to do. Anthony -- -- Regards, Bruce Wade http://ca.linkedin.com/in/**brucelwadehttp://ca.linkedin.com/in/brucelwade http://www.wadecybertech.com http://www.fittraineronline.**com http://www.fittraineronline.com - Fitness Personal Trainers Online http://www.warplydesigned.com -- -- Regards, Bruce Wade http://ca.linkedin.com/in/brucelwade http://www.wadecybertech.com http://www.fittraineronline.com - Fitness Personal Trainers Online http://www.warplydesigned.com
Re: [web2py] Re: Pass multiple arguments to URL through javascript
thank you for your interest. let me brief what i am doing. I am building an application accessing google maps api v3. I need the function that decide if a certain point is within certain bounds. my understanding is that i need to do that in the view ( by passing the point and the coordinates of the bounds ). the view js can access them if they in {{ }}. then I need to return the output of the function to the next action, by clicking h3 {{=A(' . ',_href=URL('action'))}}/param1/param2 /h3 it did not work (it does not write the values of param1 and param2 ) and h3 {{=A(' . ',_href=URL('action',args=[param1,param2]))}} /h3 is not working ( param1, param2 is not defind within {{ }} ) is there a solution without using jquery?
Re: [web2py] Re: Pass multiple arguments to URL through javascript
You do realise that you are missing the closing ? h3 {{=A(' . ',_href=URL('action'))}}/param1/param2 /h3 Also what you are trying to do in this code will never work. h3 {{=A(' . ',_href=URL('action'))}}/param1/param2 /h3 becomes h3 a href='/action'/a/param1/param2 /h3 Try this: h3a href={{=URL('action')}}/param1/param2/a/h3 On Sun, May 20, 2012 at 7:09 PM, Ashraf Mansour ad...@aqar-riyadh.comwrote: thank you for your interest. let me brief what i am doing. I am building an application accessing google maps api v3. I need the function that decide if a certain point is within certain bounds. my understanding is that i need to do that in the view ( by passing the point and the coordinates of the bounds ). the view js can access them if they in {{ }}. then I need to return the output of the function to the next action, by clicking h3 {{=A(' . ',_href=URL('action'))}}/**param1/param2 /h3 it did not work (it does not write the values of param1 and param2 ) and h3 {{=A(' . ',_href=URL('action',args=[param1,param2]))}} /h3 is not working ( param1, param2 is not defind within {{ }} ) is there a solution without using jquery? -- -- Regards, Bruce Wade http://ca.linkedin.com/in/brucelwade http://www.wadecybertech.com http://www.fittraineronline.com - Fitness Personal Trainers Online http://www.warplydesigned.com
Re: [web2py] Re: Pass multiple arguments to URL through javascript
I wasn't suggesting he should use the URL() function, just pointing out that it *can't* be used to build a URL on the client side, and showing a simple example of how to build a URL on the client side via Javascript (use of the URL() function was incidental and not central to the point of the example). In any given case, it may or may not make sense to use the URL() function to build the base URL (vs. hard-coding it in a .js file), and I wouldn't state a blanket rule either way (if you're using the web2py rewrite system, it's generally a good idea to use URL() whenever feasible). Also, I hadn't realized you made an update to my code example. The update wouldn't work because it attempts to put Javascript variables inside Python code that must be executed on the server. Anthony On Sunday, May 20, 2012 8:56:51 PM UTC-4, Bruce Wade wrote: Well honestly if it was a dynamic link I wouldn't even use URL as good style of JS programming is putting the code in a .js if this is the case then the web2py parser cannot even access the URL to parse it. (At least I have never been successful with that process.) If they are getting the params at a later time and in a js file there is no need in using the URL just use url = '/path/' + param + '/' + param; The only problem with this is if he changes the path, however if he changes the path he would be required to update URL() anyways. I have fell into the trap on trying to rely on the helpers to much myself in the past. Some places in your code you shouldn't use them. Trying to use them for everything kind of forces you to change your logic to bend around web2py instead of having web2py work with your logic. Also whenever you use URL instead of the direct url='/path' the framework is forced to do more function calls, in some situations like this one it isn't really worth having the extra function call and it is actually more typing :D On Sun, May 20, 2012 at 5:19 PM, Anthony abasta...@gmail.com wrote: He said param1 and param2 are determined on the client side -- which means they cannot be set on the server side by web2py. Of course, I don't know what he's actually doing on the client side, so this exact code may or may not be relevant. Do you have an alternative suggestion? Anthony On Sunday, May 20, 2012 7:57:10 PM UTC-4, Bruce Wade wrote: Why would you do the code like that?? Updated: script jQuery(function() { var param1 = 'hello' var param2 = 'world' jQuery('a#mylink').attr('href'**, '{{=URL(action, args=[' + param1 + ',' + param2 + '])}}'); }); /script h3{{=A('...', _href='', _id='mylink')}}/h3 On Sun, May 20, 2012 at 4:36 PM, Anthony abasta...@gmail.com wrote: On Sunday, May 20, 2012 3:43:38 PM UTC-4, Ashraf Mansour wrote: thanks for the immediate reply. param1 and param2 are determined on the client side. how can it be generated by javascript? It depends. Here's one example: script jQuery(function() { var param1 = 'hello' var param2 = 'world' jQuery('a#mylink').attr('href'**, '{{=URL(action)}}' + '/' +param1 + '/' + param2); }); /script h3{{=A('...', _href='', _id='mylink')}}/h3 When the page is loaded, the href of the mylink anchor tag will be replaced with the action URL along with the values of param1 and param2 appended. Of course, you'll need some way to set the values of param1 and param2, and there may be some other event that should trigger the replacement -- it depends what you're trying to do. Anthony -- -- Regards, Bruce Wade http://ca.linkedin.com/in/**brucelwadehttp://ca.linkedin.com/in/brucelwade http://www.wadecybertech.com http://www.fittraineronline.**com http://www.fittraineronline.com - Fitness Personal Trainers Online http://www.warplydesigned.com -- -- Regards, Bruce Wade http://ca.linkedin.com/in/brucelwade http://www.wadecybertech.com http://www.fittraineronline.com - Fitness Personal Trainers Online http://www.warplydesigned.com
Re: [web2py] Re: Pass multiple arguments to URL through javascript
both h3a href={{=URL('action')}}/param1/param2/a/h3 and h3a href={{=URL('action')}}/param1/param2/a/h3 are not working ( they are not showing the values of param1 and param2 )
Re: [web2py] Re: Pass multiple arguments to URL through javascript
let me brief what i am doing. I am building an application accessing google maps api v3. I need the function that decide if a certain point is within certain bounds. my understanding is that i need to do that in the view ( by passing the point and the coordinates of the bounds ). the view js can access them if they in {{ }}. What do you mean by needing to do that in the view? Do you mean in the browser? If so, that's not the same thing as in the view. In web2py, the view is a special template that can include Python code. The view is executed on the server, and the output of that execution is what is sent to the browser. The browser does not see any of the Python code (i.e., none of the {{...}} end up in the browser). is there a solution without using jquery? It's still not clear what you are doing. What are param1 and param2? Are they determined dynamically within the browser? If so, you'll need Javascript to build the URL in the browser (it doesn't have to be jQuery specifically, but some form of Javascript). If param1 and param2 are known on the server at the time the page is being constructed by web2py, then you can simply include them as args in the call to the URL() function. Anthony
Re: [web2py] Re: Pass multiple arguments to URL through javascript
both h3a href={{=URL('action')}}/param1/param2/a/h3 and h3a href={{=URL('action')}}/param1/param2/a/h3 are not working ( they are not showing the values of param1 and param2 ) You are simply putting the literal strings param1 and param2 directly into your HTML there, so of course the values of those variables won't be shown. What are param1 and param2? Are they Javascript variables? If so, where are they defined? If they are Javascript variables, then you have to use Javascript to build the URL -- you cannot just paste Javascript variables directly into HTML and have the browser translate them into their values. Anthony
Re: [web2py] Re: Pass multiple arguments to URL through javascript
If param1 and param2 are from javascript: 1) You can not write the code in headers as you have current written you will have to use something like the following: h3a id='url_with_params'/a/h3 // javascript function: function generateURL(param1, param2) { url = '/action/' + param1 + / + param2; $(url_with_params).attr('href', url); } On Sun, May 20, 2012 at 7:41 PM, Ashraf Mansour ad...@aqar-riyadh.comwrote: both h3a href={{=URL('action')}}/**param1/param2/a/h3 and h3a href={{=URL('action')}}/**param1/param2/a/h3 are not working ( they are not showing the values of param1 and param2 ) -- -- Regards, Bruce Wade http://ca.linkedin.com/in/brucelwade http://www.wadecybertech.com http://www.fittraineronline.com - Fitness Personal Trainers Online http://www.warplydesigned.com
Re: [web2py] Re: Pass multiple arguments to URL through javascript
On Dec 3, 2011, at 11:32 PM, Vineet wrote: @Anthony, thanks. Your method worked. Secondly, if I want to pass arguments by name, would it be okay to do something like this {{=URL(...)}} + '/' + 'p1=' + param1 + '/' + 'p2=' + param2 Almost. You want: {{=URL(...)}} + '?p1=' + param1 + 'p2=' + param2 ...then look for p1 p2 in request.vars.