I hope I don't make your eyes bleed by puting all the code here but
there is no other way to show it to you.
I made change only in one place of the code to gather "login" value:
{login:value}
Oryginaly it was: {name:value}.
$.post("{{=URL(r=request,c='default',f='ajaxuserexist')}}",
{login:value},function(result){
Below is the whole content of index.html
{{extend 'layout.html'}}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html;
charset=utf-8">
<title>Ajax User Validation with web2py by Martin Sagastume</
title>
<style type="text/css">
<!--
body { font-family:Arial,Verdana,Sans-serif; }
input[type=text]{
font-size:12px;
color:#666666;
background-color:#ffffff;
padding-top:5px;
width:200px;
height:20px;
border:1px solid #999999;
}
#resultbox { font-size:11px; }
.msg { color:blue; }
.success { color:green; }
.error { color:red; }
//-->
</style>
<script type="text/javascript"
src="{{=URL(r=request,c='static',f='jquery.js')}}"></script>
<script type="text/javascript">
var submit = false;
$(document).ready(function(){
$("form").submit(function() {
return submit;
});
});
function getData(value){
if(value != ""){
$
("#resultbox").removeClass().addClass('msg').text('Validating...').fadeIn(10);
$.post("{{=URL(r=request,c='default',f='ajaxuserexist')}}",
{login:value},function(result){
if(result=='yes'){
$
("#resultbox").removeClass().addClass('error').text('Login already
taken').fadeTo(900,1);
submit = false;
}else{
$
("#resultbox").removeClass().addClass('success').text('Login is
available for registration!').fadeTo(900,1);
submit = true;
}
});
}else{
$("#resultbox").removeClass().addClass('msg').text('This
field is required');
}
}
</script>
</head>
<body>
<form id="form1" method="post" action="page2">
<label for="country">Login:</label><br />
<input type="text" id="login" name="login"
onblur="getData(this.value)" /><br />
<div id="resultbox" class="msg"></div><br />
<input type="submit" id="bsubmit" name="bsubmit"
value="Submit" />
</form>
</body>
</html>
On 30 Cze, 22:12, mdipierro <[email protected]> wrote:
> This should give you an error because the print(username) is outside
> the funciton the function that defines username, after the function
> returns. How are you passing the username to the ajax call? Did you
> check with firebug that is calls the correct url?
>
> On 30 Giu, 15:01, elfuego1 <[email protected]> wrote:
>
> > There was NO error messages. The code just didn't work.
> > After inserting:
> > print repr(username) to default.py I got:
>
> > # -*- coding: utf-8 -*-
>
> > #########################################################################
> > ## This is a samples controller
> > ## - index is the default action of any application
> > ## - user is required for authentication and authorization
> > ## - download is for downloading files uploaded in the db (does
> > streaming)
> > ## - call exposes all registered services (none by default)
> > #########################################################################
>
> > def index():
> > """
> > example action using the internationalization operator T and flash
> > rendered by views/default/index.html or views/generic.html
> > """
> > response.flash = T('Welcome to web2py')
> > return dict(message=T('Hello World'))
>
> > def user():
> > """
> > exposes:
> > http://..../[app]/default/user/login
> > http://..../[app]/default/user/logout
> > http://..../[app]/default/user/register
> > http://..../[app]/default/user/profile
> > http://..../[app]/default/user/retrieve_password
> > http://..../[app]/default/user/change_password
> > use @auth.requires_login()
> > @auth.requires_membership('group name')
> > @auth.requires_permission('read','table name',record_id)
> > to decorate functions that need access control
> > """
> > return dict(form=auth())
>
> > def download():
> > """
> > allows downloading of uploaded files
> > http://..../[app]/default/download/[filename]
> > """
> > return response.download(request,db)
>
> > def call():
> > """
> > exposes services. for example:
> > http://..../[app]/default/call/jsonrpc
> > decorate with @services.jsonrpc the functions to expose
> > supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
> > """
> > session.forget()
> > return service()
>
> > def page2():
> > name = request.vars.login
> > #db.auth_user.insert(name=name)
> > return dict(name=name)
>
> > def ajaxuserexist():
> > username = request.vars.values()[0]
> > return db(db.auth_user.login==username).count() and 'yes' or
> > 'no'
>
> > print repr(username)
> > response._vars=response._caller(index)
>
> > On 30 Cze, 21:51, mdipierro <[email protected]> wrote:
>
> > > You can simplify it a bit
>
> > > def ajaxuserexist():
> > > username = request.vars.values()[0]
> > > return db(db.auth_user.login==username).count() and 'yes' or
> > > 'no'
>
> > > although it will not solve your problem.
> > > There is nothing special in table auth_user so the problem is
> > > somewhere else.
>
> > > I suggest you insert a print repr(username).
> > > Do you get any thicket? What does it say?
>
> > > massimo
>
> > > On 30 Giu, 14:29, elfuego1 <[email protected]> wrote:
>
> > > > During registration I want to check on the fly, if login chosen by the
> > > > user is already in use or is it not in db and available to be used.
>
> > > > def ajaxuserexist():
> > > > username = request.vars.values()[0]
> > > > query = (db.auth_user.login==username)
> > > > numres = db(query).count()
> > > > if numres > 0 :
> > > > return 'yes'
> > > > else:
> > > > return 'no'
>
> > > > When I test it on any other table that doesn't have prefix auth_ then
> > > > the above code works, but unfortunatelly I need to test auth_user
> > > > table :-(
>
> > > > On 30 Cze, 21:19, Chris S <[email protected]> wrote:
>
> > > > > Probably a dumb question, but what are you checking the user against?
>
> > > > > Both my and your example above are checking for username and you
> > > > > didn't define a username in your auth_user.
>
> > > > > Your query then is not working as:
>
> > > > > def userexist(emailcheck):
> > > > > if db(db.auth_user.email==emailcheck).count() > 0:
> > > > > return 'yes'
> > > > > else:
> > > > > return 'no'
>
> > > > > I know I've returned searches on auth_user it's no different than
> > > > > other tables. Just gotta find whey your getting no results. Might
> > > > > help if you post the exact search code that goes with the above
> > > > > auth_user and the error message that's generated.
>
> > > > > On Jun 30, 2:12 pm, elfuego1 <[email protected]> wrote:
>
> > > > > > Since I had added some fields (and intend to customize it further)
> > > > > > to
> > > > > > my auth_user table the code for auth_user in db.py looks like that:
>
> > > > > > from gluon.tools import *
> > > > > > auth=Auth(globals(),db)
>
> > > > > > db.define_table('auth_user',
> > > > > > SQLField('login', 'string', length=50, default=''),
> > > > > > SQLField('password', 'password', length=512, readable=False,
> > > > > > label='Password'),
> > > > > > SQLField('registration_key', length=512, default= '',
> > > > > > writable=False, readable=False),
> > > > > > SQLField('reset_password_key', length=512, default='',
> > > > > > writable=False, readable=False),
> > > > > > SQLField('first_name', length=128,default=''),
> > > > > > SQLField('last_name', length=128,default=''),
> > > > > > SQLField('email', length=128,default='', unique=True),
> > > > > > SQLField('phone', 'string', length=30, default=''),
> > > > > > )
>
> > > > > > On 30 Cze, 20:59, Chris S <[email protected]> wrote:
>
> > > > > > > And you've defined auth in db.py with:
>
> > > > > > > from gluon.tools import Auth
> > > > > > > auth = Auth(globals(), db)
> > > > > > > auth.define_tables()
>
> > > > > > > I've done searches on auth_user before... I think.
>
> > > > > > > On Jun 30, 1:44 pm, elfuego1 <[email protected]> wrote:
>
> > > > > > > > Unfortunatelly it doesn't.
> > > > > > > > I can access any other table that's available through my
> > > > > > > > application
> > > > > > > > but I can't get any value out of auth_user table.
> > > > > > > > Is it somehow protected?
> > > > > > > > Do I need to add some extra piece of code to expose them in my
> > > > > > > > application in order to be able to acqiure any value out of
> > > > > > > > them?
>
> > > > > > > > Desperate searcher.
>
> > > > > > > > On 30 Cze, 06:25, Chris S <[email protected]> wrote:
>
> > > > > > > > > I don't guess I follow. Isn't that the same as:
>
> > > > > > > > > def userexist(namecheck):
> > > > > > > > > if db(db.auth_user.username==namecheck).count() > 0:
> > > > > > > > > return 'yes'
> > > > > > > > > else:
> > > > > > > > > return 'no'
>
> > > > > > > > > So I"m saying your querry should be:
> > > > > > > > > query = (db.auth_user.username==username)
>
> > > > > > > > > Hope that helps
>
> > > > > > > > > On Jun 29, 5:34 pm, elfuego1 <[email protected]> wrote:
>
> > > > > > > > > > Hello,
>
> > > > > > > > > > On this
> > > > > > > > > > page:http://web2pyslices.com/main/slices/take_slice/53Ihave
> > > > > > > > > > found a great pice of code which allows to check on the
> > > > > > > > > > fly if there
> > > > > > > > > > is an exact value already in database.
>
> > > > > > > > > > Oryginal code:
>
> > > > > > > > > > def ajaxuserexist():
> > > > > > > > > > username = request.vars.values()[0]
> > > > > > > > > > query = db.users.name.like(username)
> > > > > > > > > > numres = db(query).count()
> > > > > > > > > > if numres > 0 :
> > > > > > > > > > return 'yes'
>
> > > > > > > > > > return 'no'
>
> > > > > > > > > > But when I try to implement the same solution on auth_user
> > > > > > > > > > table for
> > > > > > > > > > login column it stops working:
>
> > > > > > > > > > query = db.auth_users.login.like(username)
>
> > > > > > > > > > Do you know some solution/workaround to this problem?
>
> > > > > > > > > > Best regards.