title: PHP $_GET with Ajax/Jquery Request
body: <p>I am trying to set a variable $id=$_GET["categoryID"]. I cannot get it to work. I believe it has to do with the the Ajax request. But I don't know how I have to format it so that it will work in conjunction with that request. I need the variable for a mysql query. Any help is greatly appreciated. This is over my head and have been struggling with it for days. I have tried both GET and POST. Thanks.</p>
  
  <p>I have distilled the page down to this...</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Test $_GET&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;?php 
          if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  
          $id = $_GET["categoryID"];
          //$id=3; 
          }
  ?&gt;
    print_r($_GET) = &lt;?php print_r($_GET); ?&gt;
    &lt;br /&gt;
    print_r($id) = &lt;?php print_r($id); ?&gt; 
  &lt;/body&gt;
  &lt;/html&gt; 
  </code></pre>
  
  <p>Here is the resulting page....</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
      &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
      &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
      &lt;title&gt;Test $_GET&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
            print_r($_GET) = Array
  (
      [categoryID] =&gt; 1001
  )
        &lt;br /&gt;
        print_r($id) =  
      &lt;/body&gt;
      &lt;/html&gt; 
  </code></pre>
  
  <p>Here is the whole page....</p>
  
  <pre><code>&lt;?php
  if (@$_REQUEST['ajax']) {
      $link = $nm33;
      if ($link == false)
          trigger_error('Connect failed - ' . mysql_error(), E_USER_ERROR);
      $connected = mysql_select_db('nm', $link);
      if ($connected) {
  
      //How do I set $id = $_GET["categoryID"] It fails to set the variable.
  
          $id =$_GET["categoryID"];
      //  $id=1;
      // It will work as $id=1    
  
          $results = mysql_query('select * from selectMenu where categoryID= \'' . $id . '\' AND category="' . strtolower(mysql_real_escape_string(strip_tags($_REQUEST['category']))) . '"');
  
      //////////  
  
  
          $json = array();
          while (is_resource($results) &amp;&amp; $row = mysql_fetch_object($results)) {
              //$json[] = '{"id" : "' . $row-&gt;id . '", "label" : "' . $row-&gt;label . '"}';
              $json[] = '"' . $row-&gt;label . '"';
          }
          echo '[' . implode(',', $json) . ']';
          die(); // filthy exit, but does fine for our example.
      } else {
          user_error("Failed to select the database");
      }
  }
  ?&gt;
  &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="js/select-chain.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      &lt;!--
      $(function () {
          var cat = $('#categorySelect');
          var el = $('#elementSelect');
          var attr = $('#attributeSelect');
  
          el.selectChain({
              target: attr,
              url: 'select-menu.php',
              data: { ajax: true, anotherval: "anotherAction" }            
          });        
  
          // note that we're assigning in reverse order
          // to allow the chaining change trigger to work
          cat.selectChain({
              target: el,
              url: 'select-menu.php',
              data: { ajax: true }
          }).trigger('change');
  
      });
      //--&gt;
      &lt;/script&gt;
  &lt;link href="selectMenu.css" rel="stylesheet" type="text/css" /&gt;
  &lt;form action="performance-models.php" method="get"&gt;
    &lt;select name="category" class="dropdown" id="categorySelect"&gt;
      &lt;option selected="selected"&gt;Select Your Vehicle&lt;/option&gt;
      &lt;?php do { ?&gt;
      &lt;option&gt; &lt;?php echo $row_rsMake['make']; ?&gt;&lt;/option&gt;
      &lt;?php } while ($row_rsMake = mysql_fetch_assoc($rsMake)); ?&gt;
    &lt;/select&gt;
    &lt;select name="model" class="dropdown" id="elementSelect"&gt;
      &lt;option selected="selected"&gt;Select Model&lt;/option&gt;
      &lt;option&gt;[none selected]&lt;/option&gt;
    &lt;/select&gt;
    &lt;select name="appYear" class="dropdown" id="attributeSelect" &gt;
      &lt;option selected="selected"&gt; &lt;/option&gt;
      &lt;option&gt;[none selected]&lt;/option&gt;
    &lt;/select&gt;
    &lt;input  type="submit" value="Go"&gt;
  &lt;/form&gt;
  &lt;p&gt;&lt;br /&gt;
    &lt;br /&gt;
    print_r($_GET) = &lt;?php print_r($_GET); ?&gt; &lt;br /&gt;
    print_r($_REQUEST) = &lt;?php print_r($_REQUEST); ?&gt;&lt;br /&gt;
    echo $_REQUEST['categoryID']  &lt;?php  echo $_REQUEST['categoryID'];?&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>Here is select-chain.js</p>
  
  <pre><code>(function ($) {
      $.fn.selectChain = function (options) {
          var defaults = {
              key: "id",
              value: "label"
          };
  
                  var settings = $.extend({}, defaults, options);
  
          if (!(settings.target instanceof $)) settings.target = $(settings.target);
  
          return this.each(function () {
              var $$ = $(this);
  
              $$.change(function () {
                  var data = null;
                  if (typeof settings.data == 'string') {
                      data = settings.data + '&amp;' + this.name + '=' + $$.val();
                  } else if (typeof settings.data == 'object') {
                      data = settings.data;
                      data['category'] = $$.val();
                      data['model'] = $$.val();
                      data['year'] = $$.val();
                  }
  
                  settings.target.empty();
  
                  $.ajax({
                      url: settings.url,
                      data: data,
                      type: (settings.type || 'get'),
                      dataType: 'json',
                      success: function (j) {
                          var options = [], i = 0, o = null;
  
                          for (i = 0; i &lt; j.length; i++) {
                              // required to get around IE bug (http://support.microsoft.com/?scid=kb%3Ben-us%3B276228)
                              o = document.createElement("OPTION");
                              o.value = typeof j[i] == 'object' ? j[i][settings.key] : j[i];
                              o.text = typeof j[i] == 'object' ? j[i][settings.value] : j[i];
                              settings.target.get(0).options[i] = o;
                          }
  
              // hand control back to browser for a moment
              setTimeout(function () {
                  settings.target
                                  .find('option:first')
                                  .attr('selected', 'selected')
                                  .parent('select')
                                  .trigger('change');
              }, 0);
                      },
                      error: function (xhr, desc, er) {
                          // add whatever debug you want here.
              alert("an error occurred here");
                      }
                  });
              });
          });
      };
  })(jQuery);
  </code></pre>