How to Limit selection in checklist created directly from MYSQL database and PHP (*Not by manually making a checklist in HTML*) -


my code far outputs data mysql table on website checklist, having trouble setting limit how many options can checked off, i.e. if want 3 out of 5 boxes checked off how can in php?

note: of answers out there checkbox limit show examples checklist has been manually created in html , using javascript limit put on number of boxes can checked off. however, not case in code; code outputs checklist not manually created in html automatically created pre-set mysql database using php (as shown below). how can put limit boxes checked off in case? solution/examples code appreciated!

here php code:

<?php      $username = "root";     $password = "";     $hostname = "localhost";      $dbname = "major_degrees";     $str='';      // create connection     $conn = new mysqli($hostname, $username, $password, $dbname);      // check connection     if ($conn->connect_error) {         die("connection failed: " . $conn->connect_error);     }       $sql = "select degree_name majors";     $result = $conn->query($sql);      $out = '';     $cnt = 0;     if ($result->num_rows > 0) {         // output data of each row         while($row = $result->fetch_assoc()) {             $cnt++;             $out .= '<input id="cb_' .$cnt. '" class="someclass" type="checkbox" />' .$row['degree_name']. '<br/>';         }         echo $out;     }      $conn->close();   ?> 

like @shadouts said, can't control checkboxes php, need javascript. create list, echo output of query inside html tags. sake of example won't include query returns checkboxes, simple incremental data.

<form action="some_file" method="">     <div id="checkboxes">     <?php         for($i = 0; $i < 8; $i++) {             echo "<input class='checkchange' type='checkbox' name='check' value='ch".($i+1)."'>check ".($i+1)."<br>";         }     ?>     </div>      <input type="submit" value="submit"> </form> 

this creates 8 simple checkboxes (in case, checkboxes, each appropriate value , text. better way, fill checkbox list ajax, since seems new these technologies, i'll try , keep simple possible.

so our checkboxes can seen in browser. what's left control them. jquery.

in script tags have following:

$('.checkchange').change(function() {     var count = 0;      $('#checkboxes input:checked').each(function() {         count++;     });      if (count >= 3) {         $('#checkboxes input:not(:checked)').each(function() {             $(this).attr("disabled", true);         });     } else {         $('#checkboxes input:not(:checked)').each(function() {             $(this).removeattr('disabled');         });     } }); 

just let know, things $('.checkchange') jquery selectors. means select html elements either class, id or else. select elements class, .+classname. select elements id #+id. let's decode above code sample now:

  1. first, whenever checkbox clicked (or more accurately changes), .change function called: $('.checkchange').change
  2. $('#checkboxes input:checked').each: each checkbox has been checked, increment count value one.
  3. if user has checked 3 checkboxes disable ( $(this).attr("disabled", true); ) unchecked ( $('#checkboxes input:not(:checked)').each ) checkboxes.
  4. if not, enable them: $(this).removeattr('disabled');

in order use php inside html tags, file needs .php. file - let's checkbox.php - this:

<!doctype html> <html lang="en"> <head>     <meta charset="utf-8">     <title>checkboxing</title> </head> <body>  <form action="action.php" method="post">     <div id="checkboxes">     <?php         for($i = 0; $i < 8; $i++) {             echo "<input class='checkchange' type='checkbox' name='check' value='ch".($i+1)."'>check ".($i+1)."<br>";         }     ?>     </div>      <input type="submit" value="submit"> </form>  </body> <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <script>     $('.checkchange').change(function() {         var count = 0;          $('#checkboxes input:checked').each(function() {             count++;         });          if (count >= 3) {             $('#checkboxes input:not(:checked)').each(function() {                 $(this).attr("disabled", true);             });         } else {             $('#checkboxes input:not(:checked)').each(function() {                 $(this).removeattr('disabled');             });         }     }); </script> </html> 

to see exact functionality described above, check out this fiddle created.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -