php - How to Display MySQL data as a checklist on HTML website? -


it's not complex problem i've been stuck past few days , can't seem figure out. have database called "major_degrees" , want fetch majors table called "majors" in database , display checklist on website using php , html.

i believe fault somewhere in while loop.. printing data text want checklist can check off options want , save them.

here php code have far:

<?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);      if ($result->num_rows > 0) {         // output data of each row         while($row = $result->fetch_assoc()) {             echo $row['degree_name'];             echo "<br/>";         }         return $str;     }      $conn->close();   ?>    

try this:

<?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();   ?> 

when ready checked values , send them page:

jsfiddle demo

$(document).ready(function(){    	$('#myform').submit(function(evnt){  		evnt.preventdefault(); //suppress submitting form (for demo)  		var chk = $('#myform').serialize();  		alert(chk);  	});    }); //end document.ready
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <form id="myform" action="submitto.php" method="get">  	<input id="cb_1" name="cb_1" type="checkbox" /> test 1<br>  	<input id="cb_2" name="cb_2" type="checkbox" /> test 2<br>  	<input id="cb_3" name="cb_3" type="checkbox" /> test 3<br>  	<input id="cb_4" name="cb_4" type="checkbox" /> test 4<br>  	<input type="submit" value="go" />  </form>


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 -