Click to See Complete Forum and Search --> : Certain Answers in HTML Forms


HarleyDPalmer
04-13-2010, 08:37 PM
Hello!

I use forms quite a bit, so I get the general idea of how to create one with HTML. However, I am working on a new project that needs something more. Is there a way to make the user only input certain information into a field? Not just numbers but literally like a "code" that they must have in order to get to the next step.

Basically, I need to be able to confirm membership of my users and I need them to input a specific code or "password" if you will in order to confirm. How do I tell the input field so only accept a few correct answers? (Like the user can only input 1234, 5678, or 2345 otherwise it won't work?)

Thanks!

tirna
04-13-2010, 08:54 PM
There are a couple of ways to do this - with and without using AJAX, but both would still require a call to a server side script that checks the real password with the password entered by the user.

The real password should be stored in a database but could also be stored in a text file on the server but this is less secure than storing it in a db.

My preference would be to use AJAX:

1) In your form you have an <input> where the user enters a password. The form's submit button is disabled or hidden by default.

2) the <input> in 1) could have an onchange event handler which sends a ajax request to the server to run the script that compares the user entered password with the real one stored on the server.

3) the ajax response then returns something like true or false depending on whether the 2 passowrds match.

4) if ajax returns false, you could display a message on the web page that the user has entered an incorrect password.

5) only if ajax returns a true response do you then enable the submit button or whatever the mechanism for moving to the next step is.

Using AJAX, the user's web page doesn't get refreshed every time they enter a password, but only after they click the submit button.

You could then build on the above by limiting X number of attempts to enter a password etc.

HarleyDPalmer
04-14-2010, 12:39 PM
I don't know how to use AJAX. Can you be a bit more specific? Or what is the other way you mentioned with out AJAX?

tirna
04-14-2010, 07:06 PM
The process is essentially as I described earlier except that you don't have to use ajax.

If you don't use ajax, then the submit button will be enabled by default.

When the user submits the form containing the password, the form's action calls a server side script (PHP, ASP or whatever) that compares the user entered password to the real password stored on the server preferably in a database or some file on disk.

If the 2 passwords match then the server side script redirects the user to the next appropriate page on your website.

If the passwords do not match, then you can display an error message and redisplay the form etc etc.

HarleyDPalmer
04-14-2010, 09:38 PM
I just found this, but would 'accept-charset' work for what I want?

like <input type="text" accept-charset="1234, 5243, 5678">

I was searching around and found that but does anyone know if that does what I want it to do?

I guess I should try it out....

tirna
04-14-2010, 09:44 PM
Don't put the password in your code.

Anyone can right-click your web page, then select view source and they will be able to see your password.

HarleyDPalmer
04-14-2010, 10:51 PM
Okay, I don't know the codage to put into the side script. I know how this is supposed to work, but I can't find anything about the code to put in the side script that you mentioned.

Like I said, I know how it's supposed to work, what you explained earlier, but I don't know the code to make it work.

Thanks for the help.

tirna
04-14-2010, 11:05 PM
ok, then ssuming your web hosting account has access to PHP then maybe have a go at learning at least the basics of PHP and how to create a database (to store your password).

The w3schools has good tutorials on just about anything related to website development.

http://www.w3schools.com/PHP/

A slightly easier, but less secure option, is to store your password in a text file on the server, instead of a db, and then use PHP to open and read the text file and compared the password in it to that the user entered in the form.

The w3schools website also has tutes and examples on server side file handling.

HarleyDPalmer
04-14-2010, 11:14 PM
Thanks so much for all your help! I really appreciate it!

tirna
04-14-2010, 11:21 PM
no problem..

If you decide to have a go at learning PHP and you get stuck, just post your code here and I'm sure I or someone else will try to help. :)