Hello! I am somewhat new to php and am hoping someone can help me out with a problem. I have a php page with a php include file that contains javascript. The javascript is for a stylized sort menu that sorts the current results on the parent page. Therefore, I need to get the full url (including url variables) of the parent file from within a js script in the include file. So far this is what I have, but it doesn't work ($pageURL is empty when looking at the page source):

parent file:

<?php // get the full page url
	$pageURL = 'http';
	if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
	$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {
	$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	} else {
	$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	}

?>

.
.
.
<?php include("include.php"); ?>

include file:

<script type="text/javascript">

//Contents for a sort menu that sorts the current results on the parent page

menu1[0]='<a href="<?php echo($pageURL);?>&sort=yearDESC">Year: descending</a>'
menu1[1]='<a href="<?php echo($pageURL);?>&sort=yearASC">Year: ascending</a>'
menu1[2]='<a href="<?php echo($pageURL);?>&sort=title">Title: A-Z</a>'

Maybe there is a better way to do this? Any help would be mush appreciated.

    Couldn't you just move the $pageURL variable defintion block into the include file?

    Still, doesn't explain why $pageURL is blank in the include file - simple test of defining a variable, include()'ing a PHP script, and then echo'ing that variable in the included file worked fine for me. Were you perhaps defining $pageURL inside of a function or class? Only the variables at the global scope of the parent script will be available to the include()'ed script.

      OK, so now I have this:

      <?php
      
       $pageURL = 'http';
       if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
       $pageURL .= "://";
       if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
       } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
       }
       return $pageURL;
      
      ?>

      But the page returns as a blank page when this script is included. What have I dont wrong? Is there a better script for getting the current, complete url?

        I'm not sure what you're trying to do; my suggestion was to simply use that code in the included page above that needs the URL rather than try to define it elsewhere.

          When I paste this code in the include file for my menu - I get the same effective result- the menu/include does not show in the parent file. Below if the full code for the include file:
          (I've checked that the mechanics of the menu in this file are working fine)

          <!-----------------begin filter menu----------------->
          
          <?php
          
           $pageURL = 'http';
           if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
           $pageURL .= "://";
           if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
           } else {
            $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
           }
           return $pageURL;
          
          ?>
          
          
          
          <style type="text/css">
          @import url("../../../CSS/style.css");
          
          #dropmenudiv{
          position:absolute;
          border:1px 333333;
          border-bottom-width: 0;
          font:bold;
          line-height:18px;
          z-index:100;
          }
          
          #dropmenudiv a{
          width: 100%;
          display: block;
          text-indent: 3px;
          border-bottom: 1px solid #333333;
          padding: 1px 0;
          text-decoration: none;
          font-weight: normal;
          }
          
          #dropmenudiv a:hover{ /*hover background color*/
          background-color: #999999;
          }
          </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="<?php echo($pageURL);?>&sort=yearDESC">Year: descending</a>'
          menu1[1]='<a href="<?php echo($pageURL);?>&sort=yearASC">Year: ascending</a>'
          menu1[2]='<a href="<?php echo($pageURL);?>&sort=title">Title: A-Z</a>'
          
          
          var menuwidth='70px' //default menu width
          var menubgcolor='#444444'  //menu bgcolor
          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+';background-color:'+menubgcolor+'" 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>
          
          
          <div class="navmenu">
          <table width="82" height="20" border="0">
            <tr>
          <td width="76" align="right" bgcolor="#333333" scope="col"><div align="left"><span class="notextdecor"><a href="default.htm" onClick="return clickreturnvalue()" onMouseover="dropdownmenu(this, event, menu1, '100px')" onMouseout="delayhidemenu()"><strong><font onMouseOver="this.style.color='#FFFFFF'" onMouseOut="this.style.color = '#CCCCCC'">Sort By:</font></strong>&nbsp;<img src="http://www.agci.org/images/arrow_open_mini.gif" alt="arrow down" border="0" align="texttop" /></a></span></div></td>
          
            </tr>
          </table>
          </div>
          <!-----------------end filter menu----------------->
          
          

          Any thoughts?

            Also, when this script is put in an include file it returns the url of the include, NOT the parent file, which I what I need.

              ...the menu/include does not show in the parent file....

              That's because you have a [man]return[/man] in there that causes the included file to stop at that point (or the main file if you have the code in there).

                Are you including all that on multiple pages? It would be much cleaner to have the CSS and JavaScript in their own separate files so that it isn't being repeatedly downloaded. Retain here only the parts that vary from page to page. In this case:

                var menu1=new Array()
                menu1[0]='<a href="<?php echo($pageURL);?>&sort=yearDESC">Year: descending</a>'
                menu1[1]='<a href="<?php echo($pageURL);?>&sort=yearASC">Year: ascending</a>'
                menu1[2]='<a href="<?php echo($pageURL);?>&sort=title">Title: A-Z</a>'
                

                Incidentally, all that should be in the <head> of the HTML document; having it in the middle of the body is somewhat bad form.

                  Write a Reply...