如何使用PHP+复选框禁用/启用我的网站部分


How do I disable / enable sections of my website with PHP + Checkboxes?

我需要帮助启用/禁用我网站的某些部分。

    <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';
                }
            ?>

每次我取消选中复选框时,它都会再次检查自己。。。

检查这是否适用于您:

<?php
    $disabled = array();
    $disabled['SRS'] = isset($_POST['SRS']) && (int)$_POST['SRS'] === 1;
    $disabled['Bilder'] = isset($_POST['Bilder']) && (int)$_POST['Bilder'] === 1;
    $disabled['KWS'] = isset($_POST['KWS']) && (int)$_POST['KWS'] === 1;
?>
<p>Disabled Sections:</p>
<form id="e-d-check" method="post">
    <input type="checkbox" value="1" <?php if($disabled['SRS'] === true) echo "checked"; ?> name="SRS" onchange="document.getElementById('e-d-check').submit()">SkiRegionSimulator 2012</input>
    <input type="checkbox" value="1" <?php if($disabled['Bilder'] === true) echo "checked"; ?> name="Bilder" onchange="document.getElementById('e-d-check').submit()">Bilder</input>
    <input type="checkbox" value="1" <?php if($disabled['KWS'] === true) echo "checked"; ?> 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 ($disabled['SRS'] === true) {
                echo 'Disabled SkiRegionSimulator 2012';
            } else {
                include'SRS.php';
            }
        ?>

我们使用(猜测!)checked属性来控制选中哪个复选框。如果使用XHTML,则将checked更改为checked="checked"

一种不同但相似的提交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';
  }
  ?>

elseif语句的一个版本,以适应被认为是另一个的内容,包括取决于复选框的状态的内容-如果只能选中一个:

<?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';
}
?>

如果可以检查多个:

 <?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';
 }
 ?>