Kumar Shanmugam
Kumar Shanmugam

Reputation: 597

second dropdown based on selection of first dropdown using script and PHP/MySQL

The first dropdown box will be populated when the page loads based on values from a database. The second dropdown box should display a set of values based on the selection from the first dropdown box. I know there have been similar questions asked on here before, but I haven't found a solution that matches my scenario.

UI code:

<tr>
<td><label for="taProj"><?php echo t('Project')?>:</label></td>
<td><select name="taProj" id="taProj" onchange= "get_module()">
<option value="" >-------------------------</option>
<?php   foreach ($tap as $row){?>
<option value="<?php echo $row['proj_id']; ?>" ><?php echo $row['proj_name'];?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<td><label for="taModule"><?php echo t('Module Name')?>:</label></td>
<td><select name="taModule" id="taModule" >
<option value="" >-------------------------</option>
<?php   foreach ($tam as $row){?>
<option value="<?php echo $row['mod_id']; ?>" ><?php echo $row['mod_name'];?></option>
<?php } ?>
</select></td>
</tr>

<script type="text/javascript">
    function get_module(){      
        var projid = document.getElementById('taProj').value;
        alert(projid);
    }   

</script>

mycontroller.php

public function sample(){
        echo $_GET['projid'];       
    }

its working properly.. My need is...

How to call mycontroller.php file function sample() and how to pass the javascript value to the php function parameter.

I new in the script and jquery. please any suggest me... thanks

Kumar

Upvotes: 0

Views: 2316

Answers (2)

404 Not Found
404 Not Found

Reputation: 1223

  <script type="text/javascript">
    function get_module(){      
     var projid = document.getElementById('taProj').value;
     $.ajax({
        url : //ur controller path/methodname(module),
        data : "id="+projid,
        method : 'POST',
        success : function(data) {
             //here you will get response from your controller method and manipulate as per your use..
        }
    })
 }   

 </script>

mycontroller.php

  public function module(){
    echo $_POST['id'];       
}
      or
 public function module($id){
    echo $id;       
}

url path for calling function

  url : '<?php echo str_replace('&amp;', '&', $this->action('model')); ?>',

more info refer this link hope it may help you.

Upvotes: 1

Joke_Sense10
Joke_Sense10

Reputation: 5402

You can do it using Simple jquery Ajax.

Javscript:

<script type="text/javascript">
function get_module(){

var projid = document.getElementById('taProj').value;

//Pass your value to controller.php through ajax

$.ajax({
  url:"controller.php",
  data:{id:projid},
  type:"get",
  success:function(data){
    $("#taModule").html(data);  //inserts your data into second select field
}
});
</script>

Controller.php:

 <?php
  $id = $_GET['id'];

  //enter your DB query to retrive values using $id

 echo "<option value=".$demo['id'].">".$demo['value']."</option>";

?>

Upvotes: 0

Related Questions