donglanzhan7151 2016-01-10 00:28
浏览 78
已采纳

如何使用PHP + Checkbox禁用/启用我网站的各个部分?

I need help with enableing/disableing parts of my website.

    <p>Disabled Sections:</p>
    <form id="e-d-check" method="post">
        <input type="checkbox" name="SRS" onchange="document.getElementById('e-d-check').submit()">SkiRegionSimulator 2012</input>
        <input type="checkbox" name="Bilder" onchange="document.getElementById('e-d-check').submit()">Bilder</input>
        <input type="checkbox" name="KWS" onchange="document.getElementById('e-d-check').submit()">Klimawandel-Stunde</input>
    </form>

        <table>
            <tr>
                <th>Name</th>
                <th>Größe</th>
                <th>Datum</th>
                <th>Download</th>
            </tr>

            <?php 
                if (isset($_POST['SRS'])) {
                    echo 'Disabled SkiRegionSimulator 2012';
                } else {
                    include'SRS.php';
                }
            ?>

Everytime I uncheck the box it checks itself again...

  • 写回答

2条回答 默认 最新

  • dongling5411 2016-01-10 05:59
    关注

    A different but similar approach with submit onclick.

    <?php 
    $check_SRS = "";
    $check_Bilder = "";
    $check_KWS = "";
    if (isset($_POST['SRS']) && intval($_POST['SRS']) == 1) { $check_SRS = "checked";}
    if (isset($_POST['Bilder']) && intval($_POST['Bilder']) == 1) { $check_Bilder = "checked";}
    if (isset($_POST['KWS']) && intval($_POST['KWS']) == 1) { $check_KWS = "checked";}
    ?>
    <p>Disabled Sections:</p>
       <form id="e-d-check" method="post">
         <input type="checkbox" name="SRS" value="1" onclick="submit()" <?php echo htmlspecialchars($check_SRS); ?>>SkiRegionSimulator 2012</input>
         <input type="checkbox" name="Bilder" value="1" onclick="submit()" <?php echo htmlspecialchars($check_Bilder); ?>>Bilder</input>
         <input type="checkbox" name="KWS" value="1" onclick="submit()" <?php echo htmlspecialchars($check_KWS); ?>>Klimawandel-Stunde</input>
       </form>
    
    <table>
      <tr>
        <th>Name</th>
        <th>Größe</th>
        <th>Datum</th>
        <th>Download</th>
      </tr>
      <?php 
      if (isset($_POST['SRS']) && intval($_POST['SRS']) == 1) {
        echo 'Disabled SkiRegionSimulator 2012';
      } else {
        include'SRS.php';
      }
      ?>
    

    A version of elseif statements to accommodate what are assumed to be the other includes depending on the state of the checkboxes - if one only can be checked:

    <?php
    if(intval($_POST['SRS']) == 1){
       echo 'Disabled SkiRegionSimulator 2012';
    }elseif(intval($_POST['SRS']) != 1){
       include'SRS.php';
    }elseif(intval($_POST['Bilder']) == 1){
       echo 'Disabled Bilder';
    }elseif(intval($_POST['Bilder']) != 1){
       include'Bilder.php';
    }elseif(intval($_POST['KWS']) == 1){
       echo 'Disabled KWS';
    }elseif(intval($_POST['KWS']) != 1){
       include'KWS.php';
    }
    ?>
    

    if more than one can be checked:

     <?php
     if(intval($_POST['SRS']) == 1){
     echo 'Disabled SkiRegionSimulator 2012';
     }else{
     include'SRS.php';
     }
    
     if(intval($_POST['Bilder']) == 1){
     echo 'Disabled Bilder';
     }else{
     include'Bilder.php';
     }
    
     if(intval($_POST['KWS']) == 1){
     echo 'Disabled KWS';
     }else{
     include'KWS.php';
     }
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路