index undefined when receiving $.post from jQuery? [closed]

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.

Here is the code you've probably seen a million times that should work.

jQuery

$('#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);
    }); 
});

Which alerts me twice with the value of 6

PHP

<?php
require_once "$_SERVER[DOCUMENT_ROOT]/core/init.php";
if(isset($_POST)) {             
    $aid = $_POST['aid'];
    echo $aid;
} else {
    echo 'fail';
}
?>

I receive this error:

Notice: Undefined index: aid in C:\xampp\htdocs\core\functions\projects.php on line 5

So I added a little deeper check to my if else and it now looks like this

<?php
require_once "$_SERVER[DOCUMENT_ROOT]/core/init.php";
$account_id;
if(isset($_POST['aid'])) {          
    $aid = $_POST['aid'];
    echo $aid;
} else {
    echo 'fail';
}

and now it spits back fail. I already went here, 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 why I'm getting it.

So lets review. My jQuery, is hitting the right PHP file because I am getting responses from that specific PHP file. My variable holds a value because jQuery alerts me twice with the value it holds of 6. However my PHP file is not receiving this value? Can someone please explain to me why?

EDIT

If I add a line to my jQuery I get another message verifying the data is being sent. Yet I STILL receive the error that the $_POST index is not set. This is crazy

$('#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);
    }); 
}); 

Now I get 3 Alert boxes holding the value 6.

1 alert box fires before the post is sent with the $aid variable information of '6'
1 alert box fires after the data is received again with the $aid variable '6'
1 alert box fires after the data is received now with the php response which is again '6'!

I mean WTF? Will this nightmare ever end? That means even the variable in php is being set from the post! How is this possible? I mean look at the php 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.

FIREBUG

In firebug I see

I see POST ->
projects.php ->
POST ->
PARAMETERS ->
aid 6 ->
SOURCE -> aid=%0D%0A6
RESPONSE ->

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.

There is something odd in the source though. It says SOURCE aid=%0D%0A6 instead of the normal SOURCE aid = 6

2ND EDIT

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 php. 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

<?php
require_once "$_SERVER[DOCUMENT_ROOT]/TrakFlex/core/init.php";
if(isset($_POST['aid'])) {  
    $aid = $_POST['aid'];

    try {
        $query_projectInfo = $db->prepare("
            SELECT  projects.account_id,
                    projects.project_name,                  
                    projects.pm,    
                    //...more columns    
            FROM projects
            WHERE account_id = ?                        
        "); 

        $query_projectInfo->bindValue(1, $aid, PDO::PARAM_STR);
        $query_projectInfo->execute();
        $count = $query_projectInfo->rowCount();

        if ($count > 0) {
            echo "<table class='contentTable'>";
            echo "<th class='content_th'>" . "Job #" . "</th>";
            echo "<th class='content_th'>" . "Project Name" . "</th>";              
            //...more table headers      
            while ($row = $query_projectInfo->fetch(PDO::FETCH_ASSOC)) {                
                echo "<tr>";
                echo "<td class='content_td'>" . "<a href='#'>" . $row['account_id'] . "</a>" . "</td>";
                echo "<td class='content_td'>" . $row['project_name'] . "</td>";                
                         //.. more data 
                echo "</tr>";
            }
            echo "</table>";            
        }       
    } catch(PDOException $e) {
        die($e->getMessage());
    }   
} else {
    echo 'could not load projects table';
}

?>    

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 else statement 'could not load projects table'. So I think I'm getting closer, but I'm still stuck. Any ideas?

Answers:

Answer

POST -> aid 6 shows that the server is indeed receiving the data. Something else is clearing $_POST at some point before it reaches the if statement.

While you had require_once "$_SERVER[DOCUMENT_ROOT]/core/init.php"; commented out, your jQuery data contained the expected value. It was still commented out when you posted the Firebug info, hence the database connection error messages. Those messages, which you've since removed, indicates errors on line 7 of projects.php which tells me that your testing a file with more code than you posted here. It's not uncommon for people to show us a portion of their code, in fact, it's encouraged; however, in this case it's problematic because the error doesn't lie in the code you gave us.

Inorder to validate this finding and save your sanity, rename projects.php temporarily to projects.backup.php. Create a new file called, "projects.php" and run the following code (and ONLY the following code) through AJAX:

<?php
if(isset($_POST['aid'])) {          
    $aid = $_POST['aid'];
    echo $aid;
} else {
    echo 'fail';
}
?>

Incidentally, %0D%0A is the Windows® newline combo, CRLF (carriage return + line feed)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.