Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Problems w/ Javascript Function in PHP

Posted on 2007-07-23
9
Medium Priority
?
186 Views
Last Modified: 2008-02-07
I've had success using the following Javascript function in ASP, but I'm currently having no luck whatsoever trying to use it in PHP:

<script type="text/javascript">

function ToggleDisplay(img_name, div_id)
{      
      if (document.getElementById(div_id).style.visibility == 'hidden')
      {
            document.getElementById(img_name).src = 'minus.gif';
            document.getElementById(div_id).style.visibility = 'visible';
      }      
      else
      {
            document.getElementById(img_name).src = 'plus.gif';
            document.getElementById(div_id).style.visibility = 'hidden';
      }      
}
</script>

Here's the PHP code:

<?php

?>
  <table width="500" align="center" border="1">
  <tr>
     <td align="center"><b>Promoter Region</b></td>             
  </tr>
  </table>
 
  <div align="center" style="overflow:auto;">  
<?
$MotifNum=0;
$Motif_File = fopen("/home/websites/genework/html/DATA/test", "r");
while (!feof($Motif_File))
{
   $Motif = trim(fgets($Motif_File));
   $Seq_File = fopen("/home/websites/genework/html/DATA/$Motif", "r");
   $count=1;
   while (!feof($Seq_File))
   {
      $line = trim(fgets($Seq_File));
        switch($count)
        {
          case 1:
           $TotalFound = $line;
             $count++;
             break;
        case 2:
           $TotalGenes = $line;
             $count++;
           break;
        case 3:
             $ExpectProb = $line;
           $count++;
           break;
          case 4:
         ?><tr><IMG NAME="arrow<?=$MotifNum?>" SRC="plus.gif" onclick="ToggleDisplay('arrow<?=$MotifNum?>', 'R<?=$MotifNum?>R'); return false;"><b><?=$Motif?></b> : Found <?=$TotalFound?> Times in <?=$TotalGenes?> Gene(s)
             <div id="R<?=$MotifNum?>R" style="visibility:hidden;">
           <?
           $count++;
         break;
      }
        if ($count>4)
        {
           echo"$line\n";
      }
   }
   fclose($Seq_File);
 ?></div></tr><?
   $MotifNum++;
}
fclose($Motif_File);
?>
</div></table></body>
</html>

The nutshell version of the above is that for every Motif listed in the file 'test', there's a corresponding file in the same directory with a number of sequences that contain that motif.  So, everytime a user clicks on the gif next to the Motif, the sequences become visible.  Otherwise, they remain hidden.  As stated, this function and the function call worked in ASP, but it's not here, so I'm curious as to whether there are PHP-specific issues for which I should be looking.

Jason
0
Comment
Question by:cdromes
  • 5
  • 4
9 Comments
 
LVL 11

Expert Comment

by:BraveBrain
ID: 19551709
Any chance we can see the generated code (as from view Source in the browser)?

Client-side JavaScript doesn't care what language you use to generate the code, but only what's present in the fully generated page.
0
 
LVL 11

Expert Comment

by:BraveBrain
ID: 19551731
I'm guessing though that it doesn't work because of more opening divs than closing div, so if you don't want to paste the generated page source look for that.
0
 

Author Comment

by:cdromes
ID: 19551796
I took out the extra </div> tag; still no luck.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cdromes
ID: 19551804
Here's the source:

<html>
<head>
<script type="text/javascript">

function ToggleDisplay(img_name, div_id)
{      
      if (document.getElementById(div_id).style.visibility == 'hidden')
      {
            document.getElementById(img_name).src = 'minus.gif';
            document.getElementById(div_id).style.visibility = 'visible';
      }      
      else
      {
            document.getElementById(img_name).src = 'plus.gif';
            document.getElementById(div_id).style.visibility = 'hidden';
      }      
}
</script>
</head>
<body>
  <table width="500" align="center" border="1">
  <tr>
     <td align="center"><b>Promoter Region</b></td>             
  </tr>
  </table>
  <div style="overflow:auto;">

<IMG NAME="arrow0" SRC="plus.gif" onclick="ToggleDisplay('arrow0', 'R0R'); return false;"><b>TTTTTT</b> : Found 3 Times in 1 Gene(s)
             <div id="R0R" style="visibility:hidden;">
           >RPC53
TCTTCGTCTTCTAACAGAGAATGGTCATCATCTTTATTACTGTCGCCAAAGGTGGATTGTCCAAGTAAAA
CCTTTTCCCTTTTAGAAGCAAAATCATCAACTTCGTTTAAACCATACGGATTAATCTCATCCCCAACTTC
AGAGGTCTTAGTTCTGTTTGAGCCTTTGCGTACCATATTATTCTATACACCCACTCTAATATGTTTTCTC
TTCCTCCAAAACTTTTTGTATAAAAGCATCAGTGAGATGAGCTATGATGAGATGAGCTTCATTATTGAA<
b>TTTTTT</b>TTCATTGCAGAAGCGCAGTTTGAAAATTTTTCATCCGCCCTGCAAAGTCCAA<b>TTT
TTT</b>GATCAGCAGAAGATAATGACCAGAGAATTATGGTATTCAAGAGAACAATTCAAAAGAGGACAG
CTAGAAAGATATTGAGGTATTTATAATTTGCTAGACTAACCGAAAGTCGAAGCGTAATCTTG<b>TTTTT
T</b>ATTAAGCGGGATAGCATTCATTAAGT

</div>
</div>
0
 
LVL 11

Accepted Solution

by:
BraveBrain earned 1200 total points
ID: 19551890
Oddly enough I get that code to work in IE (not in FF though).
The reason that's odd is that you haven't set an id for your image, but you use getDocumentById() to reference it.

You have two choices:
1. change both references of document.getElementById(img_name).src to:
document.images[img_name].src

or
2. add an id to your image:
?><tr><IMG ID="arrow<?=$MotifNum?>" NAME="arrow<?=$MotifNum?>" SRC="plus.gif" onclick="ToggleDisplay('arrow<?=$MotifNum?>', 'R<?=$MotifNum?>R'); return false;"><b><?=$Motif?></b> : Found <?=$TotalFound?> Times in <?=$TotalGenes?> Gene(s)
0
 

Author Comment

by:cdromes
ID: 19551974
Choice #1 worked.  It's odd, indeed, that the original worked in IE, though.

While I've got you "on the phone", if you look at line 4 of the genetic sequence above underneath '>RPC53' you'll see that the < part of the <b> tag got chopped...I did all the parsing and pattern matching in Perl and everything works like a champ, but when I measure out 70 characters in the sequence for each line, occasionally I chop up part of a <b> or a </b> tag.  

Do you know of any efficient way of catching this and fixing it prior to throwing it to the screen?  
0
 
LVL 11

Expert Comment

by:BraveBrain
ID: 19552163
My first thought: Replace all occurences of <b> and </b> with i.e. { and } in the original files (so that they only use 1 character space each). Then change it back when echoing it.
0
 

Author Comment

by:cdromes
ID: 19552196
That's a pretty good idea...have Perl slap the brackets into the string then replace them in PHP.  

I added 50 points to the original question, just in case you came back with something :)  

Thanks!
0
 
LVL 11

Expert Comment

by:BraveBrain
ID: 19552261
Glad to help. Thanks for the points :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question