want to access php varable in java script

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
lokesh_kumar_s
Forum Commoner
Posts: 48
Joined: Mon Apr 13, 2009 5:39 am
Contact:

want to access php varable in java script

Post 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.
Last edited by Benjamin on Fri Jun 12, 2009 7:22 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
Popcorn
Forum Commoner
Posts: 55
Joined: Fri Feb 21, 2003 5:19 am

Re: want to access php varable in java script

Post 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"
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: want to access php varable in java script

Post by mikemike »

Code: Select all

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