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 == '[email protected]' 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="status">Please 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:
>> <h3><a 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 <[email protected]>wrote:
>>
>>> both
>>>
>>> <h3><a href="{{=URL('action')}}/**param1/param2">....</a></h3>
>>>
>>> and
>>>
>>> <h3><a 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
>>
>>
--