Solved

Javascript in Python

Posted on 2004-08-23
12
3,349 Views
Last Modified: 2010-08-05
Hi. I am writing a simple web page. I have a static page which uses Javascript to validate a registration form. I then call a Python script to access a gadfly DB which checks if whether the username already exists.
I need to some how get python to return to the same registration page if the username already exists. When it returns I need it to use the Javascript alert() popup to display an error msg. Else if the username isnt already in use, I will create a dynamic page.
I understand that this is a very inefficient way of doing things and it probably wouldve have been better to create the pages dynamically and usse python to validate the form. But, this is the way it must be done.
Is there a way of getting Python to return and use a Javascript popup??
Ideally, I would have the script just return false or something.. so that the page doesnt need to be refreshed.
Any ideas??
Thanks heaps.

Waulsy.
0
Comment
Question by:waulsy
  • 5
  • 4
  • 3
12 Comments
 
LVL 5

Expert Comment

by:EOL
ID: 11868636
The simple answer is no. You can not validate a username against a server-side database without dynamic web pages. You can also not do any client-side python scripting ( altough that would be very nice ) and java-script can not portably connect by itself to external datasources.
0
 
LVL 5

Expert Comment

by:EOL
ID: 11868656
Btw. without dynamic pages, what's the use of a login anyway? If you just want to restrict access to an area, use htaccess ( can also bind against a db )
0
 

Author Comment

by:waulsy
ID: 11869137
Hey, thanx for the comment.
I knew it wouldnt be easy. We have been told to use Javascript for the validation of a registration form. Then we need to somehow store the registration data (either textfile or Db), and dynamically create a page on user login.
The problem being, that is going to be hard to keep consistency about the error messages, because validation is required to be done with Javascript (e.g input boxes). And I am assuming I will need to use Python (e.g. store input data, and validate the stored data against the input data). Unless it is possible to create textfiles with Javascript? And also then read in from them.
Any advice would be greatly appreciated.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:waulsy
ID: 11869187
Hey,
I am thinking I mite need to recreate the static pages dynamically when the login details are incorrect and get a Javascript alert to popup on page load( is that possible?). Doesnt make sense to do it that way I know. But we need to submit some static pages.
0
 
LVL 9

Expert Comment

by:rjkimble
ID: 11870626
You can insert a JavaScript alert on page load. However, I don't understand how you're using JavaScript to validate the registration, unless the JavaScript is being run on the server.

Python is an excellent CGI language, however. Are you sure you can't just submit the page to a Python CGI script on the server?
0
 

Author Comment

by:waulsy
ID: 11870874
Hi rjkimble,
Yeh I cant just submit the page to python script. We must use Javascript to do some validation. I am only using javascript to make sure certain fields arent empty or invalid etc, not to check against the db.
The reason i was asking was becuase, with the current situation, it is impossible to keep consistency about the error messages.
0
 
LVL 9

Expert Comment

by:rjkimble
ID: 11871274
Perhaps you could use Python to generate a JavaScript file on the server and include that file in your HTML. That might help you keep your error messages consistent. Is that the kind of idea you're looking for?
0
 

Author Comment

by:waulsy
ID: 11877002
Yeh I suppose that is what I am looking for. Is it possible to tell Python to return to a static page? Because I still need Python to validate login details, but then wanted a Javascript popup if values are wrong. Am I asking the impossible?
0
 
LVL 9

Assisted Solution

by:rjkimble
rjkimble earned 60 total points
ID: 11877264
You could post your login credentials to a Python CGI script, which would validate them. If the validation fails, you could redirect to a suitable static page; and if the validation succeeds, you could redirect to the first page for logged in users. I'm not really sure how your site is structured. There a lot of possibilities, but if you're trying to serve up a big web site, it could get kind of messy.

It would be good to check out the Python cgi module.
0
 

Author Comment

by:waulsy
ID: 11877381
Okay that sounds good. The site is only small, so I dont need to worry to much.
Are you able to tell me how to redirect a to a static page in Python. Then we can end this forum.
Thanks for all your help.
Also, that static page would be able to contain Javascript rite?
0
 
LVL 5

Accepted Solution

by:
EOL earned 65 total points
ID: 11878219
you can use a script like this for making redircects. say you store it in your cgi-bin as redirect, then it works like.

http://yoursite.com/cgi-bin/redirect?url=http://www.yoursite.com/login

ok, here's the script. Be sure to set executable permissions.

#! /usr/bin/python
import cgi

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

print 'content-type: text/html'
print 'location: %s\n'%url
print '''<HTML>
         <META http-equiv="Refresh" content="0;url=%(url)s">
         <BODY onload="try {self.location.href='%(url)s' } catch(e) {}"><a
           href="%(url)s">%(url)s</a></BODY>
         </HTML>'''%{'url':url}
0
 
LVL 5

Expert Comment

by:EOL
ID: 11878227
btw, this script represents already a dynamic web-page
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question