I have done it both with a MySQL database and an htaccess generated text file of users/passwords.
If you have a very small number of passwords, then a text file is ok, but if you want to have a lot of users and passwords I would go with a database...
To do it either way you use AJAX to send and receive requests and data back and forth to a php script that would do the queries to the database (or read and write to the text file).
There are several good tutorials that I followed that helped me get started...