black-room-boy
black-room-boy

Reputation: 659

How to use Bootstrap Toggle in PHP?

I have a checkbox that I want to look like http://www.bootstraptoggle.com/

And if I have html input field like this: <input checked data-toggle="toggle" type="checkbox">, it really looks like that.

But how I am supposed to use this to post value 1/0 to my PHP script like it was a real checkbox ?

Do I need to add something else in html code, and how to process that in PHP script ?

Thanks in advance.

Upvotes: 1

Views: 5961

Answers (2)

CodeGodie
CodeGodie

Reputation: 12142

All you need to do is add a name and value attributes to your input. The plugin takes care of the rest already for you.

Like this:

<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.0/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.0/js/bootstrap-toggle.min.js"></script>

<form method="post" action="submit.php">
    <input type="checkbox" name="my_input" checked data-toggle="toggle">
    <input type="submit">
</form>

Result in submit.php:

var_dump($_POST);

/*
array (size=1)
  'my_input' => string 'on' (length=2)
*/

Upvotes: 2

jpganz18
jpganz18

Reputation: 5878

You might want to set a value to your input, something like

value='1' or value='true'

<input checked data-toggle="toggle" type="checkbox" value='1' or value='true' name='mycheckbox' >

Since it is a checkbox, if is checked it will go with your additional form data when you submit the form...

Then at your php side you just do something like this

isset($_POST['mycheckbox'])

and then it means it was checked... you can later capture the value (if you need it for something) or whatever you need to do..

Upvotes: 3

Related Questions