Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <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>
- <?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;
- }
- ?>
- <!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');
- }
- });
- });
- });
- <?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>";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement