We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48126
    • 3 Posts
    Hi

    I tried to build a little search form but i can't get it to work because preg_match does not find/match anything.

    From a snippet (form_snippet) i load a form (Formit) which loads a other snippet (search_snippet) as sucessMessage. So far everything works. In the search_snippet i can access the search string (
    $searchstr
    ) but preg_match can't do anything with it. If i strip off anything from the string like this,
    $searchstr = preg_replace('/[^A-Za-z0-9\-]/', '', $searchstr);
    , the thing left is
    fisearchstr
    , which looks for me like the Formit var i pass down to the snippet (
    [[!+fi.searchstr]]
    ). The string i pass down as following:
    &successMessage=`[[!habitats_search? &searchstr=`[[!+fi.searchstr]]`]]`
    . Also i tried about all combinations on using the string in preg_match (
    preg_match("/$searchstr/i", $v)
    or
    preg_match("/".$searchstr."/i", $v)
    or
    preg_match('@'.$searchstr.'@i', $v)
    , etc.)

    Can anyone tell me or have me some suggestions, on how i have to convert the string that preg_match does use it properly?

    Cheers

    Versions:
    - MODX Revolution 2.2.14-pl (traditional)
    - PHP 5.4.29
    - CentOS release 6.5 (Final) [ed. note: infoflora last edited this post 12 years, 3 months ago.]
      • 3749
      • 24,544 Posts
      First, are you sure the string being searched is as it should be (and is a string)?

      What exactly, is the search string your are sending? Please put code tags around it (highlight it and click on the <> tool) so the forum doesn't change it.

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 48126
        • 3 Posts
        Thanks for the reply. Yes, its a string from the input... or at least in my understanding.

        Here is the code of the two snippets:

        form_snippet
        ---------------
         
        
        ... some code for the list for the autocomplete function ...
        
        $js = "<script>
        $(function() {
          var availableTags = ['".implode('\',\'',$result)."'];
          $( '#searchstr' ).autocomplete({
            source: availableTags,
        	minLength: 3
          });
        });
        </script>";
        $modx->regClientStartupScript($js);
        
        echo"[[!FormIt?
           &store=`1` 
           &validate=`searchstr:required`
           &successMessage=`[[!habitats_search? &searchstr=`[[!+fi.searchstr]]` &phy=`".$phy."`]]` 
        ]]
        
        [[!FormItRetriever]]
        
        [[!+fi.validation_error_message:!empty=`
        ...
        `]]
        
        <form action='[[~[[*id]]]]' method='post'>
        	<div class='col-lg-12'>
        		<div class='input-group custom-search-form'>
        			<input type='text' class='form-control' name='searchstr' id='searchstr' value='[[!+fi.searchstr]]'>
        			<span class='input-group-btn'>
        				<button class='btn btn-default' type='submit'>
                      		<span class='glyphicon glyphicon-search'></span>
                    	</button>
                	</span>
            	</div><!-- /input-group -->
            </div>
        </form>
        
        <br><br><br>
        
        [[!+fi.successMessage]]
        ";


        search_snippet
        ----------------
        
        ... some code for the array $result ...
        
        echo "search for: $searchstr<br><br>\n";
        
        echo "var_dump: ";
        var_dump($searchstr);
        echo "<br><br>\n";
        
        if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $searchstr))
        {
            echo "special chars found!<br><br>\n";
        }
        
        $searchstr = preg_replace('/[^a-zA-Z0-9_ -]/s', '', $searchstr);
        
        if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $searchstr))
        {
            echo "special chars found 2!<br><br>\n";
        }
        
        echo "strip done: ";
        print($searchstr); 
        echo "<br><br>\n";
        
        foreach ($result as $d)
        {
          foreach ($d as $v)
          {
        	if(preg_match("/$searchstr/i", $v))
        	{
        	  $f = 1;
            }
        
          }
        
          if ($f == 1)
          {
            echo "found : ".$d[2]."<br>\n";
        	unset($f);
          } 
        }
        


        This getting me the output:

        search for: Arabidion alpinae

        var_dump: string(18) "Arabidion alpinae"

        special chars found!

        strip done: fisearchstr

        found : --! test entry with fisearchstr !--
          • 3749
          • 24,544 Posts
          First of all, you should not use echo in a MODX snippet. It may interfere with MODX's ability to parse some of the tags. Instead, use a variable called $output like this:

          $output = '';
          $output .= "search for: $searchstr<br><br>\n";
          

          Then at the end:

          return $output;


          Second, I don't think this will work:

          &successMessage=`[[!habitats_search? &searchstr=`[[!+fi.searchstr]]` &phy=`".$phy."`]]`


          I don't think the fi.searchstr placeholder tag will have been replaced at the point where the habitats_search snippet runs. In fact, I'm fairly sure that the habitats_search snippet will run before the FormIt snippet is run, so there's no chance of the user's input being available thee. I think it is trying to use the placeholder tag as a search string.

          Personally, I would not use FormIt for this. I would create a custom snippet (which you're doing anyway) and put all the logic in the snippet so I didn't have to rely on the specific order in which FormIt does things.

          I'd also leave out the JS until the search was working properly and maybe put the form either in the resource content field, or in a Tpl chunk to be retrieved in the snippet with $modx->getChunk().

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 48126
            • 3 Posts
            Thanks a lot for your help! I got it working.

            The search_snippet is fired as the "validation success message" at the end of the Formit-form, so it gets triggered at the right time, but somehow the placeholder in the "&successMessage" string does not get replaced. I just had to change the placeholder...

            &successMessage=`[[!habitats_search? &searchstr=`".$_POST['searchstr']."` &phy=`".$phy."`]]`


            like this, it is working now... even when its a bit of a mess.

            Also, thanks for the other advices!
              • 3749
              • 24,544 Posts
              Since the string is in the $_POST, that's a good solution. smiley
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting