[英]Create a Chain of drop-down list that base on the input of the previous one form the database in php
i am using database to populate the dorpdown lis. 我正在使用数据库来填充dorpdown lis。 now when i select the option from the first dynamic populated drom down list i want to use it to populate another dropdown list and based on that selection third dromdown list.
现在当我从第一个动态填充的drom下拉列表中选择选项时,我想用它来填充另一个下拉列表并基于该选择第三个dromdown列表。
Here's the code i am working on, 这是我正在处理的代码,
<html>
<?php
include './config.php';
?>
<head>
<meta charset="UTF-8">
<title></title>
<SCRIPT>
$(".dropdown").hide();
$("#testcasedata").on("change", function() {
$(".dropdown").hide();
var value = $("#testcasedata").val();
$("#dropdown" + value).show();
});
</script>
</head>
<body>
<form method="POST" action="createTestCase.php">
// This is the first dropdown list
// It will return testsuite_id in dropdownlist
<?php
$sqll="SELECT testsuite_id FROM assigned_testsuite_tester Where Tester_name = 'Pritesh'";
echo "<select class='form-control' name='testsuite'>";
echo "<option value=''>Select One</option>";
foreach ($conn->query($sqll) as $row){
echo '<option value="'.$row['testsuite_id'].'">'.$row['testsuite_id'].'</option>';
}
echo "</select>";
?>
<input type="submit" name="submit" />
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//testsuite_id will be the input variable for the second dropdownlist
// the second dropdown list will populate with testcase_id.
$testsuite = filter_input(INPUT_POST, 'testsuite');
echo $testsuite;
if($testsuite != ''){
$sqll="SELECT Testcase_id FROM assigned_testsuite_testcase Where testsuite_id = '$testsuite'";
echo "<select name='testcasedata' id='testcasedata' >";
echo "<option value=''>Select One</option>";
foreach ($conn->query($sqll) as $row){
echo '<option value="'.$row['Testcase_id'].'">'.$row['Testcase_id'].'</option>';
}
echo "</select>";
}
// with use of test case id i am retrieving the Product_id
$testcase = filter_input(INPUT_POST, 'testcase');
$sqll="SELECT Product_id FROM Testcase_master Where Testcase_id = '$testcase'";
$productid='';
foreach ($conn->query($sqll) as $row){
$productid = $row['Product_id'];
}
//now product id must be the input for the third dropdownlist.
if($productid != ''){
$sqll="SELECT circle.circle_id,circle.Circle_name FROM circle INNER JOIN assigned_circle_product ON assigned_circle_product.`Product_id` = '$productid'";
echo "<select class='dropdown' name='circledata' id='circledata'>";
echo "<option value=''>Select One</option>";
foreach ($conn->query($sqll) as $row){
echo '<option value="'.$row['Circle_id'].'">'.$row['Circle_name'].'</option>';
}
echo "</select>";
}
}
?>
</body>
i was able to populate the first drop-down list and based on that populate the second drop-down list.but having problem for populating third one. 我能够填充第一个下拉列表,并基于填充第二个下拉列表。但是填充第三个下载列表有问题。 I was browsing through similar questions and find out that i need to use jquery/JavaScript to do that.I don't know that much about jquery but have inserted code for that too but still having problem to populating the third drop down list.
我正在浏览类似的问题,并发现我需要使用jquery / JavaScript来做到这一点。我对jquery知之甚少但是也为此插入了代码,但仍然有问题填充第三个下拉列表。 I am stuck at this point.Please give me some guidance.
我被困在这一点上。请给我一些指导。
I have been learning Javascript/Jquery and this is what i have done. 我一直在学习Javascript / Jquery,这就是我所做的。 I don't know if it's a correct way or not.
我不知道这是不是正确的方式。 please look and tell me if i can do more to improve code.
请看,告诉我是否可以做更多改进代码。
First i have devided whole program into three seprate PHP files. 首先,我将整个程序分成三个单独的PHP文件。
1.Main PHP File 1.主PHP文件
<!DOCTYPE html>
<html>
<?php
include 'header.php';
include 'footer.php';
include './config.php';
?>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
function showCircle(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","GetProduct.php?q="+str,true);
xmlhttp.send();
}
function product1(str) {
str1 = document.getElementById("project");
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","GetTestcase.php?prod="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Add Circle to Product</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
Select the circles where you want your product to test:
</div>
<div class="panel-body">
<div class="dataTable_wrapper">
<form action="circlrproduct.php" method="GET">
<div class="col-lg-6">
<div class="form-group">
<label>Project Name</label>
<?php
$sqll="SELECT Project_id,Project_title FROM project_master";
echo "<select class='form-control' name='project' onchange='showCircle(this.value)'>";
echo "<option value=''>Select One</option>";
foreach ($conn->query($sqll) as $row){
echo "<option value=$row[Project_id]>$row[Project_title]</option>";
}
echo "</select>";
?>
</div>
<div class="form-group">
<div id="txtHint"></div>
<!--<div id="txtHint1"></div>-->
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="submit" value="submit">Submit</button>
<button type="reset" class="btn btn-primary">Reset</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
First i select the project from the drop down list and at at the time of selection a ajax call will be made to get the product. 首先,我从下拉列表中选择项目,在选择时,将进行ajax调用以获取产品。
GetProduct.php GetProduct.php
if(isset($_GET['q'])){ $q = (string)filter_input(INPUT_GET,'q'); }else{ // header('Location:AddCircleToProduct.php'); } $sql2="SELECT Product_id,Product_title FROM product_master where Project_id = '$q'"; $support = $q; $result = $conn -> query($sql2); echo "<label>Product Name</label>"; echo "<select class='form-control' name='product' onchange='product1(this.value)'>"; echo "<option value=''>Select One</option>"; while ($row = mysqli_fetch_array($result)){ echo "<option value={$row['Product_id']}>{$row['Product_title']}</option>"; } echo "</select>";
At the time of product selection one another call is made through Ajax to get the test case. 在产品选择时,通过Ajax再次调用以获取测试用例。
3.GET Test case 3.GET测试用例
if(isset($_GET['prod'])){
$product = filter_input(INPUT_GET, 'prod');
$query = "select * from testcase_master where Product_id = '$product'";
//echo $query;
echo '<div class="panel-body"><div class="dataTable_wrapper"><table class="table table-striped table-bordered table-hover" id="dataTables-example"">';
echo '<thead><tr><th>SELECT</th><th>Testcase Id</th><th>Testcase Title</th><th>Description</th><th>Created by</th><th>Subscriber Type</th><th>Priority</th></tr></thead><tbody>';
if($results = $conn -> query($query) or die(mysqli_errno($conn))){
while ($row = mysqli_fetch_array($results)) {
echo '<tr class="odd gradeX">';
echo "<td><input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox[]\" value=\"".$row['Testcase_id']."\" class='form-control'/></td>";
echo "<td>{$row['Testcase_id']}</td><td>{$row['Testcase_title']}</td><td>{$row['Testcase_desc']}</td><td>{$row['Created_by']}</td><td>{$row['Subscriber_type']}</td><td>{$row['Priority']}</td>";
echo '</tr>';
}
echo '</tbody></table></div></div>';
} else {
echo 'ss' . mysqli_error($conn);
}
}
else{
echo'Sorry You cant access this page Directly.';
}
?>
After getting the list of test case selection of checkbox is made. 获取复选框的测试用例选择列表后。
4.circledata.php 4.circledata.php
<?php
include './config.php';
if(isset($_GET['submit'])){
if(isset($_GET['checkbox'])){
if (is_array($_GET['checkbox'])) {
foreach($_GET['checkbox'] as $value){
$q1 = "INSERT INTO tmtool.assigned_circle_product (`Circle_id`, `Product_id`) VALUES ('.$value.','$_SESSION[prod]')";
if($conn -> query($q1) == TRUE){
echo "Data Entered successfully\n";
} else {
echo 'Error' . mysqli_error($conn);
}
}
} else {
$value = $_GET['checkbox'];
echo $value;
}
}
}
?>
And in the last file all the data are commited into the database. 在最后一个文件中,所有数据都被提交到数据库中。
Please tell me if anything i can do to improve my code. 请告诉我是否可以采取任何措施来改进我的代码。 Thank you.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.