• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Toggle Button Status Update (PHP, MySQL, AJAX)

By Abhishek Ghosh June 20, 2023 6:03 pm Updated on June 20, 2023

Toggle Button Status Update (PHP, MySQL, AJAX)

Advertisement

Our This guide ESP32 Arduino IoT Relay Control is self-hosted. The working result is here. This kind of project serves the purpose but the UI is just pathetic and you can not easily convert it to an Android or iOS app. Most of us want a working system to get a toggle button control like this one.

These days, you have a lot of options for UI such as React.js or just Twitter Bootstrap. You have the options of backend such as Firebase, Supabase and so on.

It does not matter whether you use MySQL or Supabase, you need to create a basic API system written in commonly used dynamic programing languages written as Ruby, PHP etc. If you study the server files of that project in our GitHub repository, you’ll notice esp32_update.php and index.php. The basic problem of this system is the change of state upon reloading the index.php webpage.

Advertisement

---

Toggle Button Status Update PHP MySQL AJAX

Now, look at this repo on my GitHub. index.php of this project does the similar job that of esp32_update.php of a previous project but it clearly defines how to handle GET and POST requests:

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['toggle_update'])) {
        $update = $db->prepare("UPDATE `toggle` SET `status` = ? WHERE `id` = 1 LIMIT 1;");
        $update->execute([$_POST['status']]);
        echo json_encode($_POST);
    }
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
    if (isset($_GET['toggle_select'])) {
        $select = $db->prepare("SELECT `status` FROM `toggle` WHERE `id` = 1 LIMIT 1;");
        $select->execute();
        echo json_encode($select->fetchColumn());
    } elseif (isset($_GET['toggle_updated'])) {
        $select = $db->prepare("SELECT date_format(updated, '%e %b %l:%i:%s %p') as updated FROM `toggle` WHERE `id` = 1 LIMIT 1;");
        $select->execute();
        echo json_encode($select->fetchColumn());
    }
} else {
    echo json_encode(array());
}

These files are originally written by corbpie (hence I have no control on the included URLs). You can look at this index.html file, this is the AJAX script:

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function putStatus() {
    $.ajax({
        type: "GET",
        url: "https://peachpuff.srv3r.com/api/index.php",
        data: {toggle_select: true},
        success: function (result) {
            if (result == 1) {
                $('#customSwitch1').prop('checked', true);
                statusText(1);
            } else {
                $('#customSwitch1').prop('checked', false);
                statusText(0);
            }
            lastUpdated();
        }
    });
}
 
function statusText(status_val) {
    if (status_val == 1) {
        var status_str = "On (1)";
    } else {
        var status_str = "Off (0)";
    }
    document.getElementById("statusText").innerText = status_str;
}
 
function onToggle() {
    $('#toggleForm :checkbox').change(function () {
        if (this.checked) {
            //alert('checked');
            updateStatus(1);
            statusText(1);
        } else {
            //alert('NOT checked');
            updateStatus(0);
            statusText(0);
        }
    });
}
 
function updateStatus(status_val) {
    $.ajax({
        type: "POST",
        url: "https://peachpuff.srv3r.com/api/index.php",
        data: {toggle_update: true, status: status_val},
        success: function (result) {
            console.log(result);
            lastUpdated();
        }
    });
}
 
function lastUpdated() {
    $.ajax({
        type: "GET",
        url: "https://peachpuff.srv3r.com/api/index.php",
        data: {toggle_updated: true},
        success: function (result) {
            document.getElementById("updatedAt").innerText = "Last updated at: " + result;
        }
    });
}
 
$(document).ready(function () {
    //Called on page load:
    putStatus();
    onToggle();
    statusText();
});

corbpie’s example code is not for direct conversion of the project we have initially mentioned. But that is a kind of one use-case. There are differences and limitations of these two methods for the frontend.

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Toggle Button Status Update (PHP, MySQL, AJAX)

  • Adding Help & Option in Bash Script

    Many New Hobbyist Programmers and Students Write Bash Scripts. Adding Help & Option in Bash Script is Easy. Here is Basic Guide With Example.

  • How to add toggle hidden files to context menu in Windows 7

    After reading this tutorial, you can get an option to show or hide hidden files in right click context menu in Windows 7.

  • WordPress Auto Install Script (Nginx Microcache and Plugins)

    This WordPress Auto Install Script is for Creation of Nginx Microcache Backend with PHP5-FPM, XCache, MariaDB Running WordPress with Some Plugins.

  • Deploy OctoPress on OpenStack Swift (HP Cloud)

    Here is How To Deploy OctoPress on OpenStack Swift, Example Given With HP Helion Cloud With Only Python Swift Client & a Custom Shebang Script.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy