mardi 26 mai 2015

Check/uncheck checkbox depend on data come from database

I am trying to check or uncheck a checkbox depending on the data from mysql database. I use nusoap webservice/webclient to read data and data value can be 1 or 0.

My code is:

<input name="check1" type="checkbox" id="check1" class="sag">
<script>
                            function control(){

                                $.ajax({
                                    type: "POST",
                                    url: "check.php",
                                    data: {checkdata: 1},
                                    success: function(asd){
                                        if(asd == '1'){
                                    document.getElementById('check1').setAttribute("checked", true);


                                            alert('data is 1');

                                        }
                                        else{
                                            document.getElementById('check1').removeAttribute("checked");

                                            alert('data is 0');
                                        }
                                    }
                                });

                            }
                        </script>

<body onload="control()">

With this code i can get data from database correctly and alert() works fine. But these codes don't add checked attribute to checkbox. How can i change check status of checkbox according to the data come from database on load of the page?




Aucun commentaire:

Enregistrer un commentaire