2016-09-06 153 views
0

我有一个与好看CSS样式下面的代码放在开关按钮提交PHP代码,当用户选中一个复选框

<?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(或选中)时,我想上传wifion.php,并设置为关闭或未选中时上传wifioff.php。我怎样才能做这项工作?我在这里找到这个按钮https://proto.io/freebies/onoff/

+0

你必须使用的JavaScript。 – Barmar

回答

0

如果你是熟悉的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 
     }); 
    } 
}); 
+0

这个问题根据复选框的状态提交给'wifion.php'或'wifioff.php'。那你的代码在哪里?[ – Barmar

+0

这是另一个文件wifion.php。在这种情况下,提交按钮后将example.button设置为on,php脚本将ftp上传文件到远程位置。当按钮设置为关闭时,我必须再次上传,但不同的文件 –

相关问题