html - Dynamically created checkboxes using ajax from sql result set -
i looking use ajax dynamically create checkboxes each time change selection <select>
tag, see below screenshot section of form relevant:
snippet http://i39.tinypic.com/2ik5xg8.png
note: checkboxes under "queues" should dynamic.
at moment, when change value team grabs team name (in case "test"), using ajax (post) returns manager name team.
what want table has list of "queues" associated each team; going add "onchange" attribute in tags "manager name" field.
below code i'm using accomplish team => manager name dynamic filling:
<script> window.onload = function() { getmanager($("#team").val()); } function getmanager(team) { $.ajax({ type: "post", url: "getmanager.php", data: {team:team} }).done(function( manager ) { $("#manager_name").val(manager); }); } </script>
and here getmanager.php
file uses:
<?php require("../../database/db.php"); $mysqli = new db("nab_reporting"); if ($mysqli->connect_errno) { printf("connect failed: %s\n", $mysqli->connect_error); exit(); } $team=$mysqli->real_escape_string($_post['team']); $result = $mysqli->query("select manager_name team teamname = '".$team."'"); $row = $result->fetch_assoc(); echo $row['manager_name']; mysqli_close($mysqli); ?>
keeping in mind above works; need way use onchange
attribute of manager name field use ajax (similar above), passing php page value in field (in case kane charles) , return result set (array or json maybe?) containing list of queues in database match team name.
below html code each of different bits:
team
<select name="team" id="team" required="required" onchange="getmanager(this.value)"> <?php include(__dir__ . "/../../database/db.php"); $db = new db("nab_reporting"); $result = $db->query("select teamname team"); while ($row = $result->fetch_assoc()) { echo " <option value=\"" . $row['teamname'] . "\">" . $row['teamname'] . "</option> "; } $db->close(); ?> </select>
manager name
<input type="text" name="manager_name" id="manager_name" required="required" onchange="getqueues(this.value)">
queues
<label> queues </label> <div id="queue_options"> <!-- queues dynamically created here --> </div>
i need contents of queue-options
erased , reset queues associated current team; haven't done great deal ajax, hence why i'm posting on here.
this revision should match asking about
php
// make array hold queues $data = array(); // fetch rows of queues $res = $mysqli->query("select * the_queue_table manager='" . $_get["manager"] . "'"); // loop through rows , push queues data array while(($row = mysql_fetch_object($res)) !== false) { array_push($data, $row->queue); } // return data array json object echo json_encode($data);
javascript
// page , send manager name filter $.get("file.php?manager=" + managername, function(page) { // parse json object var data = $.parsejson(page); // remove existing checkboxes $("#queue_options").children().remove(); // add checkboxes div (var item in data){ $("#queue_options").append("<input type=\"checkbox\" value=\"" + item + "\" />" + item + "<br />"); } });
Comments
Post a Comment