7

I want to create a dynamic dropdown menu that one select is based on the value of another select. I tried so many times and failed. I have the code as follows. Anyone can help me to find out what the problem is.

//template:

<script type="text/javascript">  

$(function(){ 
  getSelectVal(); 
  $("#grouptypes").change(function(){ 
      getSelectVal(); 

  }); 
});

function getSelectVal(){ 
    $.getJSON('<?php echo url_for('group_utilization/GroupModification') ?>', {'grouptypes':$("#grouptypes").val()},function(data){ 
        var groups = $("#groups"); 
        $("option",groups).remove(); 
        $.each(json,function(index,array){ 
            var option = "<option value='"+array['id']+"'>"+array['title']+"</option>"; 
            select.append(option); 
        }); 
    }); 
} 

</script>

GroupType: <select id="grouptypes">

  <?php foreach($grouptypes as $type) : ?>

  <?php echo "<option value='" . $type->name . "'>" .$type->name. "</option>"; ?>

  <?php endforeach ?>

</select><br />
<br />Result:<span id="list-of-groups"></span>

<br />
Group: <select name="group" id="groups">

</select><br />

Another file for returning database returned value:

if(isset($_GET["grouptypes"])){
 $grouptypes = $_GET["grouptypes"];

 $query = "SELECT g.name FROM groups g INNER JOIN group_types gt ON(gt.id = g.group_type_id AND gt.name = ?)";

 $groups = $db->getResultsForQuery($query, $grouptypes);
 echo json_encode($groups);
 }
4
  • 1
    When you say you failed, what do you mean? Do you have any error messages you could post? Commented Feb 1, 2012 at 21:47
  • 1
    Are there any script errors? You seem to be referring to an uninitialized variable json $.each(json,function(index,array){ Commented Feb 1, 2012 at 21:50
  • As well as the json variable, the select variable is not being initialized in the code you have posted. Commented Feb 1, 2012 at 21:51
  • Notice: Undefined variable: grouptypes in /Users/alexhu/Work/menagerie/trunk/apps/utilities/modules/group_utilization/actions/actions.class.php on line 47 Commented Feb 1, 2012 at 23:12

4 Answers 4

1

Does your PHP Script sends the appropriate headers for JSON ?

try to put this juste before "echo json_encode($groups);". Eventually put an exit call to avoid extra output.

$groups = $db->getResultsForQuery($query, $grouptypes);
header('Content-type:application/json;charset=utf-8');
echo json_encode($groups);
exit();

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

Comments

0

Should the top block of code actually be:

$(document).ready(function(){ 
  getSelectVal(); 
  $("#grouptypes").change(function(){ 
      getSelectVal(); 

  }); 
});

1 Comment

$(document).ready(function(){}); is the exact same thing as $(function () {});. Here are the docs: api.jquery.com/ready
0

You need to reference the tag with jQuery var before appending:

function getSelectVal(){ 
  var select = $("#grouptypes");

  $.getJSON('<?php echo url_for('group_utilization/GroupModification') ?>', {'grouptypes':$("#grouptypes").val()},function(data){ 
    var groups = $("#groups"); 
    $("option",groups).remove(); 
    $.each(data,function(index,array){ 
        var option = "<option value='"+array['id']+"'>"+array['title']+"</option>"; 
        select.append(option); 
    }); 
  }); 
} 

2 Comments

Hi, Ron. thanks for your answer, but it doesn't work either. It is not able to get the value passed by Json and isset($_GET["grouptypes"]) is false. I am confused
Don't you want to use the passed "data" argument instead of "json" in $.each(json... - I changed to answer above to reflect this change. Hopefully this will resolve the "isset" operation.
0

You might have problem with the way url_for() function as it might not be able output your link properly for getJSON to use. For example if I fiddle with something close to your code (BTW, are you using Symphony?)

 $.post("<? echo 'server.php';?>", function(data) {
                    aler(data);

                    });

            });

I get this link in my JS code

http://dev.wonderland/%3C?%20echo%20'server.php'?%3E

which is obviously not the link I am trying to reach to. Also, make sure to add a semicolon at the end of your PHP code up there and try it again.

Comments

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.