0

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

2
  • call function module???you mean get_module()?? Commented Oct 8, 2013 at 7:54
  • you can do it through ajax request Commented Oct 8, 2013 at 7:54

2 Answers 2

1
  <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.

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

4 Comments

hi i actually i using concrete5 cms. its have own properties of mvc. so i want pass the projid value to my controller php file. here no need to call mention the php file just use the $this->action('function name'). so will you help me how to call the function through php code...
i don't know about concrete5 cms. but this code will work in your cms. just give url path. which i have updated in my answer check
i updated my answer check and refer given link also there he has called controller method from ajax... you can do same way
thanks for your suggestion but its not work for my scenario... the link solution is different. thanks a lot once again
0

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

?>

1 Comment

hi i actually i using concrete5 cms. its have own properties of mvc. so i want pass the projid value to my controller php file. here no need to call mention the php file just use the $this->action('function name'). so will you help me how to call the function through php code...

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.