Home > OS >  Switching toggle-wrap based on database value
Switching toggle-wrap based on database value

Time:11-21

Currently I have a toggle-wrap switch in my update page. As of now in my database, the value for this can be Null, No and Yes. So when the user enters the page, I want this switch to be off if the database value is in Null or no and switched on if the database value is Yes in the database. I've tried the following to enable the toggle but it does not toggle on when the data value in my database is Yes.

View Class:

<label class="control-labels mr-4">Lead Gen?</label>
                    <div class="toggle-wrap tg-list-item">
                        <input class="tgl tgl-light" id="leadgen" name="leadgen" type="checkbox" 
                        <?php echo ($listing[0]['leadgen'] == 'Yes' ? 'enabled':'disabled'); ?> />
                        <label class="tgl-btn" for="leadgen"></label>
                    </div>

Doing a var_dump on $listing[0]['leadgen'] gave me string(3) "Yes" But the switch still doesn't turn on

CodePudding user response:

Instead of using 'enable' and 'disable' have a look at checked. Your code would become:

<label class="control-labels mr-4">Lead Gen?</label>
                    <div class="toggle-wrap tg-list-item">
                        <input class="tgl tgl-light" id="leadgen" name="leadgen" type="checkbox" 
                        <?php echo ($listing[0]['leadgen'] == 'Yes' ? 'checked' : ''); ?> />
                        <label class="tgl-btn" for="leadgen"></label>
                    </div>
  • Related