为了账号安全,请及时绑定邮箱和手机立即绑定

从 HTML 页面写入 MySQL DB

从 HTML 页面写入 MySQL DB

PHP
呼如林 2023-07-08 16:46:43
我的目标是通过 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')    })}); 
查看完整描述

1 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

您可以在更改侦听器外部定义它,并在内部调用它


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

    }


查看完整回答
反对 回复 2023-07-08
  • 1 回答
  • 0 关注
  • 92 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信