vendredi 17 juillet 2020

Web Form: Enable Disable elements depending on # of check boxes that are checked?

I have several check boxes a user can select for various classes. If no selection, hide/disable user's name, email address, & Submit button. If one or more check boxes are selected, then enable/show these elements. Here is the PHP & html code at present:

<?php
function myHideShow(e) {

  var user = document.getElementById("username");
  var email = document.getElementById("email");
  var submit = document.getElementById("submit");

    if (empty($_POST['interest'])) {
      echo("You didn't select any check boxes.");
        user.disabled = true;
        email.disabled = true;
        submit.disabled = true; }
    else {
      echo("You did select at least one check box.");
        user.disabled = false;
        email.disabled = false;
        submit.disabled = false;
    }
  }
?>
<!DOCTYPE html> <html> <head> </head> <body>
  <form name='calculusclassform' method="post" action="">
    <fieldset>
      <legend>Choose your <b>Class</b> interests</legend>
      <input type="checkbox" id="oil" name="interest[]" value="Oil Refinery Optimization" onclick="myHideShow()">
      <label for="oil">Oil Refinery Optimization</label>
      <span>Crude oil ...</span>
      <input type="checkbox" id="curvfit" name="interest[]" value="Curve Fitting Data sets" onclick="myHideShow()">
      <label for="curvfit">Curve Fitting Data sets</label>
      <span>Have a curve fitting problem? ...</span>
      <input type="checkbox" id="coding" name="interest[]" value="Calculus-level Coding" onclick="myHideShow()">
      <label for="coding">Calculus-level Coding</label>
      <span> languages ... </span>
      <h3>Classes are free, but in return, we ask you to share these class offerings with 5+ others.  Thanks.</h3>
      <span>             Name:</span>
      <input type="name" name="username" id="username" disabled><br>
      Email Address:
      <input type="email" name="email" id="email" disabled><br><br>
      <input type="submit" value="Submit" id="Submit" disabled>
    </fieldset>
  </form>
</body> </html>

Running this code shows that my PHP myHideShow function is not found ... got a solution for a newbie to creating web forms?




Aucun commentaire:

Enregistrer un commentaire