从 HTML 页面写入 MySQL DB

我的目标是通过 Bootstrap 切换按钮在 MySQL DB 上写入“是”或“否”。我知道 JavaScript 在客户端,所以我找到了一种方法(但不确定是最好的)从 JavaScript 收集变量并将其传递给将写入数据库的 PHP 文件。我的相关代码如下,但如果我尝试,什么也不会发生。我想在我的 JavaScript 代码中我必须调用“function saveStato”,但我无法理解以哪种方式。


$(function() {

    $('.inServizio').change(function() {

        if($(this).prop('checked')){

        console.log('TEST_ON')

        function saveStato() {

            $.post("inServizio.php",

            {

            stato: $("SI").val(),

            },

        function(data,status){

            document.getElementById("saveWarningText").innerHTML = data;

            $( "#saveWarningText" ).fadeIn(100);

            setTimeout(function(){ $( "#saveWarningText" ).fadeOut(100); 

            }, 3000);

            });

        

        }

        } else {

            

               console.log('TEST_OFF')

         }


        //$(this).prop('checked')

    })

}); 


呼如林
浏览 93回答 1
1回答

心有法竹

您可以在更改侦听器外部定义它,并在内部调用它saveStato();或者干脆完全去掉这个功能,然后就可以了$('.inServizio').change(function() {    if($(this).prop('checked')){        console.log('TEST_ON')            $.post("inServizio.php",{stato: $("SI").val()},function(data,status){            document.getElementById("saveWarningText").innerHTML = data;            $( "#saveWarningText" ).fadeIn(100);            setTimeout(function(){ $( "#saveWarningText" ).fadeOut(100); }, 3000);        });        } else {         //rest of your code    }
打开App,查看更多内容
随时随地看视频慕课网APP