pass variable from php to javascript

Reply

Join Date: Nov 2007
Posts: 183
Reputation: lydia21 is an unknown quantity at this point 
Solved Threads: 5
lydia21 lydia21 is offline Offline
Junior Poster

pass variable from php to javascript

 
0
  #1
Jul 31st, 2008
hi
i want to pass variable from php to javascript.i want to pass my group[name please tell me.the current code is not working
  1. <?
  2. session_start();
  3. require_once('common/dblayer.php');
  4. $db=new dblayer();
  5.  
  6. $id=$employer_id;
  7. $empid=$db->getgroupname($id);
  8. $groupname=$empid[0]["group1"];
  9. echo $groupname;
  10.  
  11. ?>
javascript code
  1. //Contents for menu 1
  2. var menu1=new Array()
  3. menu1[0]='<a href="employercp.php">Job Portal</a>'
  4. menu1[1]='<a href="cggroup.php?id=<? echo $groupname; ?>">Groups</a>'
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: pass variable from php to javascript

 
0
  #2
Jul 31st, 2008
they way you are doing it is pretty much the only way you can pass a variable to the javascript. make sure $groupname is returning a value.

also, try using <?php instead of <?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 183
Reputation: lydia21 is an unknown quantity at this point 
Solved Threads: 5
lydia21 lydia21 is offline Offline
Junior Poster

Re: pass variable from php to javascript

 
0
  #3
Jul 31st, 2008
thanks for your reply.still its not working
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: pass variable from php to javascript

 
0
  #4
Jul 31st, 2008
could you explain more. whats not working. is the variable returning a value. is there something wrong with the javascript. ect.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,076
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: pass variable from php to javascript

 
0
  #5
Jul 31st, 2008
try:
  1. var d=document.form1;
  2. var v=d.inputname.value;
  3. alert(v);
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 183
Reputation: lydia21 is an unknown quantity at this point 
Solved Threads: 5
lydia21 lydia21 is offline Offline
Junior Poster

Re: pass variable from php to javascript

 
0
  #6
Jul 31st, 2008
this is actually a menu .i have included in all the pages.
  1. <?
  2. session_start();
  3. require_once('common/dblayer.php');
  4. $db=new dblayer();
  5.  
  6. $id=$employer_id;
  7. $empid=$db->getgroupname($id);
  8. $groupname=$empid[0]["group1"];
  9. echo $groupname;
  10.  
  11. ?>
  12. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  13. <html xmlns="http://www.w3.org/1999/xhtml">
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  16.  
  17. <script type="text/javascript">
  18.  
  19. var menu1=new Array()
  20. menu1[0]='<a href="employercp.php">Job Portal</a>'
  21. menu1[1]='<a href="cggroup.php?id=<?php echo $groupname; ?>">Groups</a>'
  22.  
  23.  
  24. </script>
  25. </head>
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 183
Reputation: lydia21 is an unknown quantity at this point 
Solved Threads: 5
lydia21 lydia21 is offline Offline
Junior Poster

Re: pass variable from php to javascript

 
0
  #7
Aug 1st, 2008
please help me in solving the above code
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: pass variable from php to javascript

 
0
  #8
Aug 1st, 2008
Are you sure it isn't working ? This code with slight modification to yours is working.
  1. <?php
  2. session_start();
  3. $groupname = "test";
  4. echo $groupname;
  5. ?>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  10. <script type="text/javascript">
  11.  
  12. var menu1=new Array()
  13. menu1[0]='<a href="employercp.php">Job Portal</a>'
  14. menu1[1]='<a href="cggroup.php?id=<?php echo $groupname; ?>">Groups</a>'
  15. document.write(menu1[0] + menu1[1]);
  16. </script>
I believe $groupname is empty in your case.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 183
Reputation: lydia21 is an unknown quantity at this point 
Solved Threads: 5
lydia21 lydia21 is offline Offline
Junior Poster

Re: pass variable from php to javascript

 
0
  #9
Aug 2nd, 2008
hi
i tried printing the group name and it is working in one page.i have sent my file as attachment
Attached Files
File Type: php header_group.php (8.7 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: pass variable from php to javascript

 
0
  #10
Aug 2nd, 2008
I don't really understand what your script does!
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC