简体   繁体   中英

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

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...

Check if this works for you:

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

We control what checkbox is checked by using the (guess!) checked attribute. If you are using XHTML then change checked to checked="checked"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM