jQuery的$就交到PHP文件不能正常工作不能正常、文件、工作、jQuery

2023-09-11 22:29:19 作者:淡若清风

所以,这源于一个问题,昨天,迅速失去控制的错误是不寻常的。这个问题仍然存在,但这个问题被搁置,here,我被要求改革,现在涉及当前问题的一个新问题。所以在这里,我们走了。

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, here, and I was asked to reform a new question that now relates to the current problem. So here we go.

问题是基本的性质。如果你帮我昨天已经从$。员额切换到$阿贾克斯后提供一个变量来我 PHP 文件。但是我的 PHP 文件似乎从来没有收到这个变量指出索引未定义。

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 PHP file. However my PHP file seems to never receive this variable stating that the index is 'undefined'.

现在这通常意味着变量持有任何值,命名为不正确,或发送错误。之后,那么这个梅辛(杀我),我仍然看不出有什么原因,我的 PHP 文件不应该接受这个数据一整天。由于我是相当新的这个我真的希望有人能发现一个明显的错误或reccommend另一种可能的解决方案。

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

这里的code

的jQuery

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

PHP

<?php

include "$_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,    
                    //..irrelevant code      
            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>";
            //..irrelevant code          
            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>"; 
                //..irrelevant code
                echo "</tr>";
            }
            echo "</table>";            
        }       
    } catch(PDOException $e) {
        die($e->getMessage());
    }   
} else {
    echo 'could not load projects table';
}

?>   

当我运行此code。通过pressing'#projects我得到2警报。这第一个警报说,'6',这是可变的援助的价值和预期。第二个警报是空白。

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.

现在这里是我弄糊涂。如果文章被发送的为6的价值不在于$ _ POST ['援助']设置?此外,如果这是真的应该不是我的code执行我的条件语句的如果部分的,而的比我的其他?无论哪种方式,这令我奇怪的。我不应该收到的的东西的从后我 PHP 文件?

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 if portion of my conditional statement rather than my else?. Either way this strikes me as odd. Shouldn't I receive something back from my PHP file?

所以,在Firebug我们相信,对不对?如果我打开萤火虫,并通过这样的 萤火虫 - &GT; POST projects.php - &GT; XHR - &GT; POST(标签) - &GT; 我看到6在参数窗口和6中的源文件窗口。然后,如果我点击响应和HTML选项卡他们都持有任何价值。

So in Firebug we trust, right? If I open up Firebug and go through like this Firebug -> POST projects.php -> XHR -> POST(tab) -> 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.

所以不管怎么说,那墙上的文字是我的问题。再次,我真的希望有人能帮助我在这里。我不想再浪费时间在什么应该是一个简单的解决方案。

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.

修改

如果我改变我的PHP文件看起来像这样

If I change my php file to look like this

<?php

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

响应现在是'6'!万岁!我们取得了突破性进展!现在为什么不会加载它我的表而导致的我的查询?

The response is now '6'! Hooray! We made a breakthrough! Now why won't it load my table that results from my query?

侧面说明 为此应以被注意本来,如果我拿走

side note This should of been noted originally if I take away the

if(isset($_POST['aid'])){
//all my code
} else {
    //response
}

和C这样的变量$援助只是很难$ C $

and just hard code the variable $aid like this

$aid = '6';

然后运行 PHP 文件中直接查询成功,并在页面加载表的动态创建。

Then run the PHP file directly the query is successful and the page loads the table its dynamically creating.

此外,在应对其中一个答案要我用

Also in response to one of the answers asking me to use

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

我是用了,但我使用jQuery的v1.10.2并根据这个这些方法都是或将是德precated。无论哪种方式,取得的结果0的区别。

I was using that, but I'm using jQuery v1.10.2 and according to this those methods are or will be deprecated. Either way it made 0 difference in the outcome.

反正现在的问题是。为什么,如果我用了简单的版本我得到echo'd回'6'我的$援助变量。然而,当我尝试用它运行我的查询,我得到的什么。另外请尽量记住,如果我硬$ C C的$ 6,表造成的。

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 nothing. Also please try to remember if I hard code the 6, the table creates.

推荐答案

我想这可能是错误:

data: aid,

应该是

data: {'aid':aid},

这将为$ _ POST ['援助']标签和值,你正在寻找在你的PHP页面。

That will provide the $_POST['aid'] label and value you're looking for in your php page.

编辑:

如果您遇到这个麻烦,我想简化它的测试,对于这样的事情不工作在我的经验,主要理由是:

If you're having trouble with this, I'd simplify it for testing, the main reasons for things like this not working in my experience are:

它没有达到文件

它达到了文件,但该文件的期待着什么比它不同的收货,并且由于控制结构它不返回任何东西。

it's reaching the file but the file's expecting something different than it's receiving and due to control structures it's not returning anything

它到达了正确的数据文件,但也有在PHP文件是$ P $从以往任何时候都返回它pventing它其他错误。

it's reaching the file with the correct data, but there are other errors in the php file that are preventing it from ever returning it.

您可以轻松地排除所有这些在你的案件。

You can easily rule out each of these in your case.