Solved

Javascript in Python

Posted on 2004-08-23
12
3,212 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:waulsy
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:rjkimble
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
btw, this script represents already a dynamic web-page
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now