title: jQuery $.ajax post to PHP file not working
body: <p>So this stems from a problem yesterday that quickly spiraled out of control as the errors were unusual. This problem still exists but the question was put on hold, <a href="https://stackoverflow.com/questions/17903067/index-undefined-when-receiving-post-from-jquery">here</a>, and I was asked to reform a new question that now relates to the current problem. So here we go.</p>
  
  <p>The problem is basic in nature. If you were helping yesterday I have switched from a $.post to an $.ajax post to deliver a variable to my <code>PHP</code> file. However my <code>PHP</code> file seems to never receive this variable stating that the index is 'undefined'. </p>
  
  <p>Now this would normally mean that the variable holds no value, is named incorrectly, or was sent incorrectly. Well after a full day of messing with this (kill me) I still see no reason my <code>PHP</code> file should not be receiving this data. As I'm fairly new to this I'm really hoping someone can spot an obvious error or reccommend another possible solution.</p>
  
  <p>Here's the code</p>
  
  <p><strong>jQuery</strong></p>
  
  <pre><code>$('#projects').click(function (e) {
  alert(aid); 
  $.ajax({    
      url:'core/functions/projects.php',
      type: 'post',
      data: {'aid' : aid},
      done: function(data) {
      // this is for testing
      }
      }).fail (function() {
          alert('error');
      }).always(function(data) {
          alert(data);                                        
          $('#home_div').hide();          
          $('#pcd').fadeIn(1000);
          $('#project_table').html(data); 
      });         
  });
  </code></pre>
  
  <p><strong>PHP</strong></p>
  
  <pre><code>&lt;?php
  
  include "$_SERVER[DOCUMENT_ROOT]/TrakFlex/core/init.php";
  
  if(isset($_POST['aid'])) {  
      $aid = $_POST['aid'];               
      try {
          $query_projectInfo = $db-&gt;prepare("
              SELECT  projects.account_id,
                      projects.project_name,                  
                      projects.pm,    
                      //..irrelevant code      
              FROM projects
              WHERE account_id = ?                        
          "); 
  
          $query_projectInfo-&gt;bindValue(1, $aid, PDO::PARAM_STR);
          $query_projectInfo-&gt;execute();
          $count = $query_projectInfo-&gt;rowCount();
  
          if ($count &gt; 0) {
              echo "&lt;table class='contentTable'&gt;";
              echo "&lt;th class='content_th'&gt;" . "Job #" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "Project Name" . "&lt;/th&gt;";
              //..irrelevant code          
              while ($row = $query_projectInfo-&gt;fetch(PDO::FETCH_ASSOC)) {                
                  echo "&lt;tr&gt;";
                  echo "&lt;td class='content_td'&gt;" . "&lt;a href='#'&gt;" . $row['account_id'] . "&lt;/a&gt;" . "&lt;/td&gt;";
                  echo "&lt;td class='content_td'&gt;" . $row['project_name'] . "&lt;/td&gt;"; 
                  //..irrelevant code
                  echo "&lt;/tr&gt;";
              }
              echo "&lt;/table&gt;";            
          }       
      } catch(PDOException $e) {
          die($e-&gt;getMessage());
      }   
  } else {
      echo 'could not load projects table';
  }
  
  ?&gt;   
  </code></pre>
  
  <p>When I run this code by pressing '#projects' I get 2 alerts. This first alert says '6', which is the value of the variable 'aid' and is expected. The second alert is blank. </p>
  
  <p>Now here is where I get confused. If the post is being sent with a value of 6. Isn't the $_POST['aid'] set? Also if that's true shouldn't my code execute the <code>if</code> portion of my conditional statement <em>rather</em> than my <code>else</code>?. Either way this strikes me as odd. Shouldn't I receive <em>something</em> back from my <code>PHP</code> file?</p>
  
  <p>So in Firebug we trust, right? If I open up Firebug and go through like this <br>
  <code>Firebug -&gt; POST projects.php -&gt; XHR -&gt; POST(tab) -&gt;</code> <br>
  I see 6 in the 'Parameter' window and '6' in the 'Source' window. Then if I click the 'Response' and 'HTML' tabs they both hold no value.</p>
  
  <p>So anyways, that wall of text is my problem. Again, I'm really hoping someone can help me out here. I would hate to waste anymore time on what should be a simple solution.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>If I change my php file to look like this</p>
  
  <pre><code>&lt;?php
  
  if(isset($_POST['aid'])) {  
      $aid = $_POST['aid'];
      echo $aid;
  } else {
      echo 'fail';    
  }
  </code></pre>
  
  <p>The response is now '6'! Hooray! We made a breakthrough! Now why won't it load my table that results from my query? </p>
  
  <p><strong>side note</strong>
  This should of been noted originally if I take away the </p>
  
  <pre><code>if(isset($_POST['aid'])){
  //all my code
  } else {
      //response
  }
  </code></pre>
  
  <p>and just hard code the variable $aid like this</p>
  
  <pre><code>$aid = '6';
  </code></pre>
  
  <p>Then run the <code>PHP</code> file directly the query is successful and the page loads the table its dynamically creating.</p>
  
  <p>Also in response to one of the answers asking me to use </p>
  
  <pre><code>$('#projects').click(function (e) {
      alert(aid); 
      $.ajax({    
          url:'core/functions/projects.php',
          type: 'post',
          data: aid,
          success: function(data) {
          // this is for testing
          }
          }).error (function() {
              alert('error');
          }).complete (function(data) {
              alert(data);                                        
              $('#home_div').hide();              
              $('#pcd').fadeIn(1000);
              $('#project_table').html(data); 
          });     
  });
  </code></pre>
  
  <p>I was using that, but I'm using jQuery v1.10.2 and according to <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow noreferrer">this</a> those methods are or will be deprecated. Either way it made 0 difference in the outcome. </p>
  
  <p>Anyways the question is now. Why is it if I used the simple version I get echo'd back my $aid variable of '6'. However when I try and run my query with it I get <strong>nothing</strong>. Also please try to remember if I hard code the 6, the table creates. </p>