Note that there are some explanatory texts on larger screens.

plurals
  1. POindex undefined when receiving $.post from jQuery?
    primarykey
    data
    text
    <p>I am getting a very self explanatory error. However the index, as far as I can tell is not only 100% defined, it also contains a value. This is the latest of a series of silly problems that's driving me insane today.</p> <p>Here is the code you've probably seen a million times that should work.</p> <p><strong>jQuery</strong></p> <pre><code>$('#projects').click(function (e) { alert(aid); $.post('core/functions/projects.php', { aid: aid }) .done(function(data) { alert(aid); $('#home_div').hide(); $('#pcd').fadeIn(1000); }) .fail(function(jqXHR, status, error) { alert(error); }); }); </code></pre> <p>Which alerts me twice with the value of <code>6</code></p> <p><strong>PHP</strong></p> <pre><code>&lt;?php require_once "$_SERVER[DOCUMENT_ROOT]/core/init.php"; if(isset($_POST)) { $aid = $_POST['aid']; echo $aid; } else { echo 'fail'; } ?&gt; </code></pre> <p>I receive this error: </p> <pre><code>Notice: Undefined index: aid in C:\xampp\htdocs\core\functions\projects.php on line 5 </code></pre> <p>So I added a little deeper check to my if else and it now looks like this</p> <pre><code>&lt;?php require_once "$_SERVER[DOCUMENT_ROOT]/core/init.php"; $account_id; if(isset($_POST['aid'])) { $aid = $_POST['aid']; echo $aid; } else { echo 'fail'; } </code></pre> <p>and now it spits back <code>fail</code>. I already went <a href="https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770836#12770836">here</a>, to check out the error and it's exactly as I thought it was. That doesn't mean it makes any more sense to me <em>why</em> I'm getting it.</p> <p>So lets review. My <code>jQuery</code>, is hitting the right <code>PHP</code> file because I am getting responses from that specific <code>PHP</code> file. My variable holds a value because <code>jQuery</code> alerts me twice with the value it holds of 6. However my <code>PHP</code> file is not receiving this value? Can someone please explain to me why? </p> <p><strong>EDIT</strong></p> <p>If I add a line to my <code>jQuery</code> I get another message verifying the data is being sent. Yet I <strong>STILL</strong> receive the error that the $_POST index is not set. This is crazy</p> <pre><code>$('#projects').click(function (e) { alert(aid); $.post('core/functions/projects.php', { aid: aid }) .done(function(data) { alert(aid); alert(data); $('#home_div').hide(); $('#pcd').fadeIn(1000); }) .fail(function(jqXHR, status, error) { alert(error); }); }); </code></pre> <p>Now I get 3 Alert boxes holding the value 6.</p> <p>1 alert box fires before the post is sent with the $aid variable information of '6' <br> 1 alert box fires after the data is received again with the $aid variable '6' <br> 1 alert box fires after the data is received now with the php response which is again '6'!</p> <p>I mean WTF? Will this nightmare ever end? That means even the variable in <code>php</code> is being set from the post! How is this possible? I mean look at the <code>php</code> code all it echos is either 'fail' or $aid and $aid cant have a value unless its set by the post and furthermore it would NOT be giving me the error of Undefined index. I need to go take a break, I am gonna lose it.</p> <p><strong>FIREBUG</strong></p> <p>In firebug I see</p> <p>I see POST -> <br> projects.php -> <br> POST -><br> PARAMETERS -> <br> aid 6 -> <br> SOURCE -> aid=%0D%0A6 <br> RESPONSE -> </p> <p>There is nothing in the response, the brief time I posted a response here was because I left my 'require_once' off and I had my code commented out. </p> <p>There is something odd in the source though. It says SOURCE aid=%0D%0A6 instead of the normal SOURCE aid = 6</p> <p><strong>2ND EDIT</strong></p> <p>I had a large section of code commented out to simplify this example. The code uses the variable and query's for data. Then returns a table created in <code>php</code>. If un-comment the code I can see the table in my RESPONSE in html form. If I go to HTML I can actually see the visual table. So Whats going on? Why is that response inflating my div and not the table? I will now post some more code to explain</p> <pre><code>&lt;?php require_once "$_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, //...more columns 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;"; //...more table headers 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;"; //.. more data 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>So as you can see my full php file actually sends back a table of data. I can see this table in FireBug -> NET -> XHR -> HTML. I can also see all the php code in FireBug -> NET -> XHR -> RESPONSE. However the div that should hold the table only holds the <code>else</code> statement 'could not load projects table'. So I think I'm getting closer, but I'm still stuck. Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload