954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

pass variable from php to javascript

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

<?
session_start();
 require_once('common/dblayer.php');
  $db=new dblayer();
  
$id=$employer_id;
$empid=$db->getgroupname($id);
$groupname=$empid[0]["group1"];
echo $groupname;
				
?>

javascript code

//Contents for menu 1
var menu1=new Array()
menu1[0]='<a href="employercp.php">Job Portal</a>'
menu1[1]='<a href="cggroup.php?id=<? echo $groupname; ?>">Groups</a>'
lydia21
Junior Poster
183 posts since Nov 2007
Reputation Points: 19
Solved Threads: 5
 

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 <?

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

thanks for your reply.still its not working

lydia21
Junior Poster
183 posts since Nov 2007
Reputation Points: 19
Solved Threads: 5
 

could you explain more. whats not working. is the variable returning a value. is there something wrong with the javascript. ect.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

try:

var d=document.form1;
var v=d.inputname.value;
alert(v);
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

this is actually a menu .i have included in all the pages.

<?
session_start();
 require_once('common/dblayer.php');
  $db=new dblayer();
  
$id=$employer_id;
$empid=$db->getgroupname($id);
$groupname=$empid[0]["group1"];
echo $groupname;
				
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<script type="text/javascript">

var menu1=new Array()
menu1[0]='<a href="employercp.php">Job Portal</a>'
menu1[1]='<a href="cggroup.php?id=<?php echo $groupname; ?>">Groups</a>'


</script>
</head>
lydia21
Junior Poster
183 posts since Nov 2007
Reputation Points: 19
Solved Threads: 5
 

please help me in solving the above code

lydia21
Junior Poster
183 posts since Nov 2007
Reputation Points: 19
Solved Threads: 5
 

Are you sure it isn't working ? This code with slight modification to yours is working.

<?php
session_start();
 $groupname = "test";
echo $groupname;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">

var menu1=new Array()
menu1[0]='<a href="employercp.php">Job Portal</a>'
menu1[1]='<a href="cggroup.php?id=<?php echo $groupname; ?>">Groups</a>'
document.write(menu1[0] + menu1[1]);
</script>

I believe $groupname is empty in your case. :-/

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

hi
i tried printing the group name and it is working in one page.i have sent my file as attachment

Attachments header_group.php (8.69KB)
lydia21
Junior Poster
183 posts since Nov 2007
Reputation Points: 19
Solved Threads: 5
 

I don't really understand what your script does!

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

when a user click a menu.it will drop down all the options.its a menu bar.
eg when u click file in IE it will display New,File.......
something like tat

lydia21
Junior Poster
183 posts since Nov 2007
Reputation Points: 19
Solved Threads: 5
 

I just passed a dummy value for $groupname and it works. I stick to my words, your variable is empty, so it isn't working.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

it is so easy to pass variable from php to javascript.
Lets consider there are two scripting in one page like below.

<?php //php coding
$ x=90;
?>

now we will pass the value of x from php to javascript;
So, lets write another code to pass value

now the value of x is passed to id;

mehrab
Newbie Poster
5 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You