# FormBuilder — PHP Single-Tenant Form Builder

A self-hosted form builder for Namecheap shared hosting (PHP 7.4+ · MySQL · SMTP).

---

## Features

- **Multiple forms** — each with a unique public URL, listed on a landing page
- **Per-form visibility** — public or password-protected
- **11 field types** — text, textarea, email, phone, date, number, select, radio, checkbox, file upload, signature
- **Admin dashboard** — create/edit/delete forms via a drag-and-drop builder; view & manage submissions
- **Responsive** — desktop sidebar + mobile bottom navigation
- **PDF generation** — dynamic PDF per submission (upgrades to FPDF if installed via Composer)
- **Email** — admin notification + submitter confirmation, both with PDF attached
- **File uploads** — stored on server, attached to email if under size limit, linked otherwise
- **Setup wizard** — browser-based, no Terminal needed
- **Rate limiting** — file-based, no Redis required

---

## File Structure

```
formbuilder/
├── config.php           ← YOUR CONFIG — fill in before uploading
├── lib.php              ← Core: DB, auth, SMTP, PDF, helpers
├── index.php            ← Public landing page (lists forms)
├── form.php             ← Public form renderer (?s=slug)
├── admin.php            ← Admin dashboard (SPA)
├── setup.php            ← Setup wizard (delete after use)
├── composer.json        ← PHPMailer + FPDF (optional)
├── .htaccess            ← Routing + security
├── api/
│   └── index.php        ← REST API router (all routes)
└── data/
    ├── pdfs/            ← Generated PDFs
    ├── uploads/         ← Uploaded files (YYYY/MM/)
    └── submissions/     ← (reserved)
```

---

## Quick Start

### 1 — Upload files
Upload the entire `formbuilder/` folder to `public_html/formbuilder/` (or your preferred path).

### 2 — Run the setup wizard
Visit `https://yourdomain.com/formbuilder/setup.php` and complete all 5 steps:
1. **Environment check** — verifies PHP, PDO, OpenSSL
2. **Configuration** — DB credentials, admin password, SMTP settings → writes `config.php`
3. **Database** — connects and creates `fb_forms` + `fb_submissions` tables
4. **PHPMailer** — optional install via Composer for reliable email delivery
5. **Test email** — sends a test to verify SMTP works
6. **Finish** — deletes `setup.php` automatically

### 3 — Log in and create your first form
Visit `https://yourdomain.com/formbuilder/admin.php`

---

## API Reference (admin-only unless noted)

| Method | Path | Auth | Description |
|--------|------|------|-------------|
| GET | `/api/public/forms` | None | List public active forms |
| GET | `/api/public/forms/:slug` | None | Form schema by slug |
| POST | `/api/submit/:slug` | None | Submit a form |
| POST | `/api/auth/login` | — | Admin login |
| POST | `/api/auth/logout` | Session | Admin logout |
| GET | `/api/stats` | Session | Dashboard stats |
| GET | `/api/forms` | Session | List all forms |
| POST | `/api/forms` | Session | Create form |
| GET | `/api/forms/:id` | Session | Get form + fields |
| PUT | `/api/forms/:id` | Session | Update form |
| DELETE | `/api/forms/:id` | Session | Delete form |
| GET | `/api/forms/:id/submissions` | Session | List submissions |
| GET | `/api/submissions/:id` | Session | View submission |
| GET | `/api/submissions/:id/pdf` | Session | Download PDF |
| DELETE | `/api/submissions/:id` | Session | Delete submission |

---

## Future Upgrades (Phase 2)
- Per-admin user accounts with roles
- Per-form branding (logo, colors, custom header)
- Custom PDF templates
- Conditional logic / multi-step forms
- Webhooks / Zapier integration
