1

I have a PHP page with 2 sections side by side. The left pane has a MySQL query that runs and produces a list of categories as links. The right pane should have subcategories that appear when links in the left pane are clicked.

I have some AJAX in an attached JS file that should pass the ID in the links from the left pane, into a query in the right pane. The query should run. It runs if I take out a variable.

The PHP/SQL Queries work fine. JS does not.

I think this is the appropriate way of doing this.

ajax.js

$( document ).ready(function() {
$( 'a' ).on( 'click', function() {
    var a = $( this ).attr( 'id' );

    $.ajax({
        type: "POST",
        url: "categories.php",
        data: "ajax="+a,
        success: function(response){
            alert( a );
        },
        error: function() {
            alert('Error');
        }
    })
});

});

I am being told everything works, but I cannot call $_POST['ajax'] in PHP. Perhaps my page is not being refreshed. There is no form on the page.

Lastly, my file hierarchy has categories.php comprised of a list of includes, which are in a folder that is not public.

4

2 回答 2

1

我认为您的 ajax 语法是错误的。如果您的论点是a并且您的帖子标识符是,请尝试此操作ajax

$( document ).ready(function() {
    $( 'a' ).on( 'click', function() {
        var a = $( this ).attr( 'id' );

        $.ajax({
            type: "POST",
            url: "categories.php",
            data: {ajax : a},
            success: function(response){
                alert( a );
            },
            error: function() {
                alert('Error');
            }
        });
    });
});
于 2013-10-14T18:46:18.050 回答
0

尝试在 $.ajax 调用上设置 async: false 。另外,如何从 php 返回数据?您是否对数据使用 JSON_ENCODE?

于 2013-10-14T18:45:52.587 回答