Answers.Body: <p>The reason was that my form was missing <code>qfKey</code>. This is a <a href="https://en.wikipedia.org/wiki/Cross-site_request_forgery" rel="nofollow noreferrer">CSRF</a> key generated as part of QuickForm forms.</p>
  <p>This key must be generated for the particular form in use. By using XDebug in Vim, I was able to find out that the form name must be <code>CRM_Contact_Controller_Search</code>, and that it expects a sequence number too.</p>
  <p>So my form is generated as follows:</p>
  <pre><code>&lt;?php
  // ...
  civicrm_initialize();                                                                
  $qfKey = htmlspecialchars(CRM_Core_Key::get('CRM_Contact_Controller_Search', 1));    
  $mySearchForm = &lt;&lt;&lt;HTML                                            
                                                                                       
  &lt;form action=&quot;/civicrm/contact/search/advanced&quot; method=&quot;post&quot;&gt;
    &lt;div&gt;
      &lt;label&gt;Search name or email                                                 
        &lt;input type=&quot;text&quot; name=&quot;sort_name&quot; accesskey=&quot;q&quot; /&gt;         
      &lt;/label&gt;
      &lt;input type=&quot;hidden&quot; name=&quot;hidden_location&quot; value=&quot;1&quot; /&gt;                         
      &lt;input type=&quot;hidden&quot; name=&quot;hidden_custom&quot; value=&quot;1&quot; /&gt;                           
      &lt;input type=&quot;hidden&quot; name=&quot;qfKey&quot; value=&quot;$qfKey&quot;/&gt;                               
      &lt;input type=&quot;hidden&quot; name=&quot;_qf_Advanced_refresh&quot; value=&quot;Search&quot; /&gt;               
    &lt;/div&gt;                                                                                                                
  &lt;/form&gt;          
  HTML;                                                                    
  </code></pre>
  <p>Posting here as it took me ages to figure out!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-16T07:23:39.217Z
Answers.LastActivityDate: 2021-03-16T07:23:39.217Z
Answers.UserDisplayName: artfulrobot
Body: <p>I wanted to include a custom form on our intranet that would initiate a contact search when submitted, like the quick search does if you enter text and press Enter.</p>
  <p>I copied the form but when it came through to the search page a message popped up saying &quot;Because your session timed out, we have reset the search page.&quot;</p>
  
Tags: <integration>
Title: Create an external form to launch a search
ThreadText: # Create an external form to launch a search
  
  (Asked by: username_0 on 2021-03-16)
  
  I wanted to include a custom form on our intranet that would initiate a contact search when submitted, like the quick search does if you enter text and press Enter.
  
  I copied the form but when it came through to the search page a message popped up saying "Because your session timed out, we have reset the search page."
  
  **Tags:** integration
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2021-03-16. Score: 3
  
  The reason was that my form was missing `qfKey`. This is a [CSRF](https://en.wikipedia.org/wiki/Cross-site_request_forgery) key generated as part of QuickForm forms.
  
  This key must be generated for the particular form in use. By using XDebug in Vim, I was able to find out that the form name must be `CRM_Contact_Controller_Search`, and that it expects a sequence number too.
  
  So my form is generated as follows:
  
  ```
  <?php
  // ...
  civicrm_initialize();                                                                
  $qfKey = htmlspecialchars(CRM_Core_Key::get('CRM_Contact_Controller_Search', 1));    
  $mySearchForm = <<<HTML                                            
                                                                                       
  <form action="/civicrm/contact/search/advanced" method="post">
    <div>
      <label>Search name or email                                                 
        <input type="text" name="sort_name" accesskey="q" />         
      </label>
      <input type="hidden" name="hidden_location" value="1" />                         
      <input type="hidden" name="hidden_custom" value="1" />                           
      <input type="hidden" name="qfKey" value="$qfKey"/>                               
      <input type="hidden" name="_qf_Advanced_refresh" value="Search" />               
    </div>                                                                                                                
  </form>          
  HTML;                                                                    
  
  ```
  
  Posting here as it took me ages to figure out!
  
  
  ---
  
  