A complete HTML + CSS + JavaScript + PHP/MySQL student registration form: client-side validation, an AJAX submit (no page reload), server-side validation, hashed passwords, and an optional profile-photo upload.
| File | Purpose |
|---|---|
index.html |
The form markup |
style.css |
Styling |
script.js |
Client-side validation + fetch() submit |
register.php |
Server-side validation, hashing, and DB insert |
db_connect.php |
PDO/MySQL connection (edit your credentials here) |
database.sql |
Creates the database and students table |
uploads/ |
Where uploaded profile photos are stored |
- Requirements: PHP 7.4+ with PDO MySQL, and MySQL or MariaDB.
- Import the schema:
mysql -u root -p < database.sql - Edit
db_connect.phpand setDB_HOST,DB_NAME,DB_USER,DB_PASSto match your setup. - Keep all files in one folder (e.g. your project root or
htdocs/student-registration/) — the HTML referencesstyle.css,script.js, andregister.phpas relative paths. - Run it through a PHP server (opening
index.htmldirectly as afile://URL won't work, since the form'sfetch()call needs an HTTP origin):Then visitphp -S localhost:8000
http://localhost:8000. This also works unchanged under XAMPP, WAMP, MAMP, or a shared PHP host. - Make sure the
uploads/folder is writable by PHP (chmod 755 uploads, or775if your web server user needs group write access).register.phpwill also try to create it automatically if it's missing.
- script.js validates every field on blur/change and again on submit, then sends the data as
FormDatatoregister.phpviafetch(). It reads back a JSON response and shows a success banner or highlights the specific fields the server rejected. - register.php re-validates everything server-side (client-side checks can always be bypassed), checks the email isn't already registered, hashes the password with
password_hash(), stores the optional photo underuploads/with a randomized filename, and inserts the record using a prepared PDO statement.
This covers the core registration flow, but a production deployment should add:
- HTTPS everywhere (passwords and personal data are sent in the request body).
- CSRF protection on the form submission.
- Rate limiting / a CAPTCHA on
register.phpto deter automated sign-ups. - A proper session/login system to go along with this registration step.
- Server-level file-size limits (
upload_max_filesize,post_max_sizeinphp.ini) that match the 2MB check in the code.