943,600 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 5629
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 31st, 2008
0

pass variable from php to javascript

Expand Post »
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
PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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>'
Similar Threads
Reputation Points: 19
Solved Threads: 5
Junior Poster
lydia21 is offline Offline
183 posts
since Nov 2007
Jul 31st, 2008
0

Re: pass variable from php to javascript

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 <?
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Jul 31st, 2008
0

Re: pass variable from php to javascript

thanks for your reply.still its not working
Reputation Points: 19
Solved Threads: 5
Junior Poster
lydia21 is offline Offline
183 posts
since Nov 2007
Jul 31st, 2008
0

Re: pass variable from php to javascript

could you explain more. whats not working. is the variable returning a value. is there something wrong with the javascript. ect.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Jul 31st, 2008
0

Re: pass variable from php to javascript

try:
javascript Syntax (Toggle Plain Text)
  1. var d=document.form1;
  2. var v=d.inputname.value;
  3. alert(v);
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Jul 31st, 2008
0

Re: pass variable from php to javascript

this is actually a menu .i have included in all the pages.
PHP Syntax (Toggle Plain Text)
  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>
Reputation Points: 19
Solved Threads: 5
Junior Poster
lydia21 is offline Offline
183 posts
since Nov 2007
Aug 1st, 2008
0

Re: pass variable from php to javascript

please help me in solving the above code
Reputation Points: 19
Solved Threads: 5
Junior Poster
lydia21 is offline Offline
183 posts
since Nov 2007
Aug 1st, 2008
0

Re: pass variable from php to javascript

Are you sure it isn't working ? This code with slight modification to yours is working.
php Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Aug 2nd, 2008
0

Re: pass variable from php to javascript

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, 46 views)
Reputation Points: 19
Solved Threads: 5
Junior Poster
lydia21 is offline Offline
183 posts
since Nov 2007
Aug 2nd, 2008
0

Re: pass variable from php to javascript

I don't really understand what your script does!
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: \ stuff
Next Thread in PHP Forum Timeline: Adding Discount





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC