We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25307
    • 114 Posts
    Will a snippet not accept SERVER(’PHP_SELF’) within a snippet? having a tricky time converting a php file to a snippet... will post code if needbe, just checking quick fix perhaps overlooked first. thank you.
      • 27376
      • 576 Posts
      Snippets are executed within an [tt]eval()[/tt] function. However, superglobals as stated in PHP Documentation on variables, are available in any context.
        • 25307
        • 114 Posts
        guess thats not it. maybe it my syntax or I’m overlooking/misunderstand the proper method. here’s the code..

        <?php
        
        // ++++++++++++++++++++++++++++ 
        // part 1 :: propegate drop down box...
        // ++++++++++++++++++++++++++++
        
        $u = $modx->getFullTableName("web_user_attributes"); 
        $w = $modx->getFullTableName("web_groups"); 
        $n = $modx->getFullTableName("webgroup_names"); 
        
        // Begin query
        $sql ="SELECT DISTINCT n.id, n.name FROM {$n} n LEFT JOIN {$w} w ON n.id = w.webgroup LEFT JOIN {$u} u ON u.id = w.webuser"; 
        
        // Check optional paramaters  
        if (isset($type) )
        {
        $sql .=" WHERE u.type = '{$type}'";
        }
        
        if (isset($remodeler) )
        {
        $sql .=" WHERE u.remodeler = '{$remodeler}'";
        }
        
        // execute query
        $sql .=" ORDER BY n.name ASC";
        $categories = $modx->db->query($sql);
        
        if ($categories) {
        	echo '<form name="dd" id="dd" method="POST" action='.$_SERVER['PHP_SELF'].'>';
        	echo '<input type="radio" name="ddType" value="1">Builders</input>';
        	echo '<input type="radio" name="ddType" value="2">Associates</input><br />';
        	echo '<select name="ddCat" onchange="document.dd.submit()" id="ddCat">';
        		echo '<option selected value="Select a category.">Select a category.</option>';
        		while ($row = mysql_fetch_array($categories) ) {
                	echo '<option value="'.$row{'id'}.'">'.$row{'name'}.'</option>';
        	    }
        			echo '</select>';
        			echo '</form>';
        		} else {
        			echo 'There are no categories to display.';
        		
        } 
        
        // ++++++++++++++++++++++++++++ 
        // part 2 :: display results
        // ++++++++++++++++++++++++++++
        
        $ddType = $_POST['ddType'];
        $ddCat = $_POST['ddCat'];
        $ddFields = 'u.company, u.website, u.comment, u.address, u.city, u.state, u.zip, u.fullname,u.phone, u.fax, u.mobilephone, u.email';
        
        $sql2 = "SELECT {$ddFields} FROM {$u} AS u
        JOIN {$w} AS w ON u.id = w.webuser
        WHERE u.type = '{$ddType}'
        AND w.webgroup = '{$ddCat}' 
        ORDER BY u.company ASC";	
        
        if (isset($ddCat) ) {
        	$output = 'User selected -> '. $ddCat . '.';
        	
        	$members = $modx->db->query($sql2);
        
        	if ($members) {
        		$output .= '<table>';	
        		$output .= '<tr><td><strong>Results</strong>';
        		while ($row = $modx->db->getRow($members)) {
        		$output .= '<tr><td>' . $row{'company'} . '</td></tr>';
        		}
        		$output .= '</table>';
        	}
        }
        return $output;
        ?>


        Here’s pretty much what happens. The drop down list is propegated as is it should be, the onchange event occurs and posts the appropriate data.. to the wrong page - like this ->index.php?ddType=2&ddCat=4 - I have a feeling it may involve the rewriting of the document alias or something, just can’t quite get a wrap on it.. Thanks for any input.
          • 22303 MODX Staff
          • 10,725 Posts
          Replace the $_SERVER[’php_self’] reference with $modx->makeUrl($modx->documentIdentifier)); this will give you the proper URL for your form action (assuming it needs to post to this same page).

          You might also replace the echo statements to return their output directly.
            • 25307
            • 114 Posts
            ok, no luck with the api call - but I did get it working correctly after digging a bit deeper through the forums and some more googling..

            I replaced
            <?php
                echo '<form name="dd" id="dd" method="POST" action='.$_SERVER['PHP_SELF'].'>';
            ?>

            with this..
            <?php
                echo '<form name="dd" id="dd" method="GET" action='.$_SERVER['REQUEST_URI'].'>';
            ?>

            and updated the results display variables to also use the GET method..
            <?php
                $ddType = $_POST['ddType'];
                $ddCat = $_POST['ddCat'];
            ?>
            

            I’m not even actually certain if the POST/GET method had anything to do with it working, but the REQUEST_URI variable definitely did. I am using the .htaccess file to handle rewrites, I assume thats why PHP_SELF wasn’t working. Would this also cause the api call you provided to not work? Is there one that would work in this scenario?

            I’m also converting the echo stuff to use return $output, thanks for the heads up on that too. Does this help process the page faster or what?
            Thanx much.