Use PHP variable in the html - onmouseover

pinkspider123
pinkspider123 used Ask the Experts™
on
Hi Guys,
I am working on a rollover menu.
which connect to the MySQL database first, and then get the menu title..............
and then display it out.

I am success to connect the database, however, In the second level of the rollover menu....I can't get it work. When I choose the 1st level, the second level menu didn't come out.

I think the problem is in the way I put the [PHP variable] into the JAVA script....the JAVAscript can't get the PHP variable. I need the PHP variable, because I need to use this to be the table title of the 2nd-level menu.
And then I use the javafunction to display the 2-nd level menu base on this title.
so is that some problem in this coding ?

<td class="tds" onmouseover="showmenu('<?php $name ?>')"  onclick="window.location='#';" style="border:1px solid gray;background-color:silver;width:150;text-align:center"><?php echo $name; ?></td></tr>


Following are my whole coding:

<html>
<head>
<style>
body{font-family:arial;}
table{font-size:80%;background:black}
a{color:black;text-decoration:none;font:bold}
a:hover{color:#606060}
td.menu{background:lightblue}
table.menu
{
font-size:100%;
position:absolute;
visibility:hidden;
}
</style>

<script type="text/javascript">


function showmenu(elmnt)
{
document.all(elmnt).style.visibility="visible"
}

function hidemenu(elmnt)
{
document.all(elmnt).style.visibility="hidden"
}
</script>

</head>

<body>
<h3>Please select the category</h3>
<p>Mouse over these options to see the drop down menus</p>
</body>

<?php



echo '<p>Please choose a category:</p>';



$cat_array = get_categories();



create_cat($cat_array);







function db_connect()

{



      $result = mysql_connect("bra.bra.bra.bra.bra.bra", "brabrabra", "brabrabra");

      if (!$result)

            return false;

      if (!mysql_select_db("super"))

            return false;

      return result;

}



function db_result_to_array($result)

{

      $res_array = array();



      for ($count=0; $row = mysql_fetch_array($result); $count++)

            $res_array[$count] = $row;



      return $res_array;

}



function get_categories()

{

      $conn = db_connect();

      $query = 'select name from categories';

      $result = mysql_query($query);

      

      if (!$result)

            return false;

      

      $num_cats = @mysql_num_rows($result);



      if($num_cats == 0)

            return false;



      $result = db_result_to_array($result);

      return $result;

}



function get_subcat($name)

{

      $conn = db_connect();

      $query = "select name

              from subcategories

              where name= $name";

      $result = mysql_query($query);

      

      if (!$result)

            return false;

      

      $num_subcats = @mysql_num_rows($result);



      if($num_subcats == 0)

            return false;



      $result = db_result_to_array($result);

      return $result;

}



function create_cat($cat_array)

{

      if (!is_array($cat_array))

      {

            echo 'NO CAEGORIES CURRENTLY AVAILABLE<br/>';

            return;

      }

      ?>

      <table width="100%">

      <table cellpadding=0 cellspacing=0 name=link_table id=link_table style="position:relative">

      <tr>

      <?php

      foreach($cat_array as $row)

      {

            $url = 'show_cat.php?carid='.($row['catid']);

            $title = $row["name"];

            //echo '<li>';

            show_cat($url, $title);

      }

      echo '<hr/>';

      ?>

      </table>

      <?php

}



function show_cat($url, $name)

{

      // output URL as link and br

      $subcat_array=get_subcat($name);

      create_subcat($cat_array);

      ?>

      <td class="tds" onmouseover="showmenu('<?php $name ?>')"  onclick="window.location='#';" style="border:1px solid gray;background-color:silver;width:150;text-align:center"><?php echo $name; ?></td></tr>

         <?php

}



function create_subcat($cat_array)

{

      if (!is_array($cat_array))

      {

            echo 'NO SUBCAEGORIES CURRENTLY AVAILABLE<br/>';

            return;

      }

      ?>

      <table width="100%">

      <table cellpadding=0 cellspacing=0 name=link_table id=link_table style="position:relative">

      <tr>

      <?php

      foreach($cat_array as $row)

      {

            $url = 'show_cat.php?carid='.($row['catid']);

            $title = $row["name"];

            //echo '<li>';

            show_subcat($url, $title);

      }

      echo '<hr/>';

      ?>

      </table>

      <?php

}



function show_subcat($url, $name)

{



      ?>

      <td class="tds" onmouseover="showmenu('<?php $name ?>')"  onclick="window.location='#';" style="border:1px solid gray;background-color:silver;width:150;text-align:center"><?php echo $name; ?></td></tr>

         <?php

}

?>

</html>

thank you.........
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
<td class="tds" onmouseover="showmenu('<?php echo($name); ?>')"  onclick="window.location='#';" style="border:1px solid gray;background-color:silver;width:150;text-align:center"><?php echo $name; ?></td></tr>



try that code.


-Brian

Commented:
Brian,

 Nice catch....took me a second to notice the echo missing ;)

Cheers,
Kevin
you could also alternatively use
<?=$name?> for a shorter syntax.. that will just echo it out right away.
also you probably need an ID feild for instance otherwise you might pull a javascript error when you are looking for the elmnt.



<td class="tds" onmouseover="showmenu('<?=$name?>')" id=<?=$name?>  onclick="window.location='#';" style="border:1px solid gray;background-color:silver;width:150;text-align:center"><?=$name?></td></tr>

onmouseover="showmenu('<?php $name ?>')"

Change that to either:
onmouseover="showmenu('<?php echo $name ?>')"

or

onmouseover="showmenu('<?=$name ?>')"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial