index.php 2.76 KB
<?php
require_once 'Database.php'; // Pastikan file Database.php berada di direktori yang sama atau sesuaikan path-nya
require_once 'upload_form.php';

class Form {
    private $conn;

    public function __construct() {
        $database = new Database();
        $this->conn = $database->conn;
    }

    public function text($name, $label) {
        return "<label for='$name'>$label:</label>
                <input type='text' id='$name' name='$name' required><br>";
    }

    public function password($name, $label) {
        return "<label for='$name'>$label:</label>
                <input type='password' id='$name' name='$name' required><br>";
    }

    public function radio($name, $label, $options) {
        $html = "<label>$label:</label><br>";
        foreach ($options as $value => $text) {
            $html .= "<input type='radio' id='$name-$value' name='$name' value='$value' required>
                      <label for='$name-$value'>$text</label><br>";
        }
        return $html;
    }

    public function checkbox($name, $label) {
        return "<input type='checkbox' id='$name' name='$name'>
                <label for='$name'>$label</label><br>";
    }

    public function select($name, $label, $options) {
        $html = "<label for='$name'>$label:</label>
                 <select id='$name' name='$name' required>";
        foreach ($options as $value => $text) {
            $html .= "<option value='$value'>$text</option>";
        }
        $html .= "</select><br>";
        return $html;
    }

    public function textarea($name, $label) {
        return "<label for='$name'>$label:</label><br>
                <textarea id='$name' name='$name' required></textarea><br>";
    }

    public function file($name, $label) {
        return "<label for='$name'>$label:</label>
                <input type='file' id='$name' name='$name' required><br>";
    }

    public function submitForm($data) {
        try {
            $sql = "INSERT INTO upload (nama_channel, judul, visibilitas, deskripsi) 
                    VALUES (:nama_channel, :judul, :visibilitas, :deskripsi)";
            
            $stmt = $this->conn->prepare($sql);
            
            $stmt->bindParam(':nama_channel', $data['nama_channel']);
            $stmt->bindParam(':judul', $data['judul']);
            $stmt->bindParam(':visibilitas', $data['visibilitas']);
            $stmt->bindParam(':deskripsi', $data['deskripsi']);
            
            $stmt->execute();
            
            return "Data berhasil disimpan";
        } catch(PDOException $e) {
            return "Error: " . $e->getMessage();
        }
    }
}

// Penggunaan kelas Form
$form = new Form();

// Proses form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $result = $form->submitForm($_POST);
    echo $result;
}
?>