How to Get Toggle Button Working With PHP

Manage data using switch/toggle button on html with php

You are trying to use a php variable in Javascript.

Try this:

<input  id='cmn-toggle-1' 
class='cmn-toggle cmn-toggle-round'
<?php echo $ligado== '1' ? ' checked' : ''; ?> >
<label for='cmn-toggle-1'></label>

On submission you can check for the variable:


if(!empty($_POST['my_checkbox'])) {

// user checked the box

} else {

// user did not check the box


How can i make toggle-able html buttons POST to php

Add hidden input and whenever you clicking on button change hidden value and submit hidden field.


<input class="selected_button" name="selected_button" value="" type="hidden">
<button class="btn service-button" name="app" value="Web App" type="button">Web App</button>
<button class="btn service-button" name="brand" value="Branding" type="button">Branding</button>
<button class="btn service-button" name="design" value="Web Design" type="button">Web Design</button>
<button class="btn service-button" name="other" value="Other" type="button">Other</button>


//On button click
$(".btn").click(function() {
//The string to add to the selected-button value
var toAdd = $(this).attr("value");
//Add/remove active-button and service-button. Replaces one with the
//other depending on the current state.
//Checks if the button has already been added in case the user deselects the button
if($(".selected_button").val().indexOf(toAdd) >= 0) {
//Removes the comma and text
$(".selected_button").val($(".selected_button").val().replace((', ' + toAdd), ''));
//If the button is being added for the first time
} else {
//Adds the comma and text
$(".selected_button").val($(".selected_button").val() + ", " + toAdd);


$selected = $_POST['selected_button'];
if(isset($_POST['selected_button'])) {
$service .= $selected;

Hope it will help


you use radio buttons by using css style you can display like button

Toggle Switch - Set Values

Since you only need two values for your checkbox, you can handle it like so:

if(isset($_POST['checkbox'])) {
// Set "activation_status" to 1.
} else {
// Set "activation_status" to 0.

If a checkbox is unchecked, the corresponding value in PHP will simply not be set. So if you try to retrieve the value of $_POST['checkbox'] when the checkbox is unchecked, it will throw an error.

In case you need a slider with a range of values, you can also check out the HTML5-only <input type="range"/>.

PHP toggle button that keeps the value

Bro, to save state you have the following methods

Client Side

  1. LocalStorage
  2. SessionStorage

Server Side

  1. Session

Application wide

  1. DB
  2. File

Client Side & Server Side only works with in a computer. If you want to maintain in different computers then you have to go with DB or File.

Related Topics

Leave a reply