jQuery Ajax加载/发布[关闭]

                <div class="grid--cell fl1 lh-lg">
                    <div class="grid--cell fl1 lh-lg">
                        This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,   or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making   this question more broadly applicable, <a href="/help/reopen-questions">visit the help center</a>.

                    </div>
                </div>
            </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2013-03-07 21:23:05Z" class="relativetime">7 years ago</span>.</div>
        </div>
    </aside>

Im trying to get the .load method to post to a page and then echo back what was posted to see if it is working, i can't get this to work. I tells me that the variable is undefined, here is what i have:

teachers.php:

<?php
echo $POST['class'];

The JS:

$('#test').load('teacher.php', {class: 'test'});

That is triggered once something is clicked.

#test is a <div> that I want the data to post to.

Thanks for the help.

</div>

It's $_POST instead of $POST. Don't know if that was a typo, but that will also create some issues.

Most of the times problems like these are due to the asynchronicity of AJAX. Everything you load via AJAX (with .post(), .load(), .ajax(), .get()) doesn't "exist" unless the AJAX round trip of request-response has been completed.

Be sure to "use" what you load with AJAX (a HTML bit, a JSON response, ...) in a callback function, that is called on the success of the AJAX request.

For example let's say my page ajax.php returns this string: <div id='ajaxdiv'>hello!</div>, we want to load this (HTML) string in #mydiv:

$("#mydiv").load("ajax.php");
console.log($("#mydiv #ajaxdiv")); <- undefined

It won't work because the code continues regardless of the completion of the request. Instead:

$("#mydiv").load("ajax.php", function () { console.log($("#mydiv #ajaxdiv"));});

works because JQuery makes sure to use the callback function only if the request has been completed. (You can also add parameters like your {class: test}, just put them between the URL and the callback function).

Edit: use $_POST and not $POST.

if your just trying to post information from jQuery to a PHP file when something with the id of test is clicked then do this:

teachers.php:

<?php
    echo $_POST['class'];
?>

The JS:

$('#test').click(function() {
    $.post('teacher.php', {class: 'test'},
        function(output) {
            //output = what is echoed from the php file.
            alert(output);
        );
});