0

Hie,I had asked this question already but I have make it briefly now please share some logic. 1st dropdown List-enter image description here Contain Class-1, class-2, class-3..etc. 2nd dropdown List-enter image description here Depends on class.If we chose class-1 and class-2 then respective Student Id will show on 2nd dropdown list. I want selected Classes in respective variables and selected turbines in respective variable. Then I will get this selected variables and use in another page.

I want multiple selection in each dropdown list. suppose I have select one Class-1 then, On the classId second dropdown list will fill with respective studentID,again I am select another class-2 then again Add respective studentID list in 2nd dropdown list.

In my databse script ClassId and StudentId this attribute present in same table only (not diffrent tables).

Table view somewhat like this->

 ClassID    StudentID   StudentNm 
      1        101         abc 
      1        102         xyz 
      1        103         jkl 
      2        201         uio
      2        202         tyu 
      3        302         qwe 
      3        305         zxc
9
  • can you please more clear? Commented Feb 28, 2015 at 10:43
  • @bablu I want multiple selection from both dropdown list and 2nd dropdown dependent on 1st dropdown. Commented Feb 28, 2015 at 10:47
  • As you have already asked the same question ( see stackoverflow.com/questions/28758879/… ) you should clarify the original question rather than creating a new question. Commented Feb 28, 2015 at 10:58
  • 1
    You are populating second dropdown from classID, It means you need to bind ajax function on change event of first dropdown. Commented Feb 28, 2015 at 11:15
  • 2nd dropdown values from another table? Commented Feb 28, 2015 at 11:21

2 Answers 2

2

try this sample

your table shoule be like this

   CREATE TABLE 'data'
   (
   'id' int primary key auto_increment,
    'data' varchar(50),
     'weight' int(2),
   );

  CREATE TABLE `data_parent` 
   (
  `pid` int(11) primary key auto_increment,
  `did` int(11) unique,
  `parent` int(11),
  Foreign key(did) references data(id)
  )

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".country").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;

$.ajax
({
type: "POST",
url: "ajax_city.php",
data: dataString,
cache: false,
success: function(html)
{
$(".city").html(html);
} 
});

});

});
</script>
Country :
<select name="country" class="country">
<option selected="selected">--Select Country--</option>
<?php
include('db.php');
$sql=mysql_query("select id,data from data where weight='1'");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$data=$row['data'];
echo '<option value="'.$id.'">'.$data.'</option>';
} ?>
</select> <br/><br/>

City :
<select name="city" class="city">
<option selected="selected">--Select City--</option>
</select>

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<select multiple="multiple" name="classId" class="select-box">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
    <option value="5">Option 5</option>
</select>

 <script src="https://code.jquery.com/jquery-2.1.3.js"></script>
 <script>
    $('.select-box').change(function(){
        var classId = $(this).val();

        $.ajax({
            url : 'getSub.php',
            type: 'POST',
            dataType : 'JSON',
            data : {
                'classId' : classId,
            },
            success : function(data){
                console.log(data);
            }

        });
    });
 </script>
</body>
</html>

When you print selectedValues values will be printed in array format. in getSub.php you can get values and send it as json

getSub.php

<?php

$classIds = implode(',', $_POST['classId']);

$stmet = "SELECT StudentID from TBL where ClassID IN ('$classIds')";

$query = mysql_query($stmet);

$result = mysql_fetch_array($query);

echo json_encode($result);
?>

13 Comments

You need to pass selectedValues as parameter. Otherwise dropdown will not be based on class value, also event will be "change" not "click".
@bablu You have to mean, the selected Class Id get on getSub .php page. and on that page select student value and show on second dropdown.
@bablu It work on button event or on selection of 1st dropdown? because, In my html view 2nd drop down exactly bottom of the first dropdow so,
I want it on change on select-box and also multiple selection on both dropdown
@bablu Yes your code is working like charm! Thank you! Your approch is good to question. but,I have one query that, Here we have use list box, Can we use dropdownList only..meance I have to say, If I select one class then dropdown list is close again, then again select another class and so on...Here you have using dropdown list box, but I want in dropdown list. acn we approch ths?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.