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

时间:2016-01-10 00:28:28

标签: php checkbox

我需要帮助启用/禁用我网站的部分内容。

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

每当我取消选中该框时,它会再次检查自己......

2 个答案:

答案 0 :(得分:1)

检查这是否适合您:

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

我们使用(guess!)checked属性控制选中的复选框。如果您使用的是XHTML,请将checked更改为checked="checked"

答案 1 :(得分:0)

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