Jump to content

Trouble with Disabling Select Box


Landslyde
Go to solution Solved by mac_gyver,

Recommended Posts

 <script type="text/javascript" language="javascript">
    function disableFunc() {
       document.getElementById("groupselect").disabled = true;
}
</script>
<form method="post" class="subform" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
<div class="row" style="background: skyblue; margin-top:-23px">
<div class="col-sm-2">
<div class="input-group">
<select id="groupselect" class="form-control" name="groupid" onchange="disableFunc()">
<option name="0"<?php echo ($_POST['groupid'] == '0') ? ' selected="selected"' : ''; ?> value="0" >GROUP</option>
<option name="1"<?php echo ($_POST['groupid'] == '1') ? ' selected="selected"' : ''; ?> value="1" >1</option>
         <option name="2"<?php echo ($_POST['groupid'] == '2') ? ' selected="selected"' : ''; ?> value="2" >2</option>
         <option name="3"<?php echo ($_POST['groupid'] == '3') ? ' selected="selected"' : ''; ?> value="3" >3</option>
         <option name="4"<?php echo ($_POST['groupid'] == '4') ? ' selected="selected"' : ''; ?> value="4" >4</option>
         <option name="5"<?php echo ($_POST['groupid'] == '5') ? ' selected="selected"' : ''; ?> value="5" >5</option>
         <option name="6"<?php echo ($_POST['groupid'] == '6') ? ' selected="selected"' : ''; ?> value="6" >6</option>

The onchange function works as it shld. My problem is the HTML. I got this select box code from another forum. Without the onchange event in the code, when the user selects a Groupid option, it automatically loads another select box with names. But with it in the code (as I've shown it here), the select box gets disabled immediately causing no names to be loaded in the other select box. I need to be able to load the other select box with names before the groupid select box disables. Any help on this is appreciated. Thanks.

Link to comment
Share on other sites

disabled form controls are not submitted with the form data. so, i'm guessing that is what is causing the rest of the code, that you didn't show us, from working.

 

how is this second select/option menu being requested/produced? why are you choosing to disable the first one? would making it read-only accomplish your goal, as read-only form controls are submitted with form data.

Link to comment
Share on other sites

mac_gyver:

 

Read only wldnt solve my issue.

 

This is for therapists to track payment and attendance history (among other details) for their attendees. I have to have that select box for the groupid disabled after they make a selection.

 

Why? I'll try to explain, and I'll post more code for you.

 

When the Group is selected from the first select box, via the groupid in table a,  names are loaded into a second select box.

The id and memberid associated with those names are then written to table b to be used solely for attendance purposes. When the therapist posts a payment for the attendee, it's inserted into table c, then that attendeeid is deleted from the table b. What's left then gets written to table c as not being in attendance through clicking a 'Flush' button. 

 

I have a 'Dump' button set next to the Groupid select box. When clicked, it removes all the id's and memderid's from the table b. But I have no safeguard to ensure they click that button before trying to load a new Group. If they loaded another Group before dumping what's already in table b, then the table gets needlessly cluttered until the Dump button is clicked. So the only way I saw to make sure they clicked the dump button was to disable the Group select box, then re-enabling it once the dump button was clicked. And that wld be perfect, except that the disabling, as it is now, causes the select box for the names to not be loaded.

 

Here's more of the code:

 <form method="post" class="subform" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
<div class="row" style="background: skyblue; margin-top:-23px">
<div class="col-sm-2">
<div class="input-group">
<select id="groupselect" class="form-control" name="groupid" onchange="disableFunc()">
<option name="0"<?php echo ($_POST['groupid'] == '0') ? ' selected="selected"' : ''; ?> value="0" >GROUP</option>
<option name="1"<?php echo ($_POST['groupid'] == '1') ? ' selected="selected"' : ''; ?> value="1" >1</option>
         <option name="2"<?php echo ($_POST['groupid'] == '2') ? ' selected="selected"' : ''; ?> value="2" >2</option>
         <option name="3"<?php echo ($_POST['groupid'] == '3') ? ' selected="selected"' : ''; ?> value="3" >3</option>
         <option name="4"<?php echo ($_POST['groupid'] == '4') ? ' selected="selected"' : ''; ?> value="4" >4</option>
         <option name="5"<?php echo ($_POST['groupid'] == '5') ? ' selected="selected"' : ''; ?> value="5" >5</option>
         <option name="6"<?php echo ($_POST['groupid'] == '6') ? ' selected="selected"' : ''; ?> value="6" >6</option>
         <option name="7"<?php echo ($_POST['groupid'] == '7') ? ' selected="selected"' : ''; ?> value="7" >7</option>
         <option name="8"<?php echo ($_POST['groupid'] == '8') ? ' selected="selected"' : ''; ?> value="8" >8</option>
         <option name="9"<?php echo ($_POST['groupid'] == '9') ? ' selected="selected"' : ''; ?> value="9" >9</option>    
         <option name="10"<?php echo ($_POST['groupid'] == '10') ? ' selected="selected"' : ''; ?> value="10" >10</option>
</select>
      <span class="input-group-btn">
      <!--<input type="submit" name="load" class="btn btn-default" value="Load">-->        
      <input type="submit" name="dump" class="btn btn-danger" value="Dump">
      </span>
</div>
</div>
<div class="col-sm-3">
<div class="input-group">
<select class="form-control" name="view" style="text-transform: uppercase"> 
<option value="">Select</option>
<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST") { // First brace 
 
$payment = $paymentErr = ""; 
$_SESSION['groupid'] = $_POST['groupid'];
$memberid = $_SESSION["memberid"]; /* We use $memberid to load the Select options box (below) to make
sure that that ONLY attendee information for THAT member is loaded */
$_SESSION['id'] = $_POST['view'];
 
/* Set session vars for uname and pwd1 from invalidlogin.php to be able to login using login.php */
$uname = $_SESSION['uname']; // Set $name for login.php
$pwd1 = $_SESSION['pwd1'];  // Set $pwd1 for login.php
require_once 'login.php'; // This file contains database access credentials   
 
// Load the Select Box with names that match the groupid and memberid
$stmt = $db->prepare('SELECT a.attendeeid, fname, lname, a.groupid, a.memberid, s.attendeeid, suspend
                    FROM attendees AS a
                    JOIN suspended AS s ON a.attendeeid = s.attendeeid 
                    WHERE a.memberid = :memberid 
                    AND suspend = "N"
                    AND a.groupid = :groupid
                    ORDER BY lname');
$stmt->bindValue(':memberid', $memberid, PDO::PARAM_INT);
$stmt->bindValue(':groupid', $_SESSION['groupid'], PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row){ 
 
   // Load the flush Table with the IDs from the selected group
   if($_SESSION['flush'] == 0) {
      $stmt = $db->prepare('INSERT INTO flush (attendeeid, memberid)
                           VALUES(:attendeeid, :memberid)');
      $stmt->bindValue(':attendeeid', $row[0], PDO::PARAM_INT);
      $stmt->bindValue(':memberid', $row[4], PDO::PARAM_INT);
      $stmt->execute();
   }
   echo "<option value='$row[0]'"; // Holds selected name from click to click without resetting to Select
      if(isset($_SESSION['id']) and $_SESSION['id'] == $row[0]) {
        echo ' selected="selected"';
     }
     echo ">$row[2], $row[1]</option>"; 
   }?>
</select>
<?php $_SESSION['flush'] = 1; ?> 
<span class="input-group-btn">
                  <input type="submit" name="fetch" class="btn btn-default" value="Fetch">
               </span>
</div>
</div>
<div class="col-sm-offset-6">
<div class="input-group">
<span class="input-group-btn">
                  <input type="submit" name="flush" class="btn btn-danger" value="Flush">
               </span>
</div>
</div>
</div>
<?php if($_POST['dump']) { ?>
   <div class="row" style="margin-top:20px">
      <div class="col-sm-3">
         <label style="text-transform: uppercase; color: red" class="control-label">Dumped Group <?php echo $_SESSION['groupid']; ?></label> 
      </div>
   </div>
<?php } else{ ?>
   <div class="row" style="margin-top:20px">
      <div class="col-sm-3">
        <label style="text-transform: uppercase; color: red" class="control-label">Group <?php echo $_SESSION['groupid']; ?> Loaded</label> 
      </div>
   </div>
<?php } ?>
<div class="row" style="margin-top:20px">
   <div class="col-sm-3">
      <label style="text-transform: uppercase" class="control-label">Payment:</label> 
   </div>
</div>
<div class="row">
<div class="col-sm-3">
<div class="input-group">
<span class="input-group-addon">$</span>
<input class="form-control" type="text" name="payment" placeholder="0.00" 
value="<?php if (isset($_POST['payment'])) { echo $payment; } ?>">
<span class="input-group-btn">
                 <input type="submit" name="postpayment" class="btn btn-primary" value="Post">
              </span>
<span class="error"><?php echo $paymentErr;?></span>
</div>
</div>
</div>
<?php  
if($_POST['dump']) { 
      $stmt = $db->prepare('DELETE FROM flush WHERE memberid = :memberid');
      $stmt->bindValue(':memberid', $_SESSION['memberid'], PDO::PARAM_INT);
      $stmt->execute(); 
      $_SESSION['flush'] = 0;
      echo '<script type="text/javascript">alert("Please select a new Group"); </script>';
}

My code isn't elegant, so I hope you can make sense of it. Disabling that Group select box is the ideal solution for me. Any suggestions?

Edited by Landslyde
Link to comment
Share on other sites

do the disabling of the groupid select/option menu in your php code, rather than using javascript.

 

when you submit the groupid select/option menu and redisplay the page, use the fact that you now have a non-default/non-zero groupid value to cause the disabled attribute to be output in that select/option menu. i would also only output the 'dump' button and any other form controls that are dependent on having a valid groupid, if you actually have a non-default/non-zero groupid value.

 

submitting the 'dump' button would clean up any data and clear the fact that you have a groupid selected. this would cause the page to be displayed without the disabled attributed in the groupid select/option menu and would prevent the output of the 'dump' button and the rest of the form controls.

Link to comment
Share on other sites

  • Solution

here's an example -

<?php
session_start();

// post method form processing code
if($_SERVER["REQUEST_METHOD"] == "POST")
{
    // load
    if(isset($_POST['load'])){
        // process the groupid input, for brevity, just save it in the session variable
        // you would normally validate/cast it as a positive integer and detect if one of the choices has been selected
        $_SESSION['groupid'] = $_POST['groupid'];
        
        // do whatever processing you want when a groupid has been selected
    }
    
    // dump
    if(isset($_POST['dump'])){
        // do whatever processing you want for this condition
        
        // clear the groupid
        unset($_SESSION['groupid']);
    }
    
    // any other post method form processing code....
}
?>



<?php
// at the part of your code where you are producing the output for the form -
?>
<form method='post'>
<select name="groupid" <?php echo isset($_SESSION['groupid']) ? 'disabled':''; ?>>
<option value='0'>GROUP</option>
<?php
foreach(range(1,10) as $group){ // you should be retrieving the group numbers from wherever you have the group information stored, not hard-coded in the code
    $sel = isset($_POST['groupid']) && $_POST['groupid'] == $group ? 'selected' : '';
    echo "<option value='$group' $sel>$group</option>\n";
}
?>
</select>
<?php
if(!isset($_SESSION['groupid'])){
    // no groupid selected
    echo "<input type='submit' name='load' value='Load'>";
} else {
    // a groupid is selected, output the stuff for when there is a groupid
    echo "<input type='submit' name='dump' value='Dump'>";

    // whatever else you want to output
}
?>
</form>

if would help you greatly in creating the program logic that does what you want if you organize your code with the form processing code grouped together and the code that produces the output on your page grouped together.

  • Like 1
Link to comment
Share on other sites

I appreciate you taking the time to show me this, mac_gyver.  It helped a lot.  Of all the times I searched on how to load options in a select box, not once did I come across code like yours.  Clean, very nice.  Also new to me was changing a buttons label/value.  Pure silk, that.  I'm putting it together right now.  If possible, I'll send you a link with a username and pass to get in to my site so you can view it.  I've been working on it for 6 months straight :)  Thanks again for the insight and taking the time I know you probably don't really have to show me that code.  Much appreciated.

Edited by Landslyde
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.