2016-09-06 74 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上傳文件到遠程位置。當按鈕設置爲關閉時,我必須再次上傳,但不同的文件 –

相關問題