从下拉列表中选择选项,具体取决于两个下拉列表,它将显示mysql database php中的主题选项

时间:2017-01-30 14:38:52

标签: javascript php jquery html mysql

Database Screen Shot

1)CSE

2)ECE

学期

1)1

2)3

3)5

主题

1)DBMS

2)OS

3)DCLD

4)SS

以上三个字段都在数据库中。我想要的是从下拉框中选择 CSE ,从< 3 需要从主题下拉框中选择strong>学期下拉框, DBMS ,依此类推。

我已经为两个下拉框配置了选项选项,即主题表示当我从中选择选项时,相应的值已经从学期下拉列表中选择

以下代码运行良好。请从学期下拉列表中选择主题下拉列表来帮助我

代码如下:

  

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;
}
?>

1 个答案:

答案 0 :(得分:0)

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>";
}
?>