繁体   English   中英

从下拉列表中选择选项,取决于两个下拉列表,这将显示mysql数据库php的主题选项

[英]Select option from dropdown list depending on two dropdown list which will show subject options from mysql database php

数据库屏幕截图

1)CSE

2)ECE

学期

1)1

2)3

3)5

学科

1)数据库管理系统

2)作业系统

3)DCLD

4)SS

以上三个字段都在数据库中。我想要的是当我从Stream下拉框中选择CSE ,从Semester下拉框中选择3时 ,需要从Subject下拉框中选择DBMS ,依此类推。

我已经配置了两个下拉框的选项选择,即主题意味着当我从流中选择选项时,从学期下拉菜单中选择了相应的值

下面的代码运行良好,请从StreamSemester下拉菜单中选择Subject下拉菜单来帮助我

代码如下:

select.php

<html>
<head>
<link rel="stylesheet" type="text/css" href="select_style.css">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function fetch_select(val)
{
$.ajax({
type: 'post',
url: 'fetch_data.php',
data: {
get_option:val
},
success: function (response) {
document.getElementById("new_select").innerHTML=response; 
}
});
}

</script>

</head>
<body>
<p id="heading">Select Subject for Timesheet</p>
<center>
<div id="select_box">
<select onChange="fetch_select(this.value);">
<option>Select Stream</option>
<?php
$host = 'localhost';
$user = 'xx';
$pass = 'zzz';
mysql_connect($host, $user, $pass);
mysql_select_db('sample');

$select=mysql_query("select stream from streamSub group by stream");
while($row=mysql_fetch_array($select))
{
echo "<option>".$row['stream']."</option>";
}

?>
</select>

<select id="new_select">
</select>
</div>     
</center>
</body>
</html>

fetch_data.php

<?php
if(isset($_POST['get_option']))
{
$host = 'localhost';
$user = 'XX';
$pass = 'ZZZ';
mysql_connect($host, $user, $pass);
mysql_select_db('sample');

$stream = $_POST['get_option'];
$find=mysql_query("select subject from streamSub where  stream='$stream'");
while($row=mysql_fetch_array($find))
{
echo "<option>".$row['subject']."</option>";
}

exit;
}
?>

Main.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Fetch Data Using Ajax</title>
<style type='text/css'>
  .hide{
    display: none;
  }
  .show{
    display: block;
  }
</style>
</head>
<body>
<form>
  <div>
    <label>
      Stream
    </label>
    <select id="stream">
      <option value="">Select Stream</option>
      <option value="CSE">CSE</option>
      <option value="ECE">ECE</option>
    </select>
  </div>
  <div>
    <label>
      Semester
    </label>
    <select id="sem">
      <option value="">Select Semester</option>
      <option value="1">1</option>
      <option value="2">3</option>
      <option value="3">5</option>
    </select>
  </div>
  <div id="subjectDiv" class="hide">
     <label>
      Subject
    </label>
    <select id='subject'>

    </select>
  </div>
</form>
 </body>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>
<script>
$(document).ready(function(){
$('#sem').change(function(){
  $.ajax({
    url:'getData.php',
    type:'POST',
    data:{
      stream : $('#stream').val(),
      sem : $('#sem').val()
    },
    success:function(result){
      console.log(result);
        $('#subject').html(result);
        $('#subjectDiv').removeClass('hide');
        $('#subjectDiv').addClass('show');
    }
  });
});
});

getData.php

<?php

$con = mysqli_connect('localhost','root','','test');

$query = "SELECT * FROM getdata WHERE stream = '".$_POST['stream']."' AND sem = ".$_POST['sem'];

$row = mysqli_query($con,$query);

echo "<option value=''>Select Subject</option>";
while($data = mysqli_fetch_assoc($row)){
  echo "<option value='".$data['subject']."'>".$data['subject']."</option>";
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM