Page 1 of 1

want to access php varable in java script

Posted: Fri Jun 12, 2009 6:32 am
by lokesh_kumar_s
hi i want to access the php variable in java script. see i will explain the problem

first answer to these questions
1>client machine does not know php right. so i cannot access the php variable at client side using java script. but how about this can i access java script variable at server side?

the problem is using php i am getting some entries from database filling it inside the html dropdown list like this

Code: Select all

 
<?php $result=mysql_query("select Company,ManagerName from manager_Prof"); ?>
<select name=drpcompany >
<?php while($array=mysql_fetch_row($result)): ?>
<option><?php echo $array[0]; ?></option>
<?php endwhile; ?>
</select>
 
and iam having another text box i want to display corresponding $array[1] in it. I.e., i want to display corresponding manager id of the company selected from the drop down in the text box.

and i want to do it without going back to server.

Re: want to access php varable in java script

Posted: Fri Jun 12, 2009 8:19 am
by Popcorn
ask PHP to print some JavaScript maybe?

inside <head> do something like:

Code: Select all

 
print '<script type=.....
               var mgrinfo = ['.$array[1].', '.$array[2].'];
               </script>';
 
then your JS can access the variable "mgrinfo"

Re: want to access php varable in java script

Posted: Fri Jun 12, 2009 9:40 am
by mikemike

Code: Select all

<script type="text/javascript">
  var mikemike = "<?php echo $mikemike; ?>";
  alert(mikemike);
</script>
:)