mardi 1 décembre 2015

Ajax/Python script/SQLite to tell is a username is taken

I'm having trouble getting this going. Here is my ajax:

 <script src="http://ift.tt/1rKdStd"></script>
  <script type="text/javascript">
    $(document).ready(function(){
      $("#username").keyup(function(){
        console.log($("#username").val());
        $.ajax({
        url: "cgi-bin/namecheck.py",
        type: "GET",
        data: {
          stuff: $('#username').val()
        },
        dataType: "json",
        success: function(dat){
          console.dir(dat);
          $("#stuff").text(dat.name);
        },

      })

      });

    });
  </script>

Where "username" is the the id that someone tries to type their new username and "stuff" is a div under that open-ended section that I want to print valid or not valid.

I also reference namecheck.py, and my script that does the actual analysis is here:

#!/usr/bin/python

import sqlite3
import cgi

import cgitb
cgitb.enable()

form = cgi.FieldStorage()
stuff = form['stuff'].value

conn = sqlite3.connect('accounts.db')
c = conn.cursor()

try:
    c.execute('select * from users where username=?', (stuff,))
    all_results = c.fetchall()
    if len(all_results) > 0:
        print "Content-type: application/json"
        print
        print '{"name": "Not Valid"}'
    else:
        print "Content-type: application/json"
        print
        print '{"name": "Valid"}'
except sqlite3.IntegrityError:
    pass

I'm not getting any fatal errors, but nothing seems to be happening when I try to test it. Any help would be great!

Aucun commentaire:

Enregistrer un commentaire