0

Hi I am strictly have problem in the script language: I am using the two dropdownlist box in php, my application developing by mvc when the page load the project dropdownlist option are loaded. but the user select the project from list and based on the project to load the module list in the dropdownlist.

I tried javascript to select the project value. and i do not know how to pass the client side variable value to server side variable. my code

view.php

<table class="row-fluid">
<tr>
<td><label for="taProj"><?php echo t('Project')?>:</label></td>
<td><select name="taProj" id="taProj" onchange= "checkField(this.value)">
<option value="" >-------------------------</option>
<?php   foreach ($tap as $row){?>
<option value="<?php echo $row['proj_id']; ?>" ><?php echo $row['proj_name'];?></option>
<?php } ?>
</select></td>
<input type="hidden" name="pid" id="pid">
</tr>                   
<tr>                        
<td><label for="taModule"><?php echo t('Module Name')?>:</label></td>
<td><select name="taModule" id="taModule" >
<option value="" >-------------------------</option>
<?php //$myv = $_POST['modid'];
$myvar = print "<script type='text/javascript'>document.getElementById('pid')</script>";
foreach ($tam as $row){ 
if($row['proj_id'] == $myvar){?>
<option value="<?php echo $row['mod_id']; ?>" ><?php echo $row['mod_name'];?></option>
<?php } }?>
</select></td>
</tr>

script code:

function checkField(val){       
    document.getElementById("pid").value = val;
    //alert(document.getElementById("pid").value);
}

the $tap and $tam from controller

My queries is, which method of script to use here and achieve this task. please suggest to me how to do this....

thanks Kumar

Kumar Shanmugam
  • 597
  • 1
  • 11
  • 40
  • I'd say you need to use ajax call to certain.php with any parameter you want and get proper return. – LorDex Oct 10 '13 at 11:50

3 Answers3

2

$("#setResult").load('thepageurl.php?pid='+document.getElementById("pid").value);

this code will send pid by GET method into thepageurl.php and the get the result and put it into setResult element

this is example : http://www.w3schools.com/jquery/ajax_load.asp

NOTE: you must include JQuery library in HTML header .

  • yeah i read the w3school i understand the jquery code but i have query here how to load the demo_test.txt and what is the Jquery library header? – Kumar Shanmugam Oct 10 '13 at 12:52
0

You can do this using an ajax call . That works like

  • U need to tell it method which you want to pass data to. (MethodNameWhereUWantToGo)

  • You need to specify what data to pass.
    (param=document.getElementById("pid").value)

  • You need to specify where to put the results of the return function. (div with id setResult)

function checkField(val)
{       
        document.getElementById("pid").value = val;
       $.ajax({
                    url: "/MethodNameWhereUWantToGo",
                    type: "POST",
                    data: { param:document.getElementById("pid").value },
                    success: function (result) {                    
                        $("#setResult").html(result);

                    }
                });
    }
Imran Jawaid
  • 471
  • 1
  • 10
  • 27
  • You need to have jquery to use it. – Imran Jawaid Oct 10 '13 at 12:01
  • i am using this code its not call my method or function... i have query here if i select project in the dropdown list the ajax will call the method .... ? you mean like this right? – Kumar Shanmugam Oct 10 '13 at 12:23
  • you just need to call this javscript function where u want. It will work. If u want to call it on dropdown list value change then u can use jquery change function on dropdown to call this function. – Imran Jawaid Oct 10 '13 at 12:26
  • yeah i wrote the javascript function onchange=checkField() in the project dropdownlist... but the ajax is not calling the method... – Kumar Shanmugam Oct 10 '13 at 12:44
  • have u added the jquery library ?? press f12 in your chrome and check in developer tools what java script error you are getting. – Imran Jawaid Oct 10 '13 at 13:11
  • you can install firebug its a plugin it will help you to see java script error that is not letting you use ajax call – Imran Jawaid Oct 10 '13 at 13:49
0

Kumar

Basically when you load this code in browser there wont be any php code seen and all the value from your for loops would be fetched and formatted with your html tags. So you can populate tap and for each tap select you get the value and fetch value using ajax.

PRASANTH
  • 695
  • 3
  • 15
  • 33
  • I can understand what you mean but one important thing i not worked with ajax and not familiar. and also i try some code using ajax, i am not succeeded – Kumar Shanmugam Oct 10 '13 at 12:56