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'
type='checkbox'
value='1'
name='my_checkbox'
<?php echo $ligado== '1' ? ' checked' : ''; ?> >
<label for='cmn-toggle-1'></label>
On submission you can check for the variable:
<?php
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.
HTML
<section>
<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>
</section>
Jquery
//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.
$(this).toggleClass("active-button").toggleClass("service-button");
//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);
}
});
PHP
<?php
$selected = $_POST['selected_button'];
if(isset($_POST['selected_button'])) {
$service .= $selected;
}
?>
Hope it will help
or
Note:
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:
<?php
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
- LocalStorage
- SessionStorage
Server Side
- Session
Application wide
- DB
- 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
How to Resolve a Http 414 "Request Uri Too Long" Error
Update Data on a Page Without Refreshing
How to Run a PHP Script in the Background After a Form Is Submitted
How to Pass Variable Between Two Web Pages With PHP Without Using Session
How to Remove a Key from an Object Not Array in Laravel
Update Database After Select Option Change
Unable to Upload File Greater Than 2Mb in Laravel
Join Category and Subcategory in Same Table
Internal Server Error While Run Laravel on Localhost
How to Increase Maximum Execution Time in Laravel
Get All Hrefs from String But Then Replace Via Another Method
How to Use PHP'S Password_Hash to Hash and Verify Passwords
Send Attachments With PHP Mail()
How to Get the Domain Name Without Www, Subdomain, and Com/Net/Org/Etc