当用户选中复选框时提交PHP代码


Submit PHP code when user check a checkbox

我有一个漂亮的css样式的开关按钮的代码

<?php
$wifi2g=file_get_contents("wifi2g.txt");
$wifi2g= trim ($wifi2g);
?>
<link rel="stylesheet" type="text/css" href="style.css">
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" <?php if ($wifi2g == "enabled") {echo "checked";} ?>>
    <label class="onoffswitch-label" for="myonoffswitch">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>

我可以从wifi2g.txt中读取,并定义按钮位置打开或关闭(选中或未选中)。我有两个脚本,我想上传到ftp服务器wifion.php和wifioff.php。这些脚本取决于按钮的位置。当用户将按钮位置设置为ON(或选中)时,我要上传wifioff.php,当设置为关闭或未选中以上传wifioff.php时。我该怎么做呢?我找到了这个按钮https://proto.io/freebies/onoff/

如果您熟悉Jquery,您可以这样做

$('.onoffswitch-checkbox').on('change',function(){
    var input = $(this);
    if(input.prop('checked') == true){
       input.closest('form').submit();
    }
});

或者如果您想继续在同一页面中,使用ajax

$('.onoffswitch-checkbox').on('change',function(){
    var input = $(this);
    if(input.prop('checked') == true){
         $.ajax({
              url:'test.php'
              ,type:'POST'
              ,data: {btn:'was checked!'}
         }).done(function(data){
             // do something with returning of php
         });
    }
});