Hi all, am trying to use bootstrap switch in my form. Am at a lost as to how to get the value of

name="onoffswitch"

from that field and insert it into mysql db, here is my code...

div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
    <label class="onoffswitch-label" for="myonoffswitch">
        <div class="onoffswitch-inner"></div>
        <div class="onoffswitch-switch"></div>
    </label>
</div>

Dani AI

Generated

Nice question, . @diafol is spot on about storing the switch as a tinyint(1) and not as the strings ON/OFF. Two small tweaks make life easier and your inserts safer:

  1. In the form, give the checkbox an explicit value and add a hidden input with the same name. That way the field always posts (0 when unchecked, 1 when checked), and you are not relying on PHP’s isset trick.
<input type="hidden" name="onoffswitch" value="0">
<input type="checkbox" id="myonoffswitch" name="onoffswitch" value="1" checked>

Notes:

  • Keep the checkbox inside the <form>.
  • Do not disable the checkbox; disabled inputs are not submitted.
  • Your Bootstrap/skin can hide the checkbox, but this underlying input is what actually gets posted.
  1. Use a prepared statement (PDO or mysqli) instead of the old mysql_* API. Here is a minimal PDO pattern that maps cleanly to your table (adjust column names to match, e.g., s_veration):
// Example assumes PHP 7+.
$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4', 'user', 'pass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$tag = $_POST['tag'] ?? '';                  // validate/sanitize as needed
$switch = (int)($_POST['onoffswitch'] ?? 0); // 0 or 1 from the form

$stmt = $pdo->prepare('INSERT INTO sre_log_remind (tag, s_veration) VALUES (?, ?)');
$stmt->execute([$tag, $switch]);

This avoids SQL injection, stores a true boolean-ish value, and does not depend on whether the checkbox was posted at all. If you still need ON/OFF for display, derive it with $switch ? 'ON' : 'OFF' at render time rather than storing it.

Recommended Answers

All 6 Replies

Member Avatar for Member #120589

A tester...

if(isset($_POST['onoffswitch']))
{
    echo "on";
}else{
    echo "off";
}

Thanks for your prompt reply...
but how do i get the values into this query :

$query ="INSERT INTO sre_log_remind VALUES('','$tag','$s_veration')";

mysql_query($query);

so that i can insert the values 'ON' or 'OFF' in to the db?

Member Avatar for Member #120589

You should save the values as a tinyint(1) - 0 or 1.

if(isset($_POST['onoffswitch']))
{
    $switch = 1;
}else{
    $switch = 0;
}

Or a one-liner...

$switch = (isset($_POST['onoffswitch'])) ? 1 : 0;

Then...

$query ="INSERT INTO sre_log_remind VALUES(NULL,'$tag',$switch)";    
commented: perfect solution +1

thanks Diafol, i will am giving it a try. Your contributions are always rich and deep. How long have you been programming...?

Member Avatar for Member #120589

Hmm, difficult to say - I've been 'playing' at programming for about 20 years. Only started to make it a serious hobby about 5 years ago. I'm not a professional programmer - that aspiration - unfortunately - is beyond my abilities - but I do enjoy playing.

LOL, thanks for your help man, it worked perfectly.Am working hard, so i can be of help to others, Greate Work Man...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.