从jQuery接收$ .post时索引是否未定义? [英] index undefined when receiving $.post from jQuery?

查看:91
本文介绍了从jQuery接收$ .post时索引是否未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个非常自我解释的错误。但是,就我所知,索引不仅是100%定义的,它还包含一个值。这是一系列愚蠢问题中的最新问题,这让我今天感到非常疯狂。



以下是您可能看到的代码数百万次应该有效的代码。



jQuery

  $('#project' ).click(function(e){
alert(aid);
$ .post('core / functions / projects.php',{aid:aid})
.done(function) (数据){
alert(aid);
$('#home_div')。hide();
$('#pcd')。fadeIn(1000);
})
.fail(函数(jqXHR,状态,错误){
alert(错误);
});
});

我提醒我两次,价值 6



PHP

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

我收到此错误:

 注意:未定义的索引:第5行的帮助:C:\ xampp \\\\\\\\\\\\\\\\\\\\\\\\ pre> 

所以我添加了一些更深入的检查我的if else现在看起来像这样

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

现在它吐了回来失败。我已经这里,去看看这个错误就像我想象的那样。这并不意味着它对我来说更有意义为什么我得到它。



所以让我们回顾一下。我的 jQuery 正在点击正确的 PHP 文件,因为我从特定的 PHP获得响应文件。我的变量保存一个值,因为 jQuery 提醒我两次它的值为6.但是我的 PHP 文件不是收到这个值?有人可以向我解释原因吗?



编辑



如果我在 jQuery 我收到另一条消息,验证数据是否正在发送。然而,我仍然收到未设置$ _POST索引的错误。这很疯狂

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

现在我得到3个警告框,其值为6。



警告框在发送帖子之前发送,其中$ aid变量信息为'6'

1警告框在使用$ aid变量'6'<再次接收数据后触发br>
1个警报框现在收到数据,同时回复'6'的php响应!



我的意思是WTF?这场噩梦会不会结束?这意味着即使是 php 中的变量也是从帖子中设置的!这怎么可能?我的意思是看看 php 代码所有它回声是'失败'或$ aid和$援助不能有一个值,除非它由帖子设置,而且它不会是给我一个未定义索引的错误。我需要休息一下,我会失去它。



FIREBUG



在firebug中我看到



我看到POST - >

projects.php - >

POST - >

参数 - >

援助6 - >

来源 - >援助=%0D%0A6

回应 - >



回复中没有任何内容,我在这里发布回复的短暂时间是因为我离开了'require_once',我的代码被注释掉了。



虽然源中有些奇怪的东西。它表示SOURCE援助=%0D%0A6而不是正常的SOURCE援助= 6



2ND EDIT



我有一大段代码被注释掉以简化这个例子。代码使用变量和查询数据。然后返回在 php 中创建的表。如果取消注释代码,我可以在我的RESPONSE中以html格式看到该表。如果我转到HTML,我实际上可以看到可视表。发生什么了?为什么这种反应会使我的div而不是表格膨胀?我现在将发布更多代码来解释

 <?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,
//...更多列
FROM项目
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'> 。 工作#。 < /第> 中;
echo< th class ='content_th'> 。 项目名 。 < /第> 中;
//...更多表格标题
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> 中;
// ..更多数据
echo< / tr>;
}
echo< / table>;
}
} catch(PDOException $ e){
die($ e-> getMessage());
}
}其他{
echo'无法加载项目表';
}

?>

因为你可以看到我的完整php文件实际上发回了一个数据表。我可以在FireBug中看到这个表 - > NET - > XHR - > HTML。我还可以在FireBug中看到所有的PHP代码 - > NET - > XHR - > RESPONSE。但是,应该保存表的div只保存 else 语句'无法加载项目表'。所以我觉得我越来越近了,但我还是被卡住了。有什么想法?

解决方案

POST - > aid 6 表明服务器确实正在接收数据。其他东西在之前清除$ _POST 它到达 if 语句。



当你有 require_once$ _SERVER [DOCUMENT_ROOT] /core/init.php; 注释掉时,你的jQuery 数据包含预期值。当您发布Firebug信息时,它仍然被注释掉,因此数据库连接错误消息。这些消息,你已经删除,表明projects.php第7行的错误告诉我你测试的文件的代码比你在这里发布的代码多。人们向我们展示他们的部分代码并不罕见,事实上,它是鼓励的;然而,在这种情况下,这是有问题的,因为错误不在于你给我们的代码。



为了验证这一发现并保存你的理智,重命名 projects.php 暂时到 projects.backup.php
创建一个名为projects.php的新文件,并通过AJAX运行以下代码(以及以下代码):

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

顺便提一句,%0D%0A是Windows®换行组合,CRLF(回车+换行)


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?

解决方案

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)

这篇关于从jQuery接收$ .post时索引是否未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆