Ok I have another question for you smart ones out there (in here):
I have this JS code inside PHP code:

      echo '
            <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
	<!--
		var alternateContent = \'something goes here\';
		document.write(alternateContent); 	
	// -->
            </SCRIPT>
      ';

Now where it says "something goes here" i need to insert the value of a PHP variable whose value is assigned by a PHP function (see my other post http://www.phpbuilder.com/board/showthread.php?threadid=10277005).

In other words I have a function called menuhtml(); that returns a menu for a site. I need to remove the symbol "|" from it and replace it with "<br>" so I do this:

$menuhtml = menuhtml();
$menuhtml = str_replace('|','<br>',$menuhtml);

At this point I need to put this menu inside the JS variable alternateContent but for some reason it don't work...
I tried:
var alternateContent = \'something '.$menuhtml.'goes here\';
and it doesn't work, but strangely this works:
var alternateContent = \'something '.$var.'goes here\';
where $var is simply a variable with a value assigned like this:
$var = 'value';

I am assuming it's a matter of what is inside $menuhtml and for some reason it won't let me put it inside a JS variable that is then printed from within a PHP echo... I know it sounds complicated and catch22-type but I think it needs to be that way, other that this everything seems to work... it's about flash and non-flash content swapping... so I need to check for flash with JS and then according to result swap content, and since everything is in a PHP file with lots of if statements the entire JS script happens to have to be inside a PHP echo... just so don't think I am crazy... ;-)

    $printvar  = "<SCRIPT LANGUAGE=\"JavaScript\" type=\"text/javascript\">\n";
    $printvar .= "<!--\n";
    $printvar .= "var alternateContent = '". $your_var ."';\n";
    $printvar .= "document.write(alternateContent);\n";
    $printvar .= "// -->\n";
    $printvar .= "</SCRIPT>";
    
    print $printvar;
    

      That didn't work...
      I tried exactly what you suggested, except that instead of multiple lines I did it like this:

      $printvar = "
      <SCRIPT LANGUAGE=\"JavaScript\" type=\"text/javascript\">
      <!--\
      var alternateContent = '". $your_var ."';
      document.write(alternateContent);
      // -->
      </SCRIPT>
      ";
      print $printvar;

      It's the same to do it like this and like you suggested, right?
      If not let me know and I'll try your exact way, but if, as I think, the two things are the same then I am afraid your suggestion didn't work...

        yes if I do an echo of it outside of this JS mess it prints out my menu, real smooth, but inside the JS it won't, even though if I put a different variable inside the JS (one that has been assigned in a simple way and is not the result of a function) that one will be shown

          What is the content of $your_var? And does any javascript errors occur?

            No, there are no JS errors.
            Here is what my code looks like (by the way my variable is called $menuhtml):

            function menuhtml() {
            return '
            |
            <a href="'.$menu0URL.'">'.$menu0LINK.'</a> |
            <a href="'.$menu1URL.'">'.$menu1LINK.'</a> |
            <a href="'.$menu2URL.'">'.$menu2LINK.'</a> |
            <a href="'.$menu3URL.'">'.$menu3LINK.'</a> |
            <a href="'.$menu4URL.'">'.$menu4LINK.'</a> |
            <a href="'.$menu5URL.'">'.$menu5LINK.'</a> |
            <a href="'.$menu6URL.'">'.$menu6LINK.'</a> |
            ';
            }
            $menuhtml = menuhtml();
            $menuhtml = str_replace('|','<br>',$menuhtml);
            echo '
            <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
            <!--
            if (!useRedirect) { // if dynamic embedding is turned on
            if(hasRightVersion) { // if we detected an acceptable version
            var oeTags = \'<object classid="clsid😃27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="339" height="284">\'
            + \' <param name="movie" value="menu.swf?phpself='.$SERVER["PHP_SELF"].'&b='.$b.'">\'
            + \' <param name="quality" value="high">\'
            + \' <embed src="menu.swf?phpself='.$
            SERVER["PHP_SELF"].'&b='.$b.'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="339" height="284"><\/embed>\'
            + \'<\/object>\';
            document.write(oeTags); // embed the flash movie
            } else { // flash is too old or we cannot detect the plugin
            // NOTE: height, width are required!
            var alternateContent = \'<table width="339" height="284" border="0" cellpadding="0" cellspacing="0"><tr><td><font size="2"><b><div align="right">'.$menuhtml.'</div></font></b></td></tr></table>\';
            document.write(alternateContent); // insert non-flash content
            }
            }
            // -->
            </SCRIPT>
            ';

            I shall point out that interestingly enough the variables $b and $_SERVER["PHP_SELF"] do come through without a problem but the var $menuhtml doesn't...

            also I should point out that if I do an echo $menuhtml; I get a menu, but if I want to print the contents of $menuhtml inside that table inside the JS inside the PHP echo then it won't print.

              When you say "it doesn't work," are you looking at your page, or the source of the page? My guess is that you are looking at the page itself, and not seeing your menu. If you look at the source, you'll probably see this for each menu item:

              <a href=""></a><br>

              If that's the case, then the problem is that your $menuxURL and $menuxLINK variables need to be declared global inside the function.

              Frankly, I think you are making this too complex. If all the function does is return text, then simply assign that text directly to $menuhtml, rather than call the function. Then you don't have to worry about variable scope.

                I didn't paste that part of the code but the variables are in fact declared globally so when I say that it doesn't work I mean that NOTHING comes up, neither in the html code source nor in the page, just plain nothing in place of where the menu should be

                  instead of:

                  return echo'balblabla';

                  could you do:

                  echo 'blabla bla';

                    what do you mean? I don't do an echo return anywhere

                      Originally posted by marcnyc
                      what do you mean? I don't do an echo return anywhere

                      ah sorry, my bad, I misread.

                        so are there any other suggestions as to what I might be doing wrong with my approach?

                          The only thing I can think of is this: if $b work normally and $menuhtml doesn't, the only difference between the two is that $b is assigned normally (passed through an URL or assigned by a $b = "value"; action) and $menuhtml is the result of an function... So I guess we'd have to find a way to sort of "freeze" the value of $menuhtml and store it as if it was a normally assigned variable... Is there any such way?

                            You really don't need a function just to return text. It would be a lot easier to read and debug if written this way:

                            <?php
                            $menuhtml = '<br>
                            	          <a href="'.$menu0URL.'">'.$menu0LINK.'</a> <br>
                            	          <a href="'.$menu1URL.'">'.$menu1LINK.'</a> <br>
                            	          <a href="'.$menu2URL.'">'.$menu2LINK.'</a> <br>
                            	          <a href="'.$menu3URL.'">'.$menu3LINK.'</a> <br>
                            	          <a href="'.$menu4URL.'">'.$menu4LINK.'</a> <br>
                            	          <a href="'.$menu5URL.'">'.$menu5LINK.'</a> <br>
                            	          <a href="'.$menu6URL.'">'.$menu6LINK.'</a> <br> ';
                            ?>
                            	<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
                            	<!--
                            	if (!useRedirect) {    // if dynamic embedding is turned on
                            		if(hasRightVersion) {  // if we detected an acceptable version
                            		  var oeTags = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="339" height="284">\'
                            		  + '  <param name="movie" value="menu.swf?phpself=<?=$_SERVER["PHP_SELF"]?>&b=<?=$b?>">'
                            		  + '  <param name="quality" value="high">'
                            		  + '  <embed src="menu.swf?phpself=<?=$_SERVER["PHP_SELF"]?>&b=<?=$b?>" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="339" height="284"></embed>'
                            		  + '</object>';
                            		  document.write(oeTags);   // embed the flash movie
                            		} else {  // flash is too old or we cannot detect the plugin
                            		  // NOTE: height, width are required!
                            		  var alternateContent = '<table width="339" height="284"  border="0" cellpadding="0" cellspacing="0"><tr><td><font size="2"><b><div align="right"><?=$menuhtml?></div></font></b></td></tr></table>';
                            		  document.write(alternateContent);  // insert non-flash content
                            		}
                            	}	
                            	// -->
                            	</SCRIPT>
                            

                              Thank you very much for your contribution to this post dnast...
                              unfortunately my JS code has to be inside an PHP echo function becuase it will only be printed if a certain PHP IF statement is true... you know what I mean?

                              I also noticed your syntax <? =$b ?>...
                              I never saw that syntax with the equal sign... what is that? just curious so I know...

                                by the way I just thought of a workaround... I tested it and it works, so unless somebody else has a way to make work the above I guess I'll have to go with this one...

                                I introduced a new variable called $flash and if $flash=="no" then I don't even echo the JS code, but because the JS code has to be called the first time to actually do the content swapping, istead of swapping content I replace the URL...
                                here it goes:

                                $menuhtml = menuhtml();
                                $menuhtml = str_replace('|','<br>',$menuhtml);
                                $title = $GET["title"];
                                $flash = $
                                GET["flash"];
                                if ( $flash == "no" ) {
                                echo '<table width="339" height="284" border="0" cellpadding="0" cellspacing="0"><tr><td><div align="right"><font size="2">please select:<b>'.$menuhtml.'</b></font></div></td></tr></table>
                                ';
                                } else {
                                echo '
                                <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
                                <!--
                                if (!useRedirect) { // if dynamic embedding is turned on
                                if(hasRightVersion) { // if we detected an acceptable version
                                var oeTags = \'<object classid="clsid😃27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="339" height="284">\'
                                + \' <param name="movie" value="menu.swf?phpself='.$SERVER["PHP_SELF"].'&b='.$b.'">\'
                                + \' <param name="quality" value="high">\'
                                + \' <embed src="menu.swf?phpself='.$
                                SERVER["PHP_SELF"].'&b='.$b.'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="339" height="284"><\/embed>\'
                                + \'<\/object>\';
                                document.write(oeTags); // embed the flash movie
                                } else { // flash is too old or we cannot detect the plugin
                                // NOTE: height, width are required!
                                // var alternateContent = \'<table width="339" height="284" border="0" cellpadding="0" cellspacing="0"><tr><td><font size="2"><b><div align="right">'.$b.'you do NOT have the FREE Flash player plug in installed - please <a href="http://www.macromedia.com/go/getflashplayer">click here</a> to dowload it<br> alternatively you may use the menu at the bottom of these pages to navigate the website</div></font></b></td></tr></table>\';
                                // document.write(alternateContent); // insert non-flash content
                                window.location.replace("'.$_SERVER["PHP_SELF"].'?b='.$b.'&flash=no&title='.$title.'");
                                }
                                }
                                // -->
                                </SCRIPT>
                                ';
                                }

                                Hope this helps somebody out there

                                  Originally posted by marcnyc
                                  Thank you very much for your contribution to this post dnast...
                                  unfortunately my JS code has to be inside an PHP echo function becuase it will only be printed if a certain PHP IF statement is true... you know what I mean?

                                  I also noticed your syntax <? =$b ?>...
                                  I never saw that syntax with the equal sign... what is that? just curious so I know...

                                  It would still work outside of <? ?>. For example:

                                  <?php
                                  if (1 > 2) {
                                  ?>
                                  1 is greater than 2
                                  <?php
                                  } else {
                                  ?>
                                  1 is not greater than 2
                                  <?php
                                  }
                                  

                                  It would only print "1 is not greater than 2". However there is a more readable syntax for this.

                                  And the <?=$var?> is just shorthand for <?php echo $var ?>. You'll see a short example towards the bottom of that link.

                                  Anyway, I guess it doesn't matter since you got it working. Good luck on the rest of your site. 🙂

                                    That's very clever! I should have thought about that myself, damnit! ;-)
                                    Anyway, because I am eager to learn I did try your suggestion and it still doesn't work... I wanted to try to to know if it would work but I am afraid it doesn't, so unless somebody else has other clever ideas that are out of my league I'll stick to my workaround...
                                    Thanks for all your help and support and lighting fast replies! This is exciting!

                                      Marc,

                                      Your problem is in your echo statement. You are delimiting the echo with the single quote ('), and you are also using single quotes in your javascript. That won't work. I recommend you use heredoc, or take all of the javascript out of the PHP code as dnast has suggested. However, I wouldn't use the shortcut syntax -- there's a good chance your server has shortcut tags disabled. So, simply use <?php echo $menuhtml; ?>.

                                      If you want to use heredoc syntax, here is your code rewritten. I have tested on my server, and it does work (although in my case the menuxURL and menuxLINK variables are empty, and there is a Javascript error that might be due to incomplete code). The menuhtml() function DOES return the intended HTML:

                                      <?php
                                      function menuhtml() {
                                      $retval = <<<EOS
                                      |
                                      <a href="$menu0URL">$menu0LINK</a> |
                                      <a href="$menu1URL">$menu1LINK</a> |
                                      <a href="$menu2URL">$menu2LINK</a> |
                                      <a href="$menu3URL">$menu3LINK</a> |
                                      <a href="$menu4URL">$menu4LINK</a> |
                                      <a href="$menu5URL">$menu5LINK</a> |
                                      <a href="$menu6URL">$menu6LINK</a> |
                                      EOS;
                                      return $retval;
                                      }
                                      $menuhtml = menuhtml();
                                      $menuhtml = str_replace('|','<br>',$menuhtml);
                                      $phpself = $_SERVER['PHP_SELF'];
                                      echo <<<EOS
                                      <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
                                      <!--
                                      if (!useRedirect) { // if dynamic embedding is turned on
                                      if(hasRightVersion) { // if we detected an acceptable version
                                      var oeTags = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="339" height="284">'
                                      + ' <param name="movie" value="menu.swf?phpself=$phpself&b=$b">'
                                      + ' <param name="quality" value="high">'
                                      + ' <embed src="menu.swf?phpself=$phpself&b=$b" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="339" height="284"><\/embed>'
                                      + '<\/object>';
                                      document.write(oeTags); // embed the flash movie
                                      } else { // flash is too old or we cannot detect the plugin
                                      // NOTE: height, width are required!
                                      var alternateContent = '<table width="339" height="284" border="0" cellpadding="0" cellspacing="0"><tr><td><font size="2"><b><divalign="right">$menuhtml</div></font></b></td></tr></table>';
                                      document.write(alternateContent); // insert non-flash content
                                      }
                                      }
                                      // -->
                                      </SCRIPT>
                                      EOS;
                                      ?>