# Frontend Form with CSV Export

**Frontend Form with CSV Export** is a WordPress plugin that provides a customizable form via a shortcode. Submissions are stored in a database and can be downloaded as a CSV file from the WordPress admin dashboard. Additionally, the plugin allows resetting all submissions with a single click.

---

## Features

- Display a customizable form using a shortcode.
- Store form submissions in a custom database table.
- Export submissions as a CSV file from the admin dashboard.
- Reset the database table to delete all submissions.
- Fully stylable using CSS.

---

## Installation

1. Download the plugin files and save them in a folder named `frontend-form-with-csv-export`.
2. Upload the folder to the `wp-content/plugins/` directory of your WordPress installation.
3. Activate the plugin from the WordPress **Plugins** menu.
4. The plugin will create a database table for storing submissions.

---

## Usage

### Display the Form

To display the form, use the shortcode `[pdavea_frontend_form]` in any post, page, or widget.

Example:
```plaintext
[pdavea_frontend_form]
```

### Manage Submissions

1. Go to **Form Submissions** in the WordPress admin menu.
2. From this page, you can:
   - View the total number of submissions.
   - Download all submissions as a CSV file by clicking the **Download CSV** button.
   - Reset the submissions table by clicking the **Reset Submissions** button (this will delete all records).

---

## How to Add More Fields

To add more fields to the form:
1. **Modify the HTML in `render_form()`:**
   - Add new fields to the form, such as:
     ```php
     <label for="phone">Phone:</label>
     <input type="tel" id="phone" name="phone" required><br>
     ```

2. **Handle the New Field in `handle_form_submission()`:**
   - Sanitize and validate the new field in the `handle_form_submission()` method:
     ```php
     $phone = sanitize_text_field(wp_unslash($_POST['phone'] ?? ''));
     if (empty($phone)) {
         wp_send_json(['success' => false, 'message' => 'Phone is required.']);
     }
     ```
   - Insert the new field into the database:
     ```php
     global $wpdb;
     $wpdb->insert($this->table_name, [
         'name'    => sanitize_text_field($_POST['name']),
         'email'   => sanitize_email($_POST['email']),
         'message' => sanitize_textarea_field($_POST['message']),
         'phone'   => $phone
     ]);
     ```

3. **Update the Database Table:**
   - Alter the database table to include the new field. For example:
     ```sql
     ALTER TABLE wp_pdavea_form_submissions ADD phone VARCHAR(20) NOT NULL;
     ```

4. **Export the New Field in `download_csv()`:**
   - Include the new field in the CSV export:
     ```php
     fputcsv($output, ['ID', 'Name', 'Email', 'Message', 'Phone', 'Created At']);
     ```

---

## How to Style the Form

### Using the Included CSS File
The plugin comes with a `style.css` file located in the same folder as the plugin. You can edit this file to customize the form's appearance.

Example styles:
```css
#pdavea-frontend-form {
    max-width: 600px;
    margin: 20px auto;
    padding: 20px;
    background-color: #ffffff;
    border: 1px solid #cccccc;
    border-radius: 8px;
    font-family: 'Arial', sans-serif;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
```

### Adding Your Own CSS
You can also add custom CSS to your WordPress theme or use a page builder to apply additional styles. Use the `#pdavea-frontend-form` ID to target the form.

---

## FAQ

### **How can I reset submissions?**
Navigate to the **Form Submissions** page in the WordPress admin menu and click the **Reset Submissions** button.

### **Can I customize the form fields?**
Yes, you can add, remove, or modify fields in the `render_form()` function. Remember to update the database and submission handling code.

### **Where are the submissions stored?**
Submissions are stored in a custom database table named `wp_pdavea_form_submissions` (or `{prefix}_pdavea_form_submissions` if you use a custom table prefix).

---

## Changelog

### **1.2**
- Added the reset submissions functionality.
- Improved security and error handling.
- Updated styling options.

### **1.1**
- Added the CSV export functionality.
- Enhanced form shortcode.

### **1.0**
- Initial release with a basic form and database integration.

---

## License

This plugin is licensed under the [GPL-2.0](https://www.gnu.org/licenses/gpl-2.0.html). You are free to modify and distribute it under the same license.

---

## Need More Features?

If you have feature requests or specific customizations, feel free to reach out. I'm happy to consider adding new functionality to the plugin upon request.

---

## Full License Statement

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see [https://www.gnu.org/licenses/gpl-2.0.html](https://www.gnu.org/licenses/gpl-2.0.html).

