title: AJAX querystring in wordpress
body: <p>I'm trying to use AJAX to pass a querystring within wordpress, I've tried various code on the net using get and post.  However I'm unable to get the parameter within my PHP.  </p>
  
  <p>Using javascript within my page works, however it posts back the page which I want to avoid.  Any suggestions?</p>
  
  <p>working code with postback:</p>
  
  <pre><code>self.location = "?q=" + postcode;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>jQuery.param.querystring(location.href= "?q=" + postcode);
  </code></pre>
  
  <p>ajax query using $_POST['q'] in php doesn't get the parameter</p>
  
  <pre><code>jQuery.ajax({
          type: 'POST',
          url:  'wp-admin/admin-ajax.php' ,
          data: {"q": postcode},
          success: function(data){jQuery('#results').val(postcode);}
      });
  </code></pre>
  
  <p>Thanks</p>
  
  <p>Gemma</p>
  
  <p>Still having issues with this, can't get my querystring into my php:</p>
  
  <pre><code>class PostcodeChecker extends WP_Widget {
        function PostcodeChecker() {
                  $widget_ops = array(
                  'classname' =&gt; 'PostcodeChecker',
                  'description' =&gt; 'Checks if valid postcode in database before request button enable'
        ); 
  
        $this-&gt;WP_Widget(
                  'PostcodeChecker',
                  'Postcode Checker',
                  $widget_ops
        );
  </code></pre>
  
  <p>}</p>
  
  <pre><code>      function widget($args, $instance) { // widget sidebar output
                  extract($args, EXTR_SKIP);
                  echo $before_widget; // pre-widget code from theme
                  print ('&lt;h2 class="blocktitle"&gt;Postcode Checker&lt;/h2&gt; ');
                  print ('&lt;input type="text" id="txtPostcode"/&gt; &amp;nbsp;&amp;nbsp;&lt;input id="btnRegister" type="button" disabled="disabled" value="request application form" /&gt;');
  
                  print ('&lt;br/&gt;&lt;input type="text" id="results"&gt;&lt;/div&gt;&lt;br/&gt;');
                  $postcode = $_GET['q'];
  
                  print $postcode;
                  $mysqli= mysqli_connect('localhost','root', 'My5ql5s5s');
  
                  if (mysqli_connect_errno()) {
                      printf("Connect failed: %s\n", mysqli_connect_error());
                      exit();
                  }
  
              // A QUICK QUERY ON A FAKE USER TABLE
                  $query = "SELECT * FROM db_wp_webteam1.wp_bduk_towns where town_postcode='BT11ER'";
                  $result = $mysqli-&gt;query($query) or die($mysqli-&gt;error.__LINE__);
  
  
              // GOING THROUGH THE DATA
                  if($result-&gt;num_rows &gt; 0) {
                      //while($row = $result-&gt;fetch_assoc()) {
                      //  echo stripslashes($row['town_postcode']);   
                      //}
                      echo 'Results found';
                  }
                  else {
                      echo 'NO RESULTS';  
                  }
  
              // CLOSE CONNECTION
                  mysqli_close($mysqli);
  
                  echo $after_widget; // post-widget code from theme
        }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>function add_js_to_wp_footer(){ ?&gt;
  &lt;script type="text/javascript"&gt;
  jQuery('#txtPostcode').keyup(function(){
      var postcode = jQuery(this).val();
      postcode = postcode.replace(/\s+/g, '');
      if (postcode.length &gt;= 5)
      {
      //self.location = "?q=" + postcode;
      jQuery('#txtPostcode').keyup(function(){
      var postcode = jQuery(this).val();
      postcode = postcode.replace(/\s+/g, '');
      if (postcode.length &gt;= 5)
      {
      jQuery.ajax({
          type: 'POST',
          url:  'wp-admin/admin-ajax.php' ,
          data: {"q": postcode},
          success: function(data){jQuery('#results').val(data);}
      });
      }
  return false;
  });
  
      }
  return false;
  });
  &lt;/script&gt;
  </code></pre>