From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: ** X-Spam-Status: No, score=2.2 required=5.0 tests=FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by gnuweeb.org (Postfix) with ESMTPS id 1D3E081722 for ; Sun, 27 Nov 2022 20:32:53 +0000 (UTC) Received: by mail-pf1-f181.google.com with SMTP id o1so4109013pfp.12 for ; Sun, 27 Nov 2022 12:32:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0KGyYqpSJOjsuv7cKJCOOsU5KbxsoxUZFR9sCUBrisQ=; b=PxXEd7MlckBnk2KaLVPXp4s6WWQI3DKOQiNY3hYkYmbmqdCtkVQGPv6CXYjQkNbqlG nTOOzDJZYUM/FpP0+sDyqhjkSgfrW6hMnvmy126KbHLozaUYTrnlDDRuczhLwUQnEeYU wxvPtMDJGHSj3ToT+adoKA+dyqxeqZlakfbY5LZ51NqRZ3vgfGOfuGhtXowItbMFtoAz zjNsWBpQ4H++C9UZB3uj/yZo8q8vYkgZppKn2YWgpqUT9CkBYrBHNvN3J/FgFIRoX8Y4 iLp8WhpjfK2YMwgaBxBvOrmdQh6OZx06JngRc4iLQ9Cf099XYAY1fmeCPG8EHuDYbPNW Y/LA== X-Gm-Message-State: ANoB5pkk7uvwos8b2TgdYakAdGITp0WwmS5ODQByQ+3T2srsE6rKevuO 41OtX/D8TvVQ9AsTy+zFl8c= X-Google-Smtp-Source: AA0mqf5digTFJvbr+4QbtWb50A+i3wS27trJceMpdeTQcAwwI6K+6PiOrDcDaN2/SXIPXTT+83ETfQ== X-Received: by 2002:a63:c48:0:b0:470:1186:de85 with SMTP id 8-20020a630c48000000b004701186de85mr28910769pgm.609.1669581172693; Sun, 27 Nov 2022 12:32:52 -0800 (PST) Received: from localhost.localdomain ([2404:8000:1021:2a9:da99:231d:4dfa:7f54]) by smtp.gmail.com with ESMTPSA id ij13-20020a170902ab4d00b0018685257c0dsm7234904plb.58.2022.11.27.12.32.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Nov 2022 12:32:52 -0800 (PST) From: Ammar Faizi To: Gilang Fachrezy Cc: Ammar Faizi , Taufiq Pohan , Aldy Prastyo , Muhammad Fitrah Pandjalu , Nauvalsa Yanandana , GNU/Weeb Mailing List , VNLX Kernel Department Subject: [PATCH v1 09/13] Create initial API integration Date: Mon, 28 Nov 2022 03:32:12 +0700 Message-Id: <20221127203216.408808-10-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221127203216.408808-1-ammarfaizi2@gnuweeb.org> References: <20221127203216.408808-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This connects the HTML form with the MySQL database via AJAX. Co-authored-by: Muhammad Fitrah Pandjalu Signed-off-by: Muhammad Fitrah Pandjalu Co-authored-by: Taufiq Pohan Signed-off-by: Taufiq Pohan Signed-off-by: Ammar Faizi --- public/api.php | 100 ++++++++++++++++++++++++++++++++++++++++++++++- public/index.php | 51 ++++++++++++++++++------ 2 files changed, 136 insertions(+), 15 deletions(-) diff --git a/public/api.php b/public/api.php index 1a812a7..97d8e4d 100644 --- a/public/api.php +++ b/public/api.php @@ -2,5 +2,101 @@ require __DIR__."/../helpers.php"; -$pdo = pdo(); -var_dump($pdo); +date_default_timezone_set("UTC"); + +function err_msg(int $code, string $msg): array +{ + return [ + "code" => $code, + "error" => $msg + ]; +} + +if (!isset($_GET["action"]) || !is_string($_GET["action"])) { + $code = 400; + $msg = err_msg($code, "Missing \"action\" string parameter"); + goto out; +} + +function submit_attendance(): array +{ + if ($_SERVER["REQUEST_METHOD"] !== "POST") + return [405, err_msg(405, "Method not allowed!")]; + + $j = json_decode(file_get_contents("php://input"), true); + if (!is_array($j)) + return [400, err_msg(400, "Invalid input request")]; + + if (!isset($j["full_name"]) || !is_string($j["full_name"])) + return [400, err_msg(400, "Missing \"full_name\" string argument!")]; + + if (!isset($j["city"]) || !is_integer($j["city"])) + return [400, err_msg(400, "Missing \"city\" integer argument!")]; + + if (!isset($j["phone_number"]) || !is_string($j["phone_number"])) + return [400, err_msg(400, "Missing \"phone_number\" string argument!")]; + + if (!isset($j["email"]) || !is_string($j["email"])) + return [400, err_msg(400, "Missing \"email\" string argument!")]; + + if (!isset($j["facebook_id"])) + return [400, err_msg(400, "Missing \"facebook_id\" argument!")]; + + if (!isset($j["twitter_username"])) + return [400, err_msg(400, "Missing \"twitter_username\" argument!")]; + + if (!isset($j["discord_username"])) + return [400, err_msg(400, "Missing \"discord_username\" argument!")]; + + if (!isset($j["github_username"])) + return [400, err_msg(400, "Missing \"github_username\" argument!")]; + + try { + $pdo = pdo(); + $st = $pdo->prepare(<<execute([ + $j["city"], + $j["full_name"], + $j["phone_number"], + $j["email"], + $j["facebook_id"], + $j["twitter_username"], + $j["discord_username"], + $j["github_username"], + date("Y-m-d H:i:s") + ]); + return [200, err_msg(200, "Success!")]; + } catch (PDOException $e) { + return [500, err_msg(500, "Database error: ".$e->__toString())]; + } +} + +switch ($_GET["action"]) { +case "submit_attendance": + [$code, $msg] = submit_attendance(); + break; + +default: + $code = 400; + $msg = err_msg($code, "Invalid action {$_GET["action"]}"); + break; +} + + +out: +http_response_code($code); +header("Content-Type: application/json"); +echo json_encode($msg, JSON_PRETTY_PRINT); diff --git a/public/index.php b/public/index.php index 8587c95..411f6df 100644 --- a/public/index.php +++ b/public/index.php @@ -9,41 +9,41 @@

VNL Member Attendance Form

-
+
- +
- +
- +
- +

Social Media Accounts

Must be filled at least one!

- - + +
- - + +
- - + +
- - + +
@@ -66,6 +66,31 @@ dataType: "json", success: load_select2_city }); + + let form = $("#attendance_form"); + form.submit(function () { + let data = form.serializeArray(); + let json = {}; + let i; + + for (i in data) { + let key = data[i].name; + let val = data[i].value; + + if (key === "city") + val = parseInt(val); + + json[key] = val; + } + + $.post({ + url: "api.php?action=submit_attendance", + data: JSON.stringify(json), + success: function () { + alert("success!"); + } + }); + }); -- Ammar Faizi