Answers.Body: <p>Since you have the jQuery tag, I assume you're open to a jQuery solution.</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/19491336/how-to-get-url-parameter-using-jquery-or-plain-javascript">This StackOverflow question</a> covers pulling a variable from a URL argument.</li>
  <li>Then it's just a matter of identifying the CSS identifier of the field you want to fill (right-click, use &quot;Inspect Element&quot;) and referencing it.  E.g.:</li>
  </ul>
  <pre><code>var state = GetURLParameter('state');
  $('#state_province_id-2').val(state);
  </code></pre>
  <p>The <a href="https://select2.org/programmatic-control/add-select-clear-items" rel="nofollow noreferrer">Select2 documentation</a> suggests this should work on Select2 widgets as well.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-21T13:02:55.420Z
Answers.LastActivityDate: 2021-04-21T13:02:55.420Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.Body: <p>Alternative answer is a Civi extension that uses the <code>buildForm</code> hook to prepopulate the field.  Here's some example code that does something very similar on a custom field:</p>
  <pre><code>function trackingcodes_civicrm_buildForm( $formName, &amp;$form ) {
    if ($formName == 'CRM_Contribute_Form_Contribution_Main' ||
        $formName == 'CRM_Contribute_Form_Contribution_Confirm' ||
        $formName == 'CRM_Contribute_Form_Contribution_ThankYou' ) {
      CRM_Core_Resources::singleton()-&gt;addStyleFile('trackingcodes', 'trackingcodes.css');
    }
    if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
      if (isset($_GET['track1'])) {
        $defaults['custom_180'] = $_GET['track1'];
      }
      if (isset($_GET['track2'])) {
        $defaults['custom_181'] = $_GET['track2'];
      }
      if (isset($_GET['track3'])) {
        $defaults['custom_182'] = $_GET['track3'];
      }
      $form-&gt;setDefaults($defaults);
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-21T13:05:54.713Z
Answers.LastActivityDate: 2021-04-21T13:05:54.713Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>On a profile listing page, you can query string to pre-select the field. e.g. add <code>&amp;state_province-Primary=NN</code> where NN is the ID in the civicrm_state_province table.</p>
  <p>However, on a profile create page, specifically on a contribution page, this does not work.</p>
  <p>How can we make this work?</p>
  
Tags: <profile><jquery>
Title: Query string state/province field
ThreadText: # Query string state/province field
  
  (Asked by: username_0 on 2021-04-19)
  
  On a profile listing page, you can query string to pre-select the field. e.g. add `&state_province-Primary=NN` where NN is the ID in the civicrm\_state\_province table.
  
  However, on a profile create page, specifically on a contribution page, this does not work.
  
  How can we make this work?
  
  **Tags:** profile, jquery
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2021-04-21. Score: 1
  
  Since you have the jQuery tag, I assume you're open to a jQuery solution.
  
  * [This StackOverflow question](https://stackoverflow.com/questions/19491336/how-to-get-url-parameter-using-jquery-or-plain-javascript) covers pulling a variable from a URL argument.
  * Then it's just a matter of identifying the CSS identifier of the field you want to fill (right-click, use "Inspect Element") and referencing it. E.g.:
  
  ```
  var state = GetURLParameter('state');
  $('#state_province_id-2').val(state);
  
  ```
  
  The [Select2 documentation](https://select2.org/programmatic-control/add-select-clear-items) suggests this should work on Select2 widgets as well.
  
  
  ---
  
  ### Answer by username_1 on 2021-04-21. Score: 1
  
  Alternative answer is a Civi extension that uses the `buildForm` hook to prepopulate the field. Here's some example code that does something very similar on a custom field:
  
  ```
  function trackingcodes_civicrm_buildForm( $formName, &$form ) {
    if ($formName == 'CRM_Contribute_Form_Contribution_Main' ||
        $formName == 'CRM_Contribute_Form_Contribution_Confirm' ||
        $formName == 'CRM_Contribute_Form_Contribution_ThankYou' ) {
      CRM_Core_Resources::singleton()->addStyleFile('trackingcodes', 'trackingcodes.css');
    }
    if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
      if (isset($_GET['track1'])) {
        $defaults['custom_180'] = $_GET['track1'];
      }
      if (isset($_GET['track2'])) {
        $defaults['custom_181'] = $_GET['track2'];
      }
      if (isset($_GET['track3'])) {
        $defaults['custom_182'] = $_GET['track3'];
      }
      $form->setDefaults($defaults);
    }
  }
  
  ```
  
  
  ---
  
  