Member Avatar for lydia21

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>'
Member Avatar for somedude3488

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

Member Avatar for lydia21

thanks for your reply.still its not working

Member Avatar for somedude3488

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

Member Avatar for Shanti C

try:

var d=document.form1; var v=d.inputname.value; alert(v);
Member Avatar for lydia21

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>
Member Avatar for lydia21

please help me in solving the above code

Member Avatar for nav33n

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. :-/

Member Avatar for lydia21

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

Member Avatar for nav33n

I don't really understand what your script does!

Member Avatar for lydia21

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

Member Avatar for nav33n

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.

Member Avatar for mehrab

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

<script type="text/javascript">//javascript coding
var id;
</script>

<?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
<script type="text/javascript">//javascript coding
id=<?php echo $x ?>;
</script>
now the value of x is passed to id;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.