0

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>'
5
Contributors
12
Replies
14
Views
8 Years
Discussion Span
Last Post by mehrab
0

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

0

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

0

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

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

0

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

Attachments
<?
session_start();
 require_once('common/dblayer.php');
  $db=new dblayer();
  
$id=$employer_id;
$empid=$db->getgroupname($id);
echo $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" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.button9 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #CCFEFE; font-weight: bold; }
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
-->
</style>
<style type="text/css">

#dropmenudiv{
width:100px;
background-image:url(images/button_02.png);
position:absolute;
border:1px solid #ffffff;
border-bottom-width: 0;
font:normal 12px Verdana;
line-height:18px;
z-index:10;
}

#dropmenudiv a{

width: 100%;
display: block;
text-indent: 3px;
border-bottom: 1px solid #ffffff;
padding: 1px 0;
text-decoration: none;
font: Arial, Helvetica, sans-serif ;
color: #CCFEFE;
font-weight:bold;
}

#dropmenudiv a:hover{
background-image:url(images/button_01.png) ;
background-repeat:repeat-x/*hover background color*/
}


.dropdownmenu{
visibility:hidden;
width: 165px;
background-image: url(images/button_02.png);

}

</style>

<script type="text/javascript">

/***********************************************
* AnyLink Drop Down Menu-  Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source 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=<?php echo $groupname; ?>">Groups</a>'



//Contents for menu 2, and so on
var menu2=new Array()
menu2[0]='<a href="cg_postjob.php">Post Jobs</a>'
menu2[1]='<a href="searchcgjobs.php">Search Jobs</a>'
menu2[2]='<a href="cmyjobs.php">My Jobs</a>'

var menu3=new Array()
menu3[0]='<a href="postresume.php">Post Resume</a>'
menu3[1]='<a href="searchresume.php">Search Resume</a>'
menu3[2]='<a href="myresume.php">My Resume</a>'



var menu4=new Array()
menu4[0]='<a href="mg_create.php">Create Agent</a>'


var menu5=new Array()
menu5[0]='<a href="postarticle.php">Post Article</a>'
menu5[1]='<a href="viewarticles.php">View Article</a>'

		


/////No further editting needed

var menuwidth='165px' //default menu width
var disappeardelay=250  //menu disappear speed onMouseout (in miliseconds)
var hidemenu_onclick="yes" //hide menu when user clicks within menu?

/////No further editting needed

var ie4=document.all
var ns6=document.getElementById&&!document.all

if (ie4||ns6)
document.write('<div id="dropmenudiv" style="visibility:hidden;width:'+menuwidth+'" onMouseover="clearhidemenu()" onMouseout="dynamichide(event)"></div>')

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}


function showhide(obj, e, visible, hidden, menuwidth){
if (ie4||ns6)
dropmenuobj.style.left=dropmenuobj.style.top="-500px"
if (menuwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=menuwidth
}
if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
obj.visibility=visible
else if (e.type=="click")
obj.visibility=hidden
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=0
if (whichedge=="rightedge"){
var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
}
else{
var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset
var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure){ //move up?
edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge
}
}
return edgeoffset
}

function populatemenu(what){
if (ie4||ns6)
dropmenuobj.innerHTML=what.join("")
}


function dropdownmenu(obj, e, menucontents, menuwidth){
if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
clearhidemenu()
dropmenuobj=document.getElementById? document.getElementById("dropmenudiv") : dropmenudiv
populatemenu(menucontents)

if (ie4||ns6){
showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
}

return clickreturnvalue()
}

function clickreturnvalue(){
if (ie4||ns6) return false
else return true
}

function contains_ns6(a, b) {
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function dynamichide(e){
if (ie4&&!dropmenuobj.contains(e.toElement))
delayhidemenu()
else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
delayhidemenu()
}

function hidemenu(e){
if (typeof dropmenuobj!="undefined"){
if (ie4||ns6)
dropmenuobj.style.visibility="hidden"
}
}

function delayhidemenu(){
if (ie4||ns6)
delayhide=setTimeout("hidemenu()",disappeardelay)
}

function clearhidemenu(){
if (typeof delayhide!="undefined")
clearTimeout(delayhide)
}

if (hidemenu_onclick=="yes")
document.onclick=hidemenu



</script>
</head>


<body>
 <div align="center">
 <table width="883" height="20">
   <tr><td>
 
	<div align="center">
	  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="883" height="165">
        <param name="movie" value="Top 3.swf" />
        <param name="quality" value="high" />
        <embed src="Top 3.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="883" height="165"></embed>
      </object>
	</div>
 

   <div align="center">
    <table width="863" height="20"  border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="10" valign="top"><img src="images/buttonlt.gif" width="9" height="20" align="top" /></td>
        <td width="861" background="images/button.gif"><table width="863" height="20"  border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td width="20">&nbsp;</td>
             <td width="180" height="20" align="center"  class="button9" onClick="return clickreturnvalue()" onMouseover="dropdownmenu(this, event, menu1, '150px')" onMouseout="delayhidemenu()"><font color="#CCFEFE">HOME</font></td>
              <td width="180" height="20" align="center" class="button9" onMouseover="return dropdownmenu(this, event, menu2, '150px')" onMouseout="delayhidemenu()"><font color="#CCFEFE">JOBS</font>  </td>
              <td width="180" height="20"align="center"  class="button9" onMouseover="return dropdownmenu(this, event, menu3, '150px')" onMouseout="delayhidemenu()"><font color="#CCFEFE">RESUME</font></td>
              <td width="180" height="20" align="center"   class="button9" onMouseover="return dropdownmenu(this, event, menu4, '150px')" onMouseout="delayhidemenu()"><font color="#CCFEFE">MAGIC AGENT </font></td>
              <td width="180" height="20" align="center"   class="button9" onMouseover="return dropdownmenu(this, event, menu5, '150px')" onMouseout="delayhidemenu()"><font color="#CCFEFE">ARTICLE</font></td>
              <td width="20" height="20" align="center" class="button9">&nbsp;</td>
            </tr>
        </table></td>
        <td width="12" valign="top"><img src="images/buttonrt.gif" width="11" height="20" /></td>
      </tr>
    </table>
  </div>
	 
</td></tr></table>

</body>
</html>
0

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

0

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.

0

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;

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.