We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5112
    • 38 Posts
    my homepage will display an overlay based on incoming url. for example mydomain.com/index.php?olay=newsletter or mydomain.com/index.php?olay=partner. based on the olay value a particular onload will be inserted into the body tag.

    i am trying port the homepage code to a modx template and am having issues. it no longer inserts anything into the body tag. i basically cut and pasted the php below into snippets and added snippet calls to my code.

    starting code:
    <?php
        if(isset($_GET['olay']))
        {
          $source=$_GET['olay'];
        }
        else
        {
          $source="";
        }
    
        switch($source)
        {
          case "newsletter":
          $string='onload="$.fn.colorbox({href:\'/welcome-email.php\', open:true, width:600}); "';
          break;
    
          case "partner":
          $string='onload="$.fn.colorbox({href:\'/welcome-partner.php\', open:true, width:400}); "';
          break;
    
        }
    ?>
    <body <?php echo $string; ?>>
    


    template code:
    [!choose-olay!]
    <body [!call-url!] >


    any ideas?
      • 4310
      • 2,310 Posts
      I just tried your snippet code in a snippet named Olay.
      Added [!Olay!] in place of the <body> tag in a template and it worked fine, producing
      <body onload="$.fn.colorbox({href:’/welcome-partner.php’, open:true, width:400}); ">
      I’m not sure what the two different snippet calls in your post are supposed to do, or how they relate to your code.
        • 5112
        • 38 Posts
        thanks for the response. can you share the exact code you have in the snippet?

        how did you work the body tag into the same snippet at the first php block?

        i figured i needed two snippets.

        1) [!choose-olay!]
        <?php
            if(isset($_GET['olay']))
            {
              $source=$_GET['olay'];
            }
            else
            {
              $source="";
            }
        
            switch($source)
            {
              case "newsletter":
              $string='onload="$.fn.colorbox({href:\'/welcome-email.php\', open:true, width:600}); "';
              break;
        
              case "partner":
              $string='onload="$.fn.colorbox({href:\'/welcome-partner.php\', open:true, width:400}); "';
              break;
        
            }
        ?>


        2) [!call-url!]
        <?php echo $string; ?>


        am i thinking about this wrong?
          • 4310
          • 2,310 Posts
          You don’t need two snippets.
          I just used your code as is, in one snippet.
          I’d probably change it slightly to something like =>
          <?php
              if(isset($_GET['olay']))
              {
                $source=$_GET['olay'];
              }
              else
              {
                $source="default";
              }
          
              switch($source)
              {
                case "newsletter":
                $string='<body onload="$.fn.colorbox({href:\'/welcome-email.php\', open:true, width:600}); ">';
                break;
          
                case "partner":
                $string='<body onload="$.fn.colorbox({href:\'/welcome-partner.php\', open:true, width:400}); ">';
                break;
          
                case "default":
                $string='<body>';
                break;
              }
          
          return $string;
          
          ?>

            • 5112
            • 38 Posts
            thanks so much! what you posted works.

            i don’t really know php and was just trying to cut and paste what the original coder did into snippets without changing anything. but your way is much better.

            appreciate the help.
              • 4310
              • 2,310 Posts
              Glad it’s working for you smiley
                • 5112
                • 38 Posts
                i guess i still don’t understand how snippets change how php blocks function. in addition to the above php block i also had this block in the pre-template page. it worked fine, but now as a snippet the javascript does not get placed into the rendered html (ie, index.php?olay=social-notes).

                snippet called [!socialnotes!]
                <?php if ($source == "social-notes"): ?>
                <script type="text/javascript">
                if ( typeof(pmi_share) != 'undefined' ) {
                	pmi_share("link","http://www.mydomain.com/","Title","http://www.mydomain.com/images/logo.gif","Description sentence.")
                }
                $(window).ready(function () {
                	var foundin = $('span:contains("Forward to a Friend")');
                	foundin.css('display', 'none')
                	foundin.trigger('click');
                });
                </script>
                <?php endif; ?>
                

                  • 4310
                  • 2,310 Posts
                  If it’s the same template this might work
                  <?php
                      if(isset($_GET['olay']))
                      {
                        $source=$_GET['olay'];
                      }
                      else
                      {
                        $source="default";
                      }
                  
                      switch($source)
                      {
                        case "newsletter":
                        $string='<body onload="$.fn.colorbox({href:\'/welcome-email.php\', open:true, width:600}); ">';
                        break;
                  
                        case "partner":
                        $string='<body onload="$.fn.colorbox({href:\'/welcome-partner.php\', open:true, width:400}); ">';
                        break;
                        
                        case "social-notes":
                        $string='<script type="text/javascript">
                        if ( typeof(pmi_share) != \'undefined\' ) {
                  	pmi_share("link","http://www.mydomain.com/","Title","http://www.mydomain.com/images/logo.gif","Description sentence.")
                        }
                        $(window).ready(function () {
                  	var foundin = $(\'span:contains("Forward to a Friend")\');
                  	foundin.css(\'display\', \'none\')
                  	foundin.trigger(\'click\');
                        });
                         </script>
                        <body>';
                        break;
                  
                        case "default":
                        $string='<body>';
                        break;
                      }
                  
                  return $string;
                  
                  ?>
                    • 5112
                    • 38 Posts
                    yes, that works. i tried something very similar to what you did but i did not insert all the backslashes. is there a reason they are needed once it becomes a snippet when they were not needed on my pre-template php page?

                    thanks again.

                      • 5112
                      • 38 Posts
                      again i am a php newbie.

                      to answer my own final question. i needed to add the backslashes because of the single-quotes within the strings. you have to escape the single-quotes with backslashes \.

                      @bunk58: thanks again for all your help. my homepage template is now complete.