dimanche 3 mars 2019

POST either of two states of checkbox and save to MySQL database with smarty template engine for PHP?

I want to make a switch and store the ON (1) or OFF (0) state in database until clicked again. What is wrong here? I want to click on a checkbox and store either of the results. Any convenient way to make the POST request without refreshing the page?

                <form action="/path/to/script> 
                            <span style="float:right;">
                                <label class="switch">
                                    <input type="checkbox" name="postaback_status" {if isset($_POST['light_status'])} value="1" {else} value="0" {/if}{if isset($_POST['light_status']) && $_POST['light_status'] == 1} checked {/if}/>
                                    <span class="slider"></span>
                                </label>
                            </span>
                </form>

And in my PHP

    if (isset($_POST['light_status'])) {
        $e_light_status = html_output($_POST['light_status']);
        $st = $db->prepare('update table_lights set status = ? where acct_id = ?');
        $st->execute([$e_light_status, $linkid]);
    }




Aucun commentaire:

Enregistrer un commentaire