【问题标题】:Write to MySQL DB from HTML page从 HTML 页面写入 MySQL DB
【发布时间】:2020-10-20 12:43:29
【问题描述】:

我的目标是在 MySQL DB 上使用 Bootstrap 切换按钮写入“是”或“否”。 我知道 JavaScript 在客户端,所以我找到了一种方法(但不确定是最好的)从 JavaScript 收集变量并将其传递给将写入数据库的 PHP 文件。 我的相关代码如下,但如果我尝试,什么也没有发生。 我想在我的 JavaScript 代码中我必须调用“函数 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')
    })
}); 

【问题讨论】:

    标签: javascript php html mysql bootstrap-4


    【解决方案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
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-24
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      相关资源
      最近更新 更多